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

Android Service(服务)详解·(二)Service基本用法

2017-08-03 09:37 746 查看
原创作品,转载请注明出处^O^
上一篇我们介绍了Android多线程编程的几个知识点,本篇一起来学习Service的基本用法。
我们先来定义一个Service
public class MyService extends Service{
public MyService(){
}

@Override
public IBinder onBind(Intent intent){
throw new UnsupportedOperationException("Not yet implemented");
}
}


可以看到MyService继承自Service类,里面只有一个onBind()方法,onBind()是Service中唯一的一个抽象方法,所以子类中必须要实现。

此时的服务还是空的,如果我们要在里面处理一些事情,还需要实现几个服务中常用的方法
public class MyService extends Service{
public MyService(){
}

@Override
public IBinder onBind(Intent intent){
throw new UnsupportedOperationException("Not yet implemented");
}

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();
}
}


可以看到MyService中又重写了onCreate()、onStartCommand()、onDestroy()

onCreate() 会在服务创建的时候调用

onStartCommand() 会在每次服务启动的时候调用

onDestroy() 会在服务销毁的时候调用

此时MyService类已经创建完成,但要让其生效还需要在AndroidManifest.xml文件中注册
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
</application>


细心的盆友可能看到注册的MyService还附带有两个属性
enabled 是否启动此服务
exported 是否允许其它程序访问此服务

现在,服务已经定义好了,如何启动呢(启动完别忘记关掉啊)
//启动
Intent startIntent = new Intent(context,MyService.class);
startService(startIntent);

//停止
Intent stopIntent = new Intent(context,MyService.class);
stopService(stopIntent);


看到这里是不是觉得很简单,不过有一点要注意下onCreate()只在服务第一次创建的时候调用,而onStartCommand()会在每次启动的时候都调用。

既然Service已经定义完成,那么怎么让它起作用呢?这时候onBind()就样登场了
public class MyService extends Service{
···
private DownloadBinder mBinder = new DownloadBinder();

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

public int getProgress(){
Log.d("MyService","getProgress executed");
return 0;
}

}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
···
}


我们新建了DownloadBinder类,继承Binder,并提供了两个方法startDownload()启动下载,getProgress()获取下载进度(当然只是执行打印日志,并没有实现具体方法)

万事俱备只欠调用,直接上代码
···
private MyService.DownloadBinder downloadBinder;

private ServiceConnection connection = new ServiceConnection() {//创建一个ServiceConnection
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//服务成功绑定时调用
downloadBinder = (MyService.DownloadBinder) iBinder;
downloadBinder.startDownload();
downloadBinder.getProgress();

}

@Override
public void onServiceDisconnected(ComponentName componentName) {//断开连接时调用
}
};

···
//bind
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
···
//unbind
unbindService(connection);


上文章我想细心的朋友已经注意到了,服务也有自己的生命周期
官网解释
官方文档

服务可以由系统运行有两个原因。如果有人调用Context.startService(),系统将检索服务(创建它并调用它的onCreate()方法),然后使用客户机提供的参数调用onStartCommand(Intent,int,int)方法。此服务将继续运行到Context.stopService()或stopSelf()。注意,多个调用Context.startService()不嵌入(尽管他们导致多个相应的调用onStartCommand()),所以不管多少次启动服务将停止一次Context.stopService()或stopSelf();然而,服务可以使用他们stopSelf(int)方法,以确保服务不停止,直到开始意图已经处理。

开始服务,主要有两个额外的操作模式,他们可以决定在运行,这取决于他们从onStartCommand返回值():START_STICKY用于显式地根据需要启动和停止服务,而START_NOT_STICKY或START_REDELIVER_INTENT应该只用于服务仍然运行在处理任何命令发送给他们。有关语义的更多细节,请参阅相关文档。

客户端还可以使用Context.bindService()来获得对服务的持久连接。同样,如果服务尚未运行(调用onCreate()),但不调用onStartCommand(),也会创建服务。客户端将接收IBinder对象,该对象将从其onBind(Intent)方法返回服务,允许客户端调用该服务。只要建立连接(不管客户机是否保留对服务的IBinder的引用),服务就会继续运行。通常IBinder返回的是在aidl中编写的复杂接口。

服务既可以启动,也可以连接到它。在这种情况下,系统将保持服务运行,只要它已经启动,或者有一个或多个与上下文相关的连接。BIND_AUTO_CREATE标记。一旦这些情况都不存在,服务的onDestroy()方法被调用,服务实际上被终止。从onDestroy()返回时,所有清除(停止线程、未注册的接收者)都应该完成。

了解了服务的基本使用和生命周期,我们来学习一些新的技巧

1.前台服务

服务的系统优先级比较低,当系统内存不足时,可能会回收掉正在后台运行的服务。前台服务会一直有一个正在运行的图标显示在系统的状态栏显示,已避免被后台回收。相信这样的例子在你的手机上很常见。

现在就让我们的服务具有前台能力,直接上代码

@Override
public void onCreate() {
super.onCreate();
Log.d("MyService","onCreate executed");
Intent intent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentIntent(pendingIntent)
.build();
startForeground(1,notification);
}


看看是不是觉得很简单,只需用修改onCreate中的代码,调用* startForeground()*后,我们的MyService就进化成了一个前台服务,并在系统状态栏显示出来



image.png

上一篇我们提到,Service中的代码是默认运行在主线程中的,如果直接在服务中进行耗时操作,就准备迎接ANR吧

现在我们上一篇讲到的多线程就起到作用了

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService","onStartCommand executed");
new Thread(new Runnable() {
@Override
public void run() {
//处理逻辑代码
stopSelf();
}
}).start();
return super.onStartCommand(intent, flags, startId);
}


在要进行耗时操作的时候,我们new Thread()在run()方法内处理耗时操作,其中stopSelf()是为了让服务在执行完之后停止下来,和stopService()同理。

问题来了,每次都要开启线程,关闭线程,不仅麻烦,忘了岂不是很尴尬。但是,Android还提供了一个类IntentService,这个类就能很好的解决这两个问题,下面我们就来创建一个IntentService类
public class MyIntentService extends IntentService {

public MyIntentService() {
super("MyIntentService");
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
//打印当前线程的Id
Log.d("MyIntentService","Thread id is" + Thread.currentThread().getId());
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyIntentService","onDestroy executed");
}
}


我们在onHandleIntent()方法中打印当前线程id和Activity所在线程id进行对比



可以看到不仅所在线程不同,而且在执行完打印操作后自动停止了,完美解决问题。

好了,服务的基本使用讲到这里,后续会结合源码来具体分析四大组件之一的Service.

我写了个Demo,涵盖这两篇涉及到的所有知识点
Demo下载 密码:gxuc

相关文章
Android Service(服务)详解·(一)相关知识点引入

每星期至少一篇跟新本系列,感兴趣可以关注。

一起学习,一起进步。



个人公众号 Coder栈
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 多线程