1. Activity共有四種啟動方式(android:launchMode)
(1) standard:默認方式,不用再次配置
standart模式就是一個棧的模式,默認每次跳轉到一個新的Activity都會新建一個實例,不管這個實例是否存在。每次跳轉就新建,壓棧,每次返回就pop彈棧。
(2) singleTop:為<activity>指定屬性android:launchMode="singleTop",棧頂重復使用模式。
每次判斷棧頂有沒有,沒有,創建;有,繼續使用。如果發現有對應的Activity在棧頂,則會重復使用,不會創建新的。
(3) singleTask:為<activity>指定屬性android:launchMode="singleTask",唯一模式。
如上圖,如果從SecondActivity instance跳轉到FirstActivity,則SecondActivity彈棧即可,不用新建
特點:每一個Activity的實例都是唯一的,只要棧中存在,就會將他上面的所有Activity彈出。保證了每個activity實例的唯一性
(4) singleInstance:一種比較獨特的模式,每一個activity都會獨占一個棧,這個棧只允許該activity的實例進棧
2. Service共有兩種啟動方式
(1) Context.startService() 方式啟動:onCreate()——onStartCommand(onStart() 過時)——onDestory()
一旦服務被開啟,服務就跟調用者沒有什么關系了。開啟者無論推出還是掛了,服務器都會在后台繼續進行。
package com.example.ld.testapplication; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class MyService extends Service { private static final String TAG = "MyService"; public MyService() { } @Override public int onStartCommand(Intent intent, int flags, int startId){ Log.d(TAG, "this is onStartCommand()"); return super.onStartCommand(intent,flags,startId); } public void onDestory(){ super.onDestroy(); Log.d(TAG, "this is OnDestory()"); } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. return null; //throw new UnsupportedOperationException("Not yet implemented"); } }
start = (Button)findViewById(R.id.start); start.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(SecondActivity.this,MyService.class); //intent.putExtra("name","secondActivity"); startService(intent); } }); stop = (Button)findViewById(R.id.stop); stop.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(SecondActivity.this,MyService.class); //intent.putExtra("name","secondActivity"); stopService(intent); } });
(2) Context.bindService() 方式啟動:onCreate()——onBind()——onunbind()——onDestory()
bind方式開啟服務,服務綁定,調用者掛了,服務器也會跟着掛掉,綁定者可以調用服務里面的方法。
package com.example.ld.testapplication; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; public class BindService extends Service { public class MyBinder extends Binder { public BindService getService1(){ return BindService.this; } } private static final String TAG = "BindService"; private MyBinder myBinder = new MyBinder(); public BindService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. Log.i(TAG, "this is onBind"); return myBinder; } public void onCreate(){ Log.i(TAG,"this is onCreate"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId){ Log.i(TAG, "this is onStartCommand()"); return super.onStartCommand(intent,flags,startId); } public void onDestory(){ super.onDestroy(); Log.i(TAG, "this is OnDestory()"); } public boolean onUnbind(Intent intent){ Log.i(TAG,"this is onUnbind"); return super.onUnbind(intent); } }
start2 = (Button)findViewById(R.id.start2); start2.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(SecondActivity.this,MyService.class); //intent.putExtra("name","secondActivity"); bindService(intent,conn, Context.BIND_AUTO_CREATE); } }); stop2 = (Button)findViewById(R.id.stop2); stop2.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub if(flag ==true){ unbindService(conn); flag = false; } } }); private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub Log.i(TAG, "this is onServiceDisconnected()"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub BindService.MyBinder binder = (BindService.MyBinder)service; BindService bindService = binder.getService1(); flag = true; } };
這里主要截取了一些主要的代碼,具體的xml文件和其他代碼需要自己補全一下