一、Android的五个进程:
- ActivityProcess: 前台进程,Activity
- VisibleProcess:可见进程,进程拥有暂停状态的Activity
- StartedServiceProcess:服务进程,包含正在运行的Service
- BackgroundProcess:后台进行,处于停止状态的Activity
- EmptyProcess:被关闭的Activity:空进程,系统会保留启动Activity信息,残留信息;
二、启动Serivice的生命周期:
onCreate()-->onStartCommand()-->onStart()(2.0以后已经被onStartCommand()的代替)-->onDestroy()
效果图:
三、绑定Service的生命周期:
onCreate()-->onBind()-->onUnBind()-->onDestroy()
效果图:
四、启动绑定service生命周期:
1、先启动service,再 绑定service,再解绑service
onCreate()-->onStartCommand()-->onBind()-->onUnbind()
效果图:
2、将启动的service,绑定之后,如果想销毁service,必须先解绑才能停止service
onCreate()-->onStartCommand-->onBind()-->onUnbind-->onDestroy()
效果图:
3、先启动,再绑定,解绑,再次绑定,将onUnbind()方法返回true,才能执行onRebind()方法
@Override
public boolean onUnbind(Intent intent) {
Log.i("dayang","-------------onUnbind---------------");
return true;
}
onCreate()-->onStartCommand()-->onBind()-->onUnbind()-->onRebind()-->onUnbind()-->onDestroy()
效果图:
五、Activity与绑定的Service的通信:
通过ServiceConnection接口和IBinder接口实现通信;
- 在Activity中绑定Service时,会创建ServiceConnection接口对象,重写onServiceConnected()方法,该方法有Service绑定成功时onBind()方法返回的IBinder接口对象;
- 在Service的执行的onBind()方法返回IBinder接口对象;
对于Ibinder接口Android给出了实现类Binder,我们只需要继承这个Binder类,就可以实现IBinder接口并写自己的逻辑代码
Activity 通过ServiceConnection接口取得与之绑定Service返回的IBinder接口对象
MyService的代码
public class MyService extends Service {
public class MyBinder extends Binder{
int count=0;
public int getCount() {
return ++count;
}
}
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
Log.i("dayang","-------------onBind-----------------");
return new MyBinder();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("dayang","-------------onStartCommand---------------");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Log.i("dayang","-------------onRebind---------------");
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("dayang","-------------onUnbind---------------");
return true;
}
@Override
public void onCreate() {
super.onCreate();
Log.i("dayang","-------------onCreate---------------");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("dayang","-------------onDestroy---------------");
}
}
绑定MyService的Actvity的代码
public class MainActivity extends AppCompatActivity {
MyService.MyBinder myBinder=null;
ServiceConnection mConn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
myBinder= (MyService.MyBinder) iBinder;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startActivity(View view){
Intent intent=new Intent(this,MyService.class);
startService(intent);
}
public void stopActivity(View view){
Intent intent=new Intent(this,MyService.class);
stopService(intent);
}
public void bindService(View view){
if(myBinder==null){
Intent intent=new Intent(this,MyService.class);
bindService(intent,mConn, Context.BIND_AUTO_CREATE);
}else{
Log.i("dayang",myBinder.getCount()+"得到次数-----");
}
}
public void unBindSerivce(View view){
Intent intent=new Intent(this,MyService.class);
unbindService(mConn);
}
}
绑定MyService的Actvity的布局文件
六、在Service中启动Activity
因为Service运行时没有任务栈,要为开启的Activity指定一个新的任务栈:
1、在Activity中,启动一个Service并发送消息
Intent intent=new Intent(this,MyService.class);
intent.putExtra("flag",true);
startService(intent);
2、启动Service之后,执行onStartCommand(),在该方法开启一个新的Activity
给新的Activity一个任务栈
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
boolean flag=intent.getBooleanExtra("flag",false);
if(flag){
Intent intent2=new Intent(this,Main2Activity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent2);
}
return super.onStartCommand(intent, flags, startId);
}