Android 兩個(gè)Service的相互監(jiān)視實(shí)現(xiàn)代碼
兩個(gè)Service之間相互監(jiān)視的實(shí)現(xiàn)
在實(shí)際開發(fā)中可能需要用到兩個(gè)Service相互監(jiān)視的情況,本示例就是實(shí)現(xiàn)此功能以作參考。
服務(wù)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("守護(hù)服務(wù)A啟動(dòng)中")
.setContentText("我是來(lái)守護(hù)服務(wù)B的")
.setContentTitle("守護(hù)服務(wù)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);
}
}
}
服務(wù)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("守護(hù)服務(wù)B啟動(dòng)中")
.setContentText("我是來(lái)守護(hù)服務(wù)A的")
.setContentTitle("守護(hù)服務(wù)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" />
由于涉及到跨進(jìn)程,onServiceConnected() 方法中使用
IBridgeInterface.Stub.asInterface(iBinder).getName();
而不能直接類型轉(zhuǎn)換
((ServiceA.MyBinder)iBinder).getName();
onStartCommand
onStartCommand() 方法必須返回整型數(shù)。整型數(shù)是一個(gè)值,用于描述系統(tǒng)應(yīng)該如何在服務(wù)終止的情況下繼續(xù)運(yùn)行服務(wù)。
返回的值必須是以下常量之一:
START_NOT_STICKY
如果系統(tǒng)在 onStartCommand() 返回后終止服務(wù),則除非有掛起 Intent 要傳遞,否則系統(tǒng)不會(huì)重建服務(wù)。
START_STICKY
如果系統(tǒng)在 onStartCommand() 返回后終止服務(wù),則會(huì)重建服務(wù)并調(diào)用 onStartCommand(),但絕對(duì)不會(huì)重新傳遞最后一個(gè) Intent。相反,除非有掛起 Intent 要啟動(dòng)服務(wù)(在這種情況下,將傳遞這些 Intent ),否則系統(tǒng)會(huì)通過(guò)空 Intent 調(diào)用 onStartCommand()。這適用于不執(zhí)行命令、但無(wú)限期運(yùn)行并等待作業(yè)的媒體播放器(或類似服務(wù))。
START_REDELIVER_INTENT
如果系統(tǒng)在 onStartCommand() 返回后終止服務(wù),則會(huì)重建服務(wù),并通過(guò)傳遞給服務(wù)的最后一個(gè) Intent 調(diào)用 onStartCommand()。任何掛起 Intent 均依次傳遞。這適用于主動(dòng)執(zhí)行應(yīng)該立即恢復(fù)的作業(yè)(例如下載文件)的服務(wù)。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android中Service與Activity之間通信的幾種方式
- Android實(shí)現(xiàn)微信自動(dòng)向附近的人打招呼(AccessibilityService)
- Android AccessibilityService實(shí)現(xiàn)微信搶紅包插件
- Android Service服務(wù)不被停止詳解及實(shí)現(xiàn)
- Android Service中使用Toast無(wú)法正常顯示問(wèn)題的解決方法
- Android基于service實(shí)現(xiàn)音樂(lè)的后臺(tái)播放功能示例
- Android Activity 與Service進(jìn)行數(shù)據(jù)交互詳解
- 淺談Android Activity與Service的交互方式
- Android Service中方法使用詳細(xì)介紹
- Android 如何保證service在后臺(tái)不被kill
相關(guān)文章
Android通過(guò)自定義view實(shí)現(xiàn)刮刮樂(lè)效果詳解
這篇文章主要介紹了如何在Android中利用自定義的view實(shí)現(xiàn)刮刮樂(lè)的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟上小編一起動(dòng)手試一試2022-03-03
Android開發(fā)之設(shè)置開機(jī)自動(dòng)啟動(dòng)的幾種方法
這篇文章主要介紹了Android開發(fā)之設(shè)置開機(jī)自動(dòng)啟動(dòng)的幾種方法的相關(guān)資料,這里提供三種方法幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
android實(shí)現(xiàn)簡(jiǎn)單的活動(dòng)轉(zhuǎn)盤
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡(jiǎn)單的活動(dòng)轉(zhuǎn)盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Android設(shè)置TextView首行縮進(jìn)示例代碼
使用過(guò)word的都會(huì)知道,在文字排版的時(shí)候經(jīng)常要設(shè)置首行縮進(jìn),這樣才會(huì)使排版更整齊,那么在Android中當(dāng)需要設(shè)置首行縮進(jìn)的時(shí)候該腫么辦呢,下面一起來(lái)看看。2016-08-08
Android Jetpack導(dǎo)航組件Navigation創(chuàng)建使用詳解
這篇文章主要為大家介紹了Android Jetpack導(dǎo)航組件Navigation創(chuàng)建及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android自動(dòng)填充短信驗(yàn)證碼功能(demo)
在項(xiàng)目開發(fā)中為了給用戶帶來(lái)極好的體驗(yàn)效果,通常需要實(shí)現(xiàn)驗(yàn)證碼的自動(dòng)填充功能,怎么實(shí)現(xiàn)呢?今天小編給大家分享Android自動(dòng)填充短信驗(yàn)證碼功能的實(shí)現(xiàn)方法,需要的朋友參考下吧2017-02-02
android 仿微信demo——微信消息界面實(shí)現(xiàn)(服務(wù)端)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)功能示例
本篇文章主要介紹了Android實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)功能示例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-03-03

