最近,想到Service用法,好像大脑中就简单的记得,一些简单用法,有些模糊,今天补上(可能最近上课上蒙了,有些忘记了),现在看看Android四大组件中的Service.简单说一些Service:它主要用于在后台处理一些耗时的逻辑,或者去执行某些需要长期运行的任务(很耗时的任务请使用线程内部)。必要的时候我们甚至可以在程序退出的情况下,让Service在后台继续保持运行状态,service在后台处理一些数据。
简单的使用方法
1.直接startService
它会依次执行Service的 onCreate –> onStartCommand 方法(第一次启动的时候)
之后再调用startService时候 就只是会执行onStartCommand(log日志图片就不贴了,博客显示图片有点问题)(所以一般我们写逻辑代码在onStartCommand方法中写)
2.调用stopService
它会执行Service的onDestory方法,停止Service
3.调用bindService,用于Service和Activity的绑定,建立关联,Service的生命周期和Activity一致(当Activity退出时Service也随之退出)
调用bindService会执行Service的(一般第三个标志位为BIND_AUTO_CREATE)方法,执行onCreate –> onBind()方法 然后Activity中ServiceConnection中onServiceConnected方法执行
4.onBind如何实现Activity与Service的交互
利用在Service创建一个继承Binder内部类,利用内部类可以访问外类的方法,然后再onBind()返回该类,Activity在serviceConnection接收Binder向上转型就可以直接在Activity中调用Service中的方法(已经bind了的service再次调用Bindservice没有效果(突然发现很多跨进程,跨组件中都有Binder影子,那天看一看)
Service的销毁方法
- 调用startService ,直接调用stopService,结束生命
- 调用bindService,直接调用unbindServie ,它会执行onDestory方法
- 调用了startService,bindService需要调用stopService和unbindService方法才会销毁(执行顺序都可以)
Service onBind的代码实例
Activity的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class SecondActivity extends Activity {
private MyService myservice;
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("SecondActivity","serviceConnected");
myservice=((MyService.MyBinder)service).getservice();
myservice.execute();//在service绑定的时候,执行该方法
}
@Override
public void onServiceDisconnected(ComponentName name) {
myservice=null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Log.e("time","SecondActivity on Create");
Intent intent=new Intent(SecondActivity.this,MyService.class);
bindService(intent,connection,BIND_AUTO_CREATE);
startService(intent);
unbindService(connection);
stopService(intent);
}
@Override
protected void onStart() {
Log.e("SecondActivity","Start");
super.onStart();
}
@Override
protected void onResume() {
Log.e("SecondActivity","on Resume");
super.onResume();
}
@Override
protected void onPause() {
Log.e("SecondActivity","on Pause");
super.onPause();
}
@Override
protected void onRestart() {
Log.e("SecondActivity","on Restart");
super.onRestart();
}
@Override
protected void onStop() {
Log.e("SecondActivity"," on Stop");
super.onStop();
}
@Override
protected void onDestroy() {
Log.e("SecondActivity"," on Destory");
unbindService(connection);
super.onDestroy();
}
}
Myservice的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class MyService extends Service {
private final IBinder binder=new MyBinder();
public class MyBinder extends Binder{
public MyService getservice(){
return MyService.this;
}
}
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
Log.e("Myservice","onBind");
//throw new UnsupportedOperationException("Not yet implemented");
return binder;
}
public void execute(){
Log.e("Myservice","execute");
}
@Override
public void onCreate() {
super.onCreate();
Log.e("Myservice","onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("Myservice","onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.e("Myservice","onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.e("Myservice","onDestory");
super.onDestroy();
}
}