service(服务)是安卓中的四大组件之一,它通常用作在后台处理耗时的逻辑,与Activity一样,它存在自己的生命周期,也需要在清单文件中配置相关信息,本博客将对Service的各个知识点进行详细讲解。
Service的生命周期
我们先来看一下谷歌官方图片:
从上述图片可以看到两种不同的启动方式其生命周期也不同:
启动的服务:
startService()->onCreate()->onStartCommand()->running->stopService()/stopSelf()->onDestroy()->stopped
其中,服务未运行时会调用一次onCreate(),运行时不调用。
绑定的服务:
bindService()->onCreate()->onBind()->running->onUnbind()->onDestroy()->stopped
服务起始于 onCreate() ,终止于 onDestory()
服务的开关过程,只有 onStartCommand() 可多次调用,其他在一个生命周期只调用一次。
这两个过程不是完全独立,也可以绑定一个由 startService() 启动过的服务
如何创建不被系统杀死的服务
服务不被杀死包括三种情况
1.系统根据资源分配情况杀死服务
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
Intent intent = new Intent(this, MyService.class);
startActivity(intent);
switch (view.getId()){
//开启服务
case R.id.btn_1:
intent.putExtra("msg","服务开始了,请接收我的信息");
startService(intent);
break;
//停止服务
case R.id.btn_2:
stopService(intent);
break;
}
}
}
Service:
public class MyService extends Service {
public MyService() {
}
int i=0;
// 开启服务
@Override
public void onCreate() {
super.onCreate();
}
// 开始执行服务内容
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String msg=intent.getStringExtra("msg");
if(!msg.isEmpty()){
Log.i("MyService","onStartCommand:"+msg);
}else {
Log.i("MyService","onStartCommand:没有传值");
}
new Thread(new Runnable() {
@Override
public void run() {
for (i=0;i<100;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("MyService","onStartCommand:"+i);
}
}
}).start();
// 返回一个粘性服务
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("MyService","onDestroy");
i=100;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.fw.MainActivity">
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启服务"
android:onClick="click"
/>
<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止服务"
android:onClick="click"
/>
<Button
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bind服务"
android:onClick="click"
/>
<Button
android:id="@+id/btn_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止Bind服务"
android:onClick="click"
/>
</LinearLayout>
AndroidMainifest涉及知识:
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>