Android Service

from : http://www.apkbus.com/forum.php?mod=viewthread&tid=15649

一、Service的概念
  Service是Android程序中四大基础组件之一,它和Activity一样都是Context的子类,只不过它没有UI界面,是在后台运行的组件.

  二、Service的生命周期
  Service对象不能自己启动,需要通过某个Activity、Service或者其他Context对象来启动.启动的方法有两种,Context.startService和Context.bindService().两种方式的生命周期是不同的,具体如下所示.
  使用context.startService() 启动Service是会会经历:
  context.startService() ->onCreate()- >onStart()->Service running
  context.stopService() | ->onDestroy() ->Service stop
  如果Service还没有运行,则android先调用onCreate()然后调用onStart();如果Service已经运行,则只调用onStart(),所以一个Service的onStart方法可能会重复调用多次.
  stopService的时候直接onDestroy,如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行.该Service的调用者再启动起来后可以通过stopService关闭Service.
  所以调用startService的生命周期为:onCreate --> onStart(可多次调用) --> onDestroy
  使用使用context.bindService()启动Service会经历:
  context.bindService()->onCreate()->onBind()->Service running
  onUnbind() -> onDestroy() ->Service stop
  onBind将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service运行的状态或其他操作.这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用onUnbind->onDestroy相应退出.
  所以调用bindService的生命周期为:onCreate --> onBind(只一次,不可多次绑定) --> onUnbind --> onDestory.
  在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次.

context.startService()

java代码:
public class MyService extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button buttonStart, buttonStop,buttonBind,buttonUnbind,buttonCount;
private static final String TAG = "ServicesDemo";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

buttonStart = (Button) findViewById(R.id.bt1);
buttonStop = (Button) findViewById(R.id.bt2);
buttonBind = (Button) findViewById(R.id.bt3);
buttonUnbind = (Button) findViewById(R.id.bt4);
buttonCount = (Button) findViewById(R.id.bt5);

buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
buttonBind.setOnClickListener(this);
buttonUnbind.setOnClickListener(this);
buttonCount.setOnClickListener(this);

}

//
//通过ServiceConnection的内部类实现来连接Service和Activity
///

private ICountService countService=null;

private ServiceConnection serviceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
countService = (ICountService) service;
Log.v("CountService", "on serivce connected, count is "
+ countService.getCount());
}

@Override
public void onServiceDisconnected(ComponentName name) {
countService = null;
}

};



@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
Log.d(TAG, "onClick: starting srvice");
//activity传递值给service,service里的onStart()方法有个intent参数,可以接收到activity传递的值
Intent intent1= new Intent();
Bundle bundle1=new Bundle();
bundle1.putString("activity-to-service","i come from activity");
intent1.putExtras(bundle1);
intent1.setClass(this, CountService.class);
this.startService(intent1);
break;
case R.id.bt2:
Log.d(TAG, "onClick: stopping srvice");
this.stopService(new Intent(this, CountService.class));
break;
case R.id.bt3:
Log.d(TAG, "onClick: bind srvice");
//bindservice启动的时候不会调用onstart方法
//activity传递值给service,service里的onbind()方法有个intent参数,可以接收到activity传递的值
Intent intent2= new Intent();
Bundle bundle2=new Bundle();
bundle2.putString("activity-to-bindservice","i come from activity");
intent2.putExtras(bundle2);
intent2.setClass(this, BindCountService.class);
this.bindService(intent2,this.serviceConnection, BIND_AUTO_CREATE);
break;
case R.id.bt4:
Log.d(TAG, "onClick: unbind srvice");
this.unbindService(serviceConnection);
break;
case R.id.bt5:
//随时和service通信,让activity获取到service里count的值是多少
if(countService!=null)
Log.d(TAG, "onClick: getCount="+countService.getCount());
break;
}
}

}
复制代码
java代码:
public class CountService extends Service{

private boolean threadDisable;
private String activityValue=null;
private int count;

@Override
public IBinder onBind(Intent intent) {
Log.v("CountService", "on bind");
return null;
}

@Override
public void onCreate() {
super.onCreate();
Log.v("CountService", "on create");
new Thread(new Runnable() {

@Override
public void run() {
while (!threadDisable) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
count++;
Log.v("CountService", "Count is " + count);
}
}
}).start();
}

@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
super.onRebind(intent);
Log.v("CountService", "on rebind");
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.v("CountService", "on start");
//接收activity传递过来的值
Bundle b=intent.getExtras();
activityValue=b.getString("activity-to-service");
Log.v("CountService", activityValue);
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.v("CountService", "on unbind");
return super.onUnbind(intent);

}

@Override
public void onDestroy() {
super.onDestroy();
this.threadDisable = true;
Log.v("CountService", "on destroy");
}

public int getCount() {
return count;
}
}
复制代码
context.bindService()

java代码:
public interface ICountService {
public abstract int getCount();
}
复制代码
java代码:
public class BindCountService extends Service {

private int count;
private boolean threadDisable;
private String activityValue=null;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.v("BindCountService", "on bind");
//接收activity传递过来的值
Bundle b=intent.getExtras();
activityValue=b.getString("activity-to-bindservice");
Log.v("BindCountService", activityValue);
return serviceBinder;
}

private ServiceBinder serviceBinder=new ServiceBinder();

public class ServiceBinder extends Binder implements ICountService{
@Override
public int getCount() {
return count;
}
}

@Override
public void onCreate() {
super.onCreate();
Log.v("BindCountService", "on create");
new Thread(new Runnable() {

@Override
public void run() {
while (!threadDisable) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
count++;
Log.v("BindCountService", "Count is " + count);
}
}
}).start();
}

@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
super.onRebind(intent);
Log.v("BindCountService", "on rebind");
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.v("BindCountService", "on start");
}

@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.v("BindCountService", "on unbind");
return super.onUnbind(intent);
}

@Override
public void onDestroy() {
super.onDestroy();
this.threadDisable = true;
Log.v("BindCountService", "on destroy");
}

}
复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值