这一节,主要来复习一下Service(服务),它在Android中常常用于执行长时间的耗时操作,一般是在Service启动工作线程。它分两种类型,一种是启动模式service,另一种是绑定模式service,再来说一下它的生命周期吧,见下图:
看看启动和绑定Service的应用,以及IntentService的应用,如下:
MainActivity:
public classMainActivityextendsActivity implementsView.OnClickListener {
//布局文件里就四个按钮,就不贴了。 private Button start; private Button stop; private Button getInfo; private Button btnIntentService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.start_main); stop = (Button) findViewById(R.id.stop_main); getInfo = (Button) findViewById(R.id.info_main); btnIntentService = (Button) findViewById(R.id.intentService_main); start.setOnClickListener(this); stop.setOnClickListener(this); getInfo.setOnClickListener(this); btnIntentService.setOnClickListener(this); } private IMessage msg; private ServiceConnection con=new ServiceConnection(){ //此service绑定被异常终止时执行 @Override public void onServiceDisconnected(ComponentName componentName) { System.out.println("onServiceDisconnected"); } //service绑定OK以后执行 @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { System.out.println("onServiceConnected"); msg= (IMessage) iBinder; } }; @Override public void onClick(View view) { switch (view.getId()){ case R.id.start_main: // //1 启动模式的服务 // Intent intent = new Intent(MainActivity.this,MyStartService.class); // intent.putExtra("A","play"); // startService(intent); //2 绑定模式的服务 Intent intent = new Intent(MainActivity.this,MyBindService.class); bindService(intent,con,BIND_AUTO_CREATE); break; case R.id.stop_main: // //1 启动模式的服务 // Intent intent2 = new Intent(MainActivity.this,MyStartService.class); // intent2.putExtra("A","pause"); // startService(intent2); //2 绑定模式的服务 unbindService(con); break; //绑定模式下收消息 case R.id.info_main: System.out.println("收到消息:"+msg.obtain()); break; //intentservice的使用 case R.id.intentService_main: startService(new Intent(MainActivity.this,MyIntentService.class)); break; } } } 启动服务MyStartService: public class MyStartService extends Service { public MyStartService() { } @Override public IBinder onBind(Intent intent) { System.out.println("onBind"); return null; } @Override public void onCreate() { super.onCreate(); System.out.println("onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("onStartCommand"); String action = intent.getStringExtra("A"); if(action.equals("play")) { System.out.println("Music should be play"); } if(action.equals("pause")){ System.out.println("Music should be pause"); } return START_REDELIVER_INTENT; } @Override public void onDestroy() { super.onDestroy(); System.out.println("onDestroy"); } } 上面红色加粗部分涉及到服务粘性的知识,如下:服务的粘性:服务所在的进程被异常kill掉以后是否会自动启动。
1)START_STICKY(被异常kill掉以后,服务会重启,但不会重新传递intent) 2)START_NOT_STICKY(被异常kill掉以后, 服务部会重启); 3)START_REDELIVER_INTENT(被异常kill掉以后, 服务会重启,并且会重新传递intent)
绑定服务:
应用情形:希望在activity中,访问service的某个方法,并获得此方法的返回值
MyBindService:
public class MyBindService extends Service { public MyBindService() { } @Override public void onCreate() { super.onCreate(); System.out.println("onCreate"); } @Override public IBinder onBind(Intent intent) { System.out.println("onBind"); return new MsgBinder(); } @Override public void onRebind(Intent intent) { super.onRebind(intent); System.out.println("onRebind"); } @Override public boolean onUnbind(Intent intent) { System.out.println("onUnbind"); return true; } @Override public void onDestroy() { super.onDestroy(); System.out.println("onDestroy"); } class MsgBinder extends Binder implements IMessage{ @Override public String obtain() { return "msg001"; } } } IMessage: public interface IMessage { public String obtain(); } 意图服务IntentService的使用:IntentService是一个service,此service 在运行时会默认启动一个工作线程,可以将耗时操作写到此工作线程回调的方法中。此回调方法(onHandleIntent)执行结束以后会自动关闭service(调用了stopSelf).
MyIntentService:
public class MyIntentService extends IntentService { public MyIntentService() { super("MyIntentService"); System.out.println("11111111"); } public MyIntentService(String name) { super(name); } /**此方法运行在工作线程*/ @Override protected void onHandleIntent(Intent intent) { System.out.println("onHandleIntent"); String name = Thread.currentThread().getName(); System.out.println("name== "+name.toString()); } @Override public void onCreate() { super.onCreate(); System.out.println("onCreate"); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); System.out.println("onStart"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); System.out.println("onDestroy"); } @Override public IBinder onBind(Intent intent) { System.out.println("onBind"); return super.onBind(intent); } } 注意清单文件AndroidManifest.xml中: <service android:name=".MyStartService" android:enabled="true" android:exported="true" /> <service android:name=".MyBindService" android:enabled="true" android:exported="true" /> <service android:name=".MyIntentService" android:enabled="true" android:exported="true" />可能会涉及到进程: Android 系统中的进程:(优先级从上到下) 1)前台进程(有正在与用户交互的activity,或者有执行了startForceground的service) 2)可见进程(看得到activity,但没有焦点) 3)服务进程(没有可见activity,有正在运行的service) 4)后台进程(在后台有停止状态的activity,没有其他组件) 5)空进程 (没有任何组件在运行)这一节就先说这些。