service简介
应用的一部分,与应用执行在同一进程当中
有着较长的生命周期,没有可视化界面,运营在后台的一种服务程序
通过两种启动模式。开启不同的工作模式
android中的各种进程
**Home process 桌面进程
Visible process 可视进程
Foregroound process 前台进程
Service process 服务进程
Background process 后台进程
Content Provider process 内容提供器进程
Empty process 空进程
**
android的进程机制
系统提供回收机制
应用程序刚开启都会启动一个进程
进程的回收是有优先级的,排除桌面进程和可视化进程,其余进程的优先级由高到低排列。
一个应用程序所有的界面关闭时,会暂时处于空进程状态,系统会随时对其进行回收。
Android的service生命周期
代码校验
活动页面中加载4个按钮,来对应两种模式服务的开启和关闭
<Button
android:id="@+id/start_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="start_service"/>
<!-- android:onClick="onClick"说明时调用的时onClick方法 -->
<Button
android:id="@+id/stop_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="stop_service" />
<Button
android:id="@+id/bind_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="bind_service" />
<Button
android:id="@+id/unbind_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="unbind_service" />
新建一个MyService类,继承于Service
public class MyService extends Service {
@Override
public void onCreate() {
Log.d("MyService", "MyService创建了。。。。");
super.onCreate();
}
/**
* START_STICKY = 1;粘性:如果service被回收,不会尝试再次启动
* START_NOT_STICKY = 2;非粘性:如果service被回收,尝试再次启动,但是不会保留最后的intent
* START_REDELIVER_INTENT = 3;重新傳入Intent:如果service被回收,尝试再次启动,保留最后的intent
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "MyService开启了。。。。");
Log.d("MyService", Thread.currentThread().getName());
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d("MyService", "MyService已经绑定。。。。");
return null;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("MyService", "MyService已经解绑。。。。");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.d("MyService", "MyService销毁了。。。。");
super.onDestroy();
}
}
MainActivity类
public class MainActivity extends AppCompatActivity {
private Myconnection conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conn = new Myconnection();
Log.d("MainActivity", Thread.currentThread().getName());
}
public void onClick(View v){
switch (v.getId()){
case R.id.start_service:
Intent start = new Intent(this,MyService.class);
startService(start);
break;
case R.id.stop_service:
Intent stop = new Intent(this,MyService.class);
stopService(stop);
break;
case R.id.bind_service:
Intent bind = new Intent(this,MyService.class);
bindService(bind,conn,BIND_AUTO_CREATE);
break;
//绑定与解绑都是针对于同一个连接而言
case R.id.unbind_service:
unbindService(conn);
break;
}
}
//定义一个内部类来实现bindService的参数
class Myconnection implements ServiceConnection {
/**
* 与服务器绑定时调用
* @param name
* @param service
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//进行赋值操作
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
总结
- service的开启有两种方法,onStartCommand,onbind.两者可以相互调用,
- onbind(创建并绑定)->onStartCommand(可以多次调用)—>unbing(解绑,但不会销毁)->stopService(销毁)
- 使用了onStartCommand开启服务,就不能用unbind进行销毁。用onStartCommand开启后,可以使用bind进行绑定。
- 使用了onbind开启服务,就不能用stopService进行销毁,只能使用unbind进行解绑后会自动调用Destry()。
*2. onbind方法只会调用一次,在调用unbind之前,都不能重新调用bind. - onStartCommand会进行多次调用,一点start_service按钮就会调用
- 3.startservice()启动的服务不会自动停止,直到调用stopservice(),或者stopself();
- bindservice()启动的服务会于绑定的活动一起死
- 4.两种开启模式的区别:
- startService():注重服务的开启
- bindService():注重于服务与活动的交互