Android——Activity以及Service的启动方式
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文件和其他代码需要自己补全一下