本例子参考网上的例子而写,为学习而用
实现功能1.service后台工作,传递参数(数秒)到Activity显示
2.绑定一个Activity,与之共存亡
环境:android adt android4.0
主界面:MainStandy.java
package com.example.csdn;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
import android.view.View.OnClickListener;
public class MainStandy extends Activity {
Button button1;
Button button2;
Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_standy);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainStandy.this,CountService.class);
startService(intent);
Log.v("MainStandy", "start service!");
}
});
button2= (Button)findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainStandy.this,CountService.class);
stopService(intent);
Log.v("MainStandy", "the service shutdown!");
}
});
button3=(Button)findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainStandy.this,Binder.class);
startActivity(intent);
Log.v("MainStandy", "start the binderService!");
}
});
}
}
CountService.java
package com.example.csdn;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class CountService extends Service{
boolean ThreadDisable;
int count;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
super.onCreate();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(!ThreadDisable){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
Log.v("CountService", "Count is "+count);
}
}
}).start();
}
public void onDestroy(){
super.onDestroy();
this.ThreadDisable=true;
}
class ServiceBinder extends Binder{
public CountService getService()
{
return CountService.this;
}
}
}
HuaHua.java
package com.example.csdn;
import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.view.View;
public class HuaHua extends View{
public HuaHua(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void onDraw(Canvas canvas){
canvas.drawColor(Color.WHITE);
Paint textPaint=new Paint();
textPaint.setColor(Color.RED);
textPaint.setTextSize(35);
canvas.drawText("use the BinderService", 10, 20, textPaint);
textPaint.setColor(Color.BLUE);
textPaint.setTextSize(18);
canvas.drawText("if you used the bind service after this Activity down",20,60, textPaint);
canvas.drawText("the binded service will shutdown with the Activiity", 5, 80, textPaint);
}
}
Binder.java
package com.example.csdn;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class Binder extends Activity{
CountService countService;
public void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(new HuaHua(this));
Intent intent=new Intent(Binder.this,CountService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);//bindservice must use the method of the ServiceConnect
}
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
countService=null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
countService=((CountService.ServiceBinder)service).getService();//use the method of CountService/service
}
};
protected void onDestroy() {
super.onDestroy();
this.unbindService(conn);
Log.v("MainStandy", "out of BinderService!");
}
}
记得在布局中添加<Button>,在Mainfast中添加使用到的<service>和<activity>