1、服务,android四大组件之一
1、服务Service,它有唯一的一个抽象方法,onBind()必须要在子类中实现
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
super.onCreate();
Log.d("MyService", "onCreate executed");
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "onStartCommand executed");
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
super.onDestroy();
Log.d("MyService", "onDestroy executed");
}
}switch (v.getId()) {
case R.id.start_service:
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent); //启动服务
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent); // 停止服务
break;
default:
break;
}<service android:name="com.example.servicetest.MyService" ></service>这里重写了onCreate()、onStartCommand()和onDestroy()这三个方法。其中onCreate()是在服务第一次创建的时候调用,而onStartCommand()方法则在每次启动服务的时候都会调用。在switch中调用startService()方法来启动MyService这个服务,而stopService()则是停止服务。而且,在MyService中任何地方调用stopSelf()就会停止服务。onCreate()
2、活动和服务进行通信,虽然服务是在活动中启动,但在启动了服务后,活动活动和服务基本就没有关系了。但是,活动和服务可以靠onBind()方法关联
private DownloadBinder mBinder = new DownloadBinder();
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
public class DownloadBinder extends Binder {
public void startDownload() {
Log.d("MyService", "startDownload executed");
}
public int getProgress() {
Log.d("MyService", "getProgress executed");
return 0;
}
}private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
downloadBinder = (MyService.DownloadBinder) service;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
};switch (v.getId()) {
case R.id.bind_service:
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE); // 绑定服务
break;
case R.id.unbind_service:
unbindService(connection);
break;
default:
break;
}首先在MyService里新建一个DownloadBinder类,并集成Binder,然后在吧原来的onBinde()返回一个DownloadBinder类的实例。然后在Activity里新建一个ServiceConnection的匿名类,在里面重写onServiceConnected()和onServiceDisconnected()方法,然后在方法里通过向下转型获得DownloadBinder实例,这个实例就可以调用MyService里的startDownload()和getProgress()方法。然后通过bindService()方法,这个方法接收三个参数1:Intent对象,2:SericeConnection实例,3:是一个标志位,这里传入BIND_AUTO_CREATE表示在活动和服务进行绑定后自动创建服务。当调用bindService()这个方法后,就会直接调用SericeConnection实例中的onServiceConneted()。
3、服务的生命周期,项目中,一旦调用了startService()方法,相应的服务就会启动,并回调onStartCommand()方法。如果这个服务没有创建,则onCreate()方法会先于onStartCommand()方法执行。服务启动了之后会一直保持运行状态,知道stopService()或者stopSelf()方法被调用。需要注意每次调用startService(),onStartCommand()就会执行一次,丹实际上每个服务都只会存在一个实例。所以,不管调用多少次startService(),只需调用一次stopService()或stopSelf(),服务就会停下。
另外还可以调用bindService()来获取一个服务的持久链接,这是就会回调服务中的onBind()方法。如果这个服务未创建,onCreate()方法会先于onBind()方法执行。之后,调用onBind()方法里返回IBinder对象实例,这样就能自由的和服务进行通信了。只要调用方法和服务之间的连接没有断开,服务就会一直保持运行状态。
当调用了startService()后,再去调stopService(),这是服务中的onDestroy()方法就会执行,表示服务已经销毁。类似的,binService()和unbindService()这两方法也是同理。但是,一个服务即调用了startService()又调了bindService(),则需要同时调用stopService()和unbindService()方法,onDestroy()方法才会执行。因为之前说过,一个服务,只要被启动或被绑定后就会一直处于运行状态,所以,要同时解绑和停止服务,才会调用onDestroy()。
本文详细介绍了Android服务的概念及其生命周期,包括如何启动、停止服务,服务与活动间的通信方式,以及服务的绑定过程。通过示例代码展示了onCreate(), onStartCommand() 和 onDestroy()等关键方法的作用。
:服务(2)服务的基本用法&spm=1001.2101.3001.5002&articleId=79614135&d=1&t=3&u=c0eff7d5dd38482e8578f81cfc413b50)
564

被折叠的 条评论
为什么被折叠?



