Service生命周期和启动方式
1:Service概述
定义: Service是在后台处理一些耗时的逻辑,
或执行某些需要长期运行的任务的应用程序组件。
![](https://i-blog.csdnimg.cn/blog_migrate/dd946831aec7af922624c3f4d05e912b.png)
2:Service启动方式
第一步:新建一个MyService继承自Service
public class MyService extends Service{……}
第二步:Service每一个都必须在AndroidManifest.xml中注册
EventBus
<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">
<service android:name=".MyService">
</application>
第三步:Service注册时可以有一些属性
<service android:name=".MyService"
android:exported=“true”
android:process=“:remote”
>
第一种:启动
使用startService进行启动
Intent it=new Intent(this, SimpleService.class);
startService(it);
一旦“启动”将与访问它的组件无关,即使访问它的组件被销毁了,这个服务也一直运行下去,直到手动调用停止服务才被销毁
第二种:绑定
Intent it=new Intent(this, SimpleService.class);
bindService(intent, conn,Context.BIND_AUTO_CREATE);
在绑定模式下,当所有绑定到Service的客户端都解绑的时候,service会被销毁
四个手动方法
startService()
stopService()
bindService()
unbindService()
五个自动方法
onCreate()
onStartCommand()
onDestroy()
onBind()
onUnbind()
3:Service两种启动方式区别
1.创建Service重写方法
public class MyServices extends Service {
private static final String TAG = "amy";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"onCreate"); }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"onStartCommand");
return super.onStartCommand(intent, flags, startId); }
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG,"onBind");
return null;}
@Override
public void onRebind(Intent intent) {
Log.d(TAG,"onRebind");
super.onRebind(intent); }
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG,"onUnbind");
return super.onUnbind(intent);}
@Override
public void onDestroy() {
Log.d(TAG,"onDestroy");
super.onDestroy();
}
}
2.清单文件中注册Service
<service android:name=".MyServices"/>
3.启动Service
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services);
//参数一:上下文 参数二:自定义服务类.class
intent = new Intent(this,MyServices.class);
startService(intent);
}
4.解绑Service
@Override
protected void onDestroy() {
super.onDestroy();
//停止服务
stopService(intent);
}
4:
Service两种启动方式
1.创建Service
public class MyServices extends Service {
private static final String TAG = "amy";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"onCreate");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG,"onBind");
return new MyBinder();
}
class MyBinder extends Binder{
public MyServices getMyServices(){
return MyServices.this;
}
}
public void playMusic(){
Toast.makeText(this, "播放音乐", Toast.LENGTH_SHORT).show();
}
public void stopMusic(){
Toast.makeText(this, "停止音乐", Toast.LENGTH_SHORT).show();
}
}
2.获得ServiceConnection对象
public class ServicesActivity extends AppCompatActivity {
Intent intent;
MyServices myServices;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myServices = ((MyServices.MyBinder)service).getMyServices();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services);
//参数一:上下文 参数二:自定义服务类.class
intent = new Intent(this,MyServices.class);
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
//解绑
unbindService(connection);
}
public void playMusic(View view) {
myServices.playMusic();
}
}
3.绑定Service并调用服务中的方法
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services);
//参数一:上下文 参数二:自定义服务类.class
intent = new Intent(this,MyServices.class);
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
public void playMusic(View view) {
myServices.playMusic();
}
}
4.解绑
@Override
protected void onDestroy() {
super.onDestroy();
//解绑
unbindService(connection);
}