您的位置:首页 > 移动开发 > Android开发

Android——Service(服务)

2016-03-29 10:41 447 查看

1.What is a Service?

服务是Android中实现后台运行的解决方案,适合去处理那些不需要与用户进行交互且要求长期去运行的任务(服务的运行不依赖于任何的界面,即使当程序被切换到后台的时候,或者用户打开另一个应用程序,服务仍能够保持正常的运行)。

注意点:

(1)服务并不是一个独立的进程,而是运行在创建服务所在的应用程序进程当中,它只是其中的一部分。

(2)服务并不是一个线程,它并不是结束主线程的手段,服务不会自动的开启线程,所有的代码都是默认在主线程中的,我们需要在服务的内部创建子线程,并执行具体的任务。

服务主要的两个特征:

(1)执行一个应用程序告诉系统其想在后台进行的操作,直到明确终止服务 调用Context.startService

(2)为其他的应用程序提供功能,调用Context.bindService

2.定义一个服务

onBind()方法是Service中唯一的抽象方法,必须在子类中实现。其他的三个方法:

onCreate():创建服务时调用,只会在Service第一次被创建的时候调用,如果当前Service已经被创建过了,不管怎样调用startService()方法,onCreate()方法都不会再执行

onStartCommand():服务启动的时候调用,希望服务执行的动作,可以写在此方法中。(注意与onCreate方法区别,onStartCommand每次启动服务都会调用)

onDestroy():服务销毁的时候调用,回收资源的操作可以写在此方法中。

public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);

}

@Override
public void onDestroy() {
super.onDestroy();
}
}


服务需要在AndroidManifest.xml中注册:

<service android:name=".MyService"></service>


3.启动停止服务

@Override
public void onClick(View v) {
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;
}
}


分别创建两个Intent对象,调用startService方法和stopService方法启动和终止服务。此外,在服务中可以通过stopSelf()来终止自己。

4.Service与Activity通信

在Activity中可以指定让Service去执行什么任务,只需要让Activity和Service建立关联就好了。onBind方法用于与Activity建立关联:

private DownloadBinder mBinder=new DownloadBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}


public void onDestroy() {
super.onDestroy();
}
class DownloadBinder extends Binder{
public void startDownLoad(){
Log.d("MyService","startDownload executed");
}
}


private MyService.DownloadBinder downloadBinder;
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downloadBinder=(MyService.DownloadBinder)service;
downloadBinder.startDownLoad();
}

@Override
public void onServiceDisconnected(ComponentName name) {

}
};


case R.id.bind_service:
Intent bindIntent=new Intent(this,MyService.class);
bindService(bindIntent,connection,BIND_AUTO_CREATE);
break;
case R.id.unbind_service:
Intent unbindIntent=new Intent(this,MyService.class);
unbindService(connection);
break;


创建ServiceConnection匿名类,重写onServiceConnected,onServiceDisConnected方法,这两个方法分别会在活动与服务绑定和解除绑定时调用。

服务不仅可以和MainActivity绑定还能与任意一个活动绑定。

5.Service的生命周期

每个服务都只会存在一个实例,不管调用多少次startService,只需要调用一次stopService或stopSelf方法。

活动与服务创建连接时,(bindService)调用onBind方法,如果服务没有创建过会先调onCreate方法,然后调用onBind方法。

startService——stopService

bindService——unbindService 两种情况onDestroy都会调用

但是startService,bindService两种方法都执行,必须调用stopService unbindService 两个方法都调用才会执行onDestroy

一个服务只要启动或者绑定后会一直运行,只有两个条件同时不满足,服务才会销毁。

6.使用前台服务

如果你希望服务可以一直保持运行状态,而不会由于内存不足的原因导致被回收,就可以使用前台服务。

7.使用IntentService

服务中的代码是运行在主线程中的,所以在服务中处理一些耗时的操作,很容易出现ANR(Application not Responding)的情况。

此时可以在Service中开启子线程处理耗时的操作,但是注意要stopService/stopSelf让服务停下来

除此之外,可以采用IntentService:

IntentService中的onHandleIntent方法是在子线程中运行的,且服务在运行结束后会自动停止,因此可以在onHandleIntent处理耗时的逻辑。

case R.id.start_intentservice:
Intent intentService=new Intent(this,MyIntentService.class);
startService(intentService);
break;


public class MyIntentService extends IntentService {

public MyIntentService(String name) {
super(name);
}

@Override
protected void onHandleIntent(Intent intent) {

}

@Override
public void onDestroy() {
super.onDestroy();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: