1.创建服务
New->Service->Service
或新建类继承Service
public class MyService extends Service{
要在AndroidManifest中注册。
2.重写3个方法
2.1 onCreate()
服务创建时调用。多次启动服务只调用一次。
2.2 onStartConmmand()
每次启动服务都调用。
2.3 onDestroy()
服务销毁时调用。
3.启动和停止服务
3.1启动服务
Intent startIntent = new Intent(this, MyService.class);
startService(startIntent);
3.2停止服务
Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent);
或在MyService类中调用stopSelf()方法。
4.绑定服务
4.1在MyService类中,新建Binder类,并实例化
private DownloadBinder mBinder = new DownloadBinder();
class DownloadBinder extends Binder{
public void startDownload(){
Log.d(TAG, "startDownload: ");
}
public int getProgress(){
Log.d(TAG, "getProgress: ");
return 0;
}
}
4.2在onBind()方法中返回该实例
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
return mBinder;
}
4.3在MainActivity中,创建ServiceConnection匿名类,并实例化。
向下转型得到DaownloadBinder实例。 private MyService.DownloadBinder mDownloadBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected: ");
mDownloadBinder=(MyService.DownloadBinder)service;
mDownloadBinder.startDownload();
mDownloadBinder.getProgress();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected: ");
}
};
4.4绑定服务
Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent,connection,BIND_AUTO_CREATE);
参数3:表示活动和服务绑定后自动创建服务。MyService类执行onCreate()方法,但不执行onStartConmmand()方法。
4.5取消绑定
unbindService(connection);
5.总结
5.1启动与停止
调用startService(),启动服务,并回调onStartConmmand()方法,如果服务之前没创建过,先执行onCreate()方法。
多次启动服务只调用一次onCreate()方法,但每次都调用onStartConmmand()方法。
调用stopService()或stopSelf()方法,停止服务,回调onDestroy()方法。
每个服务只有一个实例,所有不管调用了多少次startService(),只调用一次停止服务即可。
5.2绑定与解除绑定
调用bindService(),绑定服务,回调onBind()方法。如果服务之前为创建,先执行onCreate()方法。
调用unbindService()方法,解除绑定,回调onDestroy()方法。
5.3
如果调用了startService()和bindService()方法,需要调用stopService()和unbindService()方法,onDestroy()方法才调用。
6.使用前台服务
服务优先级较低,易被回收。
6.1修改MyService的onCreate()方法
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate: ");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = 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(pi)
.build();
startForeground(1,notification);
}
7.标准服务的写法
public class MyService extends Service {
private DownloadBinder mBinder = new DownloadBinder();
class DownloadBinder extends Binder{
public void startDownload(){
Log.d(TAG, "startDownload: ");
}
public int getProgress(){
Log.d(TAG, "getProgress: ");
return 0;
}
}
public MyService() {
Log.i(TAG, "MyService: ");
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
return mBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate: ");//非前台服务可以省略以下部分
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = 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(pi)
.build();
startForeground(1,notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {//新建子线程
@Override
public void run() {
//执行具体逻辑
stopSelf();//停止服务
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
8.使用IntentService
可以自动创建子线程、自动停止的服务。
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {//运行在子线程,自动执行完自动停止服务。
Log.d(TAG, "onHandleIntent: " +"Thread id is "+Thread.currentThread().getId());
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}