Android中Service的使⽤⽅法
⽬录
Service 介绍
Service两种启动⽅式
使⽤
测试
IntentService
Activity与Service之间的通信
继承Binder类
Messenger
AIDL
Service 介绍
A Service is an application component that can perform long-running operations in the background and does not
provide a user interface. Another application component can start a service and it will continue to run in the
background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
Service是⼀个没有⽤户界⾯的应⽤程序组件,可以在后台长时间运⾏。另外⼀个应⽤程序组件可以启动⼀个Service,它可以在⽤户切换到其他应⽤的时候依旧保持在后台运⾏。另外,⼀个组件可以绑定到Service上,与它进⾏通信,甚⾄是IPC(进程间通信)。
Service两种启动⽅式
startService
bindService
其⽣命周期官⽅API介绍:
Paste_Image.png
需要注意的点:
1.通过startService启动的Service,只有调⽤了stopService(外部组件调⽤)或stopSelf(Service内部⾃⼰调⽤),才会停⽌。
2.通过startService启动的Service,在Service运⾏中⽆法与Service进⾏交互,即外部组件只能控制其开关,⽆法进⾏交互。
3.通过startService启动的Service,与外部组件之间没有关系,外部组件的⽣死跟它没有联系。
3.通过startService启动的Service,启动之后重复启动的话不会触发onCreate⽅法,但是会重复触发onStartCommand,其实貌似
也算是数据交流了吧,不过是单向的。
4.bindService启动的Service表⽰将⼀个Service绑定到⼀个组件上,其⽣命周期与该组件的⽣命周期绑定在⼀起,⽐如绑到⼀个
Activity,Activity在Destroy后Service跟着就Destroy了。
4.注意是不能绑定⼴播的,因为⼴播发完了其⽣命就到头了,常⽤的是绑Activity,还可以是Service。
5.bindService启动的Service在使⽤完之后可以解除绑定,当⼀个Service上的所有绑定的组件都解绑之后,它就会被销毁。
6.可以同时使⽤两种启动⽅式,此时的⽣命周期就变的有些复杂了,两种关联到⼀起,总结来说的话,先startService与先
bindService两种⽅式达到的效果是⼀样的,即此时unbindService的话,Service并不会结束,⽽是要等到stopService才会结束(onDestroy);若是此时stopService,也不会结束,⽽是要等到unbindService时才会结束(由于已经调⽤过stopService,此时会直接onDestroy)。
Paste_Image.png
当旧client与service之间的关联在onUnbind中都结束之后,新client绑定时,
必须是onUnbind返回true,且服务在解绑之后没有销毁安卓intent用法
使⽤
startService
创建类继承Service,在启动组件中调⽤:
Intent intent1 = new Intent(ServiceActivity1.this, MyService1.class);
startService(intent1);
停⽌⽅法同上
bindService
1.创建类继承Service,其中默认会有⼀个onBind⽅法,返回是⼀个IBinder类型对象,这个返回值就是与组件之间通信的关键。可以
通过⾃定义内部类继承Binder,在这个类中返回Service,然后在组件中通过返回的IBinder类型对象获取到Service对象从⽽进⾏操作;
2.在组件中新建⼀个ServiceConnection对象,必须重写两个⽅法,onServiceConnected(建⽴连接(bind_service)时调⽤)、
onServiceDisconnected(⼀般都不调⽤,除⾮是意外情况,unbind_service并不调⽤这个),在onServiceConnected中获取到Service对象进⾏操作。
MyService:
public class MyService1 extends Service {
@Override
public void onCreate() {
Log.i("test_out","----->onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("test_out","----->onStartCommand");
StartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i("test_out","----->onDestroy");
}
public class MyBinder extends Binder {
public MyService1 getService(){
return MyService1.this;
}
}
private MyBinder mBinder = new MyBinder();
@Override
public IBinder onBind(Intent intent) {
Log.i("test_out","----->onBind");
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("test_out","----->onUnbind");
return true;
}
@Override
public void onRebind(Intent intent) {
Log.i("test_out","----->onRebind");
}
public int getCount(){
return (int) (Math.random() * 10);
}
}
在Activity中建⽴连接:
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) { mService = ((MyService1.MyBinder)service).getService();
textView.setText("" + Count());
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("test_out","----->onServiceDisconnected");
}
};
启动与停⽌:
bindService(intent3, serviceConnection, Service.BIND_AUTO_CREATE);
第⼀个参数是intent,第⼆个是上⾯的serviceConnection,第三个表⽰绑定⽅式,Service.BIND_AUTO_CREATE表⽰绑定时不存在的话就⾃动创建。
解绑:unbindService(serviceConnection);
测试
startService -> stopService
Paste_Image.png
startService -> startService -> startService -> stopService
Paste_Image.png
果然重复start会调⽤onStartCommand,那么就相当于可以发指令。
bindService -> unbindService
Paste_Image.png
bindService -> 按下返回键
Paste_Image.png
startService -> bindService -> unbindService
Paste_Image.png

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。