編輯:關於Android編程
兩個Service之間相互監視的實現
在實際開發中可能需要用到兩個Service相互監視的情況,本示例就是實現此功能以作參考。
服務A:
public class ServiceA extends Service { private static final String TAG = ServiceA.class.getSimpleName(); MyBinder mBinder; MyServiceConnection mServiceConnection; PendingIntent mPendingIntent; @Override public void onCreate() { super.onCreate(); if(mBinder==null) { mBinder=new MyBinder(); } mServiceConnection=new MyServiceConnection(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { ServiceA.this.bindService(new Intent(ServiceA.this,ServiceB.class),mServiceConnection, Context.BIND_IMPORTANT); mPendingIntent=PendingIntent.getService(this,0,intent,0); Notification.Builder builder=new Notification.Builder(this); builder.setTicker("守護服務A啟動中") .setContentText("我是來守護服務B的") .setContentTitle("守護服務A") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(mPendingIntent) .setWhen(System.currentTimeMillis()); Notification notification=builder.build(); startForeground(startId,notification); return START_STICKY; } @Override public IBinder onBind(Intent intent) { return mBinder; } public class MyBinder extends IBridgeInterface.Stub { @Override public String getName() throws RemoteException { return "name:"+TAG; } } class MyServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { String name=null; try { name= IBridgeInterface.Stub.asInterface(iBinder).getName(); } catch (RemoteException e) { e.printStackTrace(); } Toast.makeText(ServiceA.this,name+"連接成功",Toast.LENGTH_SHORT).show(); } @Override public void onServiceDisconnected(ComponentName componentName) { Toast.makeText(ServiceA.this,TAG+"斷開連接",Toast.LENGTH_SHORT).show(); ServiceA.this.startService(new Intent(ServiceA.this,ServiceB.class)); ServiceA.this.bindService(new Intent(ServiceA.this,ServiceB.class),mServiceConnection, Context.BIND_IMPORTANT); } } }
服務B:
public class ServiceB extends Service { private static final String TAG = ServiceB.class.getSimpleName(); private PendingIntent mPendingIntent; private MyBinder mBinder; private MyServiceConnection mServiceConnection; @Override public IBinder onBind(Intent intent) { return mBinder; } @Override public void onCreate() { super.onCreate(); if (mBinder == null) { mBinder = new MyBinder(); } mServiceConnection = new MyServiceConnection(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { this.bindService(new Intent(ServiceB.this, ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT); mPendingIntent = PendingIntent.getService(this, 0, intent, 0); Notification.Builder builder = new Notification.Builder(this); builder.setTicker("守護服務B啟動中") .setContentText("我是來守護服務A的") .setContentTitle("守護服務B") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(mPendingIntent) .setWhen(System.currentTimeMillis()); Notification notification = builder.build(); startForeground(startId, notification); return START_STICKY; } public class MyBinder extends IBridgeInterface.Stub { @Override public String getName() throws RemoteException { return "name:"+TAG; } } class MyServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { String name=null; try { name=IBridgeInterface.Stub.asInterface(iBinder).getName(); } catch (RemoteException e) { e.printStackTrace(); } Toast.makeText(ServiceB.this, name + "連接成功", Toast.LENGTH_SHORT).show(); } @Override public void onServiceDisconnected(ComponentName componentName) { Toast.makeText(ServiceB.this, TAG + "斷開連接", Toast.LENGTH_SHORT).show(); ServiceB.this.startService(new Intent(ServiceB.this, ServiceA.class)); ServiceB.this.bindService(new Intent(ServiceB.this, ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT); } } }
IBridgeInterface.aidl
1 interface IBridgeInterface { 2 String getName(); 3 }
界面:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(this, ServiceA.class)); startService(new Intent(this, ServiceB.class)); } }
AndroidManifest.xml
<service android:name=".services.ServiceA" /> <service android:name=".services.ServiceB" android:process=":remote" />
由於涉及到跨進程,onServiceConnected() 方法中使用
IBridgeInterface.Stub.asInterface(iBinder).getName();
而不能直接類型轉換
((ServiceA.MyBinder)iBinder).getName();
onStartCommand
onStartCommand() 方法必須返回整型數。整型數是一個值,用於描述系統應該如何在服務終止的情況下繼續運行服務。
返回的值必須是以下常量之一:
START_NOT_STICKY
如果系統在 onStartCommand() 返回後終止服務,則除非有掛起 Intent 要傳遞,否則系統不會重建服務。
START_STICKY
如果系統在 onStartCommand() 返回後終止服務,則會重建服務並調用 onStartCommand(),但絕對不會重新傳遞最後一個 Intent。相反,除非有掛起 Intent 要啟動服務(在這種情況下,將傳遞這些 Intent ),否則系統會通過空 Intent 調用 onStartCommand()。這適用於不執行命令、但無限期運行並等待作業的媒體播放器(或類似服務)。
START_REDELIVER_INTENT
如果系統在 onStartCommand() 返回後終止服務,則會重建服務,並通過傳遞給服務的最後一個 Intent 調用 onStartCommand()。任何掛起 Intent 均依次傳遞。這適用於主動執行應該立即恢復的作業(例如下載文件)的服務。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
安卓開發的四大組件是Activity, service, broadcast receiver, 和content provider。作為業余的開發者,可能不需要太深入理
1、前言 等級信號狀態的View在現在的Android系統中非常的常見,比如手機右上角的電池狀態的圖標就非常的經典,有幾種狀態,到了快沒電的時候有些還會閃爍提示用戶充電;
在Android開發中,我們常用的布局方式主要有LinearLayout、RelativeLayout、FrameLayout等,通過這些布局我們可以實現各種各樣的界面。
想起前段時間的物聯網的外包開發,經常遇到通過wifi接受的數據,要通過轉換成十六進制字符串,或者最後又是十進制數據。都是根據雙方的協議來開發的。那麼我發送過去的數據也需要