一、Android的五個進程:
- ActivityProcess: 前臺進程跛蛋,Activity
- VisibleProcess:可見進程,進程擁有暫停狀態(tài)的Activity
- StartedServiceProcess:服務(wù)進程拣播,包含正在運行的Service
- BackgroundProcess:后臺進行途茫,處于停止?fàn)顟B(tài)的Activity
- EmptyProcess:被關(guān)閉的Activity:空進程,系統(tǒng)會保留啟動Activity信息的圆,殘留信息鼓拧;
二、啟動Serivice的生命周期:
onCreate()-->onStartCommand()-->onStart()(2.0以后已經(jīng)被onStartCommand()的代替)-->onDestroy()
效果圖:
啟動service的生命周期.jpg
三越妈、綁定Service的生命周期:
onCreate()-->onBind()-->onUnBind()-->onDestroy()
效果圖:
綁定service生命周期.jpg
四季俩、啟動綁定service生命周期:
1、先啟動service梅掠,再 綁定service酌住,再解綁service
onCreate()-->onStartCommand()-->onBind()-->onUnbind()
效果圖:
先啟動再綁定最后解綁.jpg
2、將啟動的service阎抒,綁定之后酪我,如果想銷毀service,必須先解綁才能停止service
onCreate()-->onStartCommand-->onBind()-->onUnbind-->onDestroy()
效果圖:
銷毀先啟動再綁定的Service.jpg
3且叁、先啟動都哭,再綁定,解綁逞带,再次綁定质涛,將onUnbind()方法返回true,才能執(zhí)行onRebind()方法
@Override
public boolean onUnbind(Intent intent) {
Log.i("dayang","-------------onUnbind---------------");
return true;
}
onCreate()-->onStartCommand()-->onBind()-->onUnbind()-->onRebind()-->onUnbind()-->onDestroy()
效果圖:
解綁之后再次綁定.jpg
五掰担、Activity與綁定的Service的通信:
通過ServiceConnection接口和IBinder接口實現(xiàn)通信汇陆;
- 在Activity中綁定Service時,會創(chuàng)建ServiceConnection接口對象带饱,重寫onServiceConnected()方法毡代,該方法有Service綁定成功時onBind()方法返回的IBinder接口對象阅羹;
- 在Service的執(zhí)行的onBind()方法返回IBinder接口對象;
對于Ibinder接口Android給出了實現(xiàn)類Binder教寂,我們只需要繼承這個Binder類捏鱼,就可以實現(xiàn)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()+"得到次數(shù)-----");
}
}
public void unBindSerivce(View view){
Intent intent=new Intent(this,MyService.class);
unbindService(mConn);
}
}
綁定MyService的Actvity的布局文件
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.ucai.day17_12_20_service.MainActivity">
<Button
android:onClick="startActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="startActivty" />
<Button
android:onClick="stopActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stopActivity"
/>
<Button
android:onClick="bindService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="bindService"
/>
<Button
android:onClick="unBindSerivce"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="unBindService"
/>
</LinearLayout>
六、在Service中啟動Activity
因為Service運行時沒有任務(wù)棧酪耕,要為開啟的Activity指定一個新的任務(wù)棧:
1导梆、在Activity中,啟動一個Service并發(fā)送消息
Intent intent=new Intent(this,MyService.class);
intent.putExtra("flag",true);
startService(intent);
2迂烁、啟動Service之后看尼,執(zhí)行onStartCommand(),在該方法開啟一個新的Activity
給新的Activity一個任務(wù)棧
@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);
}