如何编写自己的服务? 和Android上的广播和内容提供器差不多,Android都提供了基类,只要继承对应的类,然后实现自己的方法就可以了。
编写一个自定义服务的套路是一样的。本文中例子的界面如图所示:
其中开始服务和结束服务只是在活动中开启和关闭了服务,但是在活动和服务之间并没有过多的交流。为了和在活动和服务之间进行交流,需要特殊的设置,本文给出了绑定服务和解除绑定服务的例子。
布局代码如下:
<Button
android:id="@+id/startService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="开始服务" />
<Button
android:id="@+id/unbindService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="62dp"
android:text="解除绑定服务" />
<Button
android:id="@+id/bindService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/unbindService"
android:layout_alignLeft="@+id/startService"
android:layout_marginBottom="25dp"
android:text="绑定服务" />
<Button
android:id="@+id/stopService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/startService"
android:layout_below="@+id/startService"
android:layout_marginTop="34dp"
android:text="停止服务" />
其中活动代码如下:
public class MainActivity extends Activity implements OnClickListener{
//开始和停止服务按钮
private Button startService;
private Button stopService;
//绑定和解除绑定服务
private Button bindService;
private Button unbindService;
private MyService.DownloadBinder downloadBinder;
//服务连接对象
private ServiceConnection connection = new ServiceConnection() {
//服务断开的时候调用
@Override
public void onServiceDisconnected(ComponentName name) {
}
//服务连接的时候调用
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
downloadBinder = (MyService.DownloadBinder)binder;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//开始和停止服务 这些事件只是开启或者关闭了某个服务 但是活动和服务是没有任何交互的
startService = (Button)findViewById(R.id.startService);
stopService = (Button)findViewById(R.id.stopService);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
//绑定和解除绑定服务 这个活动和服务之间有所联系
bindService = (Button)findViewById(R.id.bindService);
unbindService = (Button)findViewById(R.id.unbindService);
bindService.setOnClickListener(this);
unbindService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startService:
Intent startIntent = new Intent(this,MyService.class);
startService(startIntent);
break;
case R.id.stopService:
Intent stopIntent = new Intent(this,MyService.class);
stopService(stopIntent);
break;
case R.id.bindService:
Intent bindIntent = new Intent(this,MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
break;
case R.id.unbindService:
unbindService(connection);
break;
default:
break;
}
}
}
自定义服务如下:
//自定义服务 继承自Service
//Android的四个部分:活动 广播 服务 内容提供器 很多实现方式都是一样的,继承已有的类,然后实现自己的方法,而且都要在配置文件中进行注册
public class MyService extends Service {
//为了实现活动与服务之间的交互,举个例子,在Myservice里面提供一个下载功能,然后在活动中可以决定何时可以开始下载,并且可以随时查看进度。
private DownloadBinder mBinder = new DownloadBinder();
class DownloadBinder extends Binder{
public void startDownload(){
Log.d("MyService", "开始下载。。。");
}
public int getProgress(){
Log.d("MyService", "获取下载进度。。。");
return 0;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
//服务创建时调用的方法
public void onCreate(){
super.onCreate();
Log.d("MyService", "服务创建。。。");
}
//服务启动时候调用的方法
public int onStartCommand(Intent intent, int flags, int startId){
Log.d("MyService", "服务启动。。。");
return super.onStartCommand(intent, flags, startId);
}
//服务销毁时调用
public void onDestroy(){
super.onDestroy();
Log.d("MyService", "服务销毁。。。");
}
}
和活动,广播一样,在配置文件中需要注册服务:
<service android:name=".MyService"></service>
运行结果如图所示:
服务的生命周期
一旦startService()方法,相应的服务就会启动,并回调onStartCommand方法。如果这个服务之前没有创建过,onCreate方法先执行,而后执行OnStartCommand方法。服务启动后就一直保持运行状态,知道stopService方法调用。每次调用startService方法,onStartCommand就会执行一次。
通过bindService来获取一个服务的持久连接,这时回调onBind方法,如果这个服务没有创建过,那么先执行onCreate方法。之后,调用onBind方法返回IBinder对象的实例。
如果即调用了startService方法,又调用了bindService方法,销毁服务的时候,要同时调用stopService和unbindService方法,才能销毁服务。
前台服务
当内存不足的时候,往往会回收掉后台服务,那么如何才能不被回收呢?可以考虑前台服务。前台服务和后台服务的区别在于,前台服务非常类似于通知的效果,会一直存在于状态栏。前台服务的使用也比较方便一些。
只要修改一下上述服务中的onCreate代码就可以:
//服务创建时调用的方法
public void onCreate(){
super.onCreate();
Notification notification = new Notification(R.drawable.ic_launcher,"前台服务",System.currentTimeMillis());
Intent notificationIntent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "标题", "内容", pendingIntent);
startForeground(1, notification);
//此句话就讲服务变成了前台服务
Log.d("MyService", "服务创建。。。");
}
其运行界面:
使用IntentService
服务的代码默认运行在主线程当中,那么在onStartCommand中可以通过启动一个子线程来做一些事情。那么线程执行之后,如何自己结束呢? 可以再线程run方法的最后加上stopSelf()方法。但是如果编码的人员忘记了启动线程做事情,或者启动了线程忘了关掉,怎么办?Android里面提供了IntentService方法。
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
//调用父类构造函数
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("MyIntentService", "线程ID是"+Thread.currentThread().getId());
}
public void onDestrory(){
super.onDestroy();
Log.d("MyIntentService", "服务销毁");
}
}
使用方法和普通Service一样道理