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);
}
}
}
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);
}
}
}
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));
}
}
<service android:name=".services.ServiceA" />
<service
android:name=".services.ServiceB"
android:process=":remote" />
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有