標籤:android
基本概念:
service是android四大組件之一,運行在後台執行耗時操作,並不提供使用者介面。其他組件如acticity可以通過startService啟動該組件,也可以通過bindService啟動並把綁定該組件進行通訊。
使用情境
後台下載檔案,以及播放音樂等
注意
service運行在主線程中,他不會建立屬於自己的線程,也不是運行在獨立的線程中,所以在使用的時候,需要自己建立線程,而不應該直接使用,這樣會造成ANR錯誤。
service的兩種形式
started service
其他組件如activity等通過stratService等啟動該組件,擁有獨立的生命週期,不依賴啟動他的組件。
bound service
其他組件為了與service建立一個長時間的串連通過bindService來建立串連。並能與之互動(發送請求,接受響應)。它的生命週期依賴綁定他的組件,一但解除綁定就會消亡。
兩個service的代碼:
package peng.liu.testview;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service { public MyService() { } @Override public void onCreate() { super.onCreate(); ServiceClass sc = new ServiceClass(); sc.start(); } private class ServiceClass extends Thread{ @Override public void run() { super.run(); for (int i = 0;i< 1000;i++){ System.out.println("hello"+i); try { Thread.sleep(1000); }catch (Exception e){ e.printStackTrace(); } } } } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); }}
另外一個service是
package peng.liu.testview;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import java.util.Random;public class MyService2 extends Service { public MyService2() { } //第一步實現binder子類 public class LiuBinder extends Binder { //建立一個公用方法,返回service執行個體 public MyService2 getService(){ return MyService2.this; } } //onBind方法返回一個LiuBinder private LiuBinder binder = new LiuBinder(); @Override public IBinder onBind(Intent intent) { return binder; } //建立自訂的業務方法,必須是公用的 public int getRandom(){ Random random = new Random(); return random.nextInt(100); } @Override public void onDestroy() { super.onDestroy(); System.out.println("service done"); }}
主類的代碼:
package peng.liu.testview;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.IntentFilter;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity implements View.OnClickListener{ private Button send,reg,unReg; private MyService2 service; boolean isBound; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.send).setOnClickListener(this); findViewById(R.id.reg).setOnClickListener(this); findViewById(R.id.unReg).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.send: startService(new Intent(MainActivity.this,MyService.class)); break; case R.id.reg: Intent intent = new Intent(MainActivity.this,MyService2.class); bindService(intent,conn,BIND_AUTO_CREATE); } Toast.makeText(MainActivity.this,"隨機數是"+service.getRandom(),Toast.LENGTH_LONG).show(); } @Override protected void onDestroy() { super.onDestroy(); unbindService(conn); } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { MyService2.LiuBinder binder = (MyService2.LiuBinder)iBinder; service = binder.getService(); isBound = true; } @Override public void onServiceDisconnected(ComponentName componentName) { isBound = false; } };}
註冊service代碼:
<service android:name=".MyService" android:enabled="true" android:exported="true" > </service> <service android:name=".MyService2" android:enabled="true" android:exported="true" > </service>
Android四大組件之一Service介紹-android學習之旅(十二)