涉及到服务通信,使用AIDL。
MyProcess.aidl:
package com.study.dn_process.inter;
interface MyProcess{
String getProcessName();
}
LocalService:
package com.study.dn_process.service;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.study.dn_process.R;
import com.study.dn_process.inter.MyProcess;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class LocalService extends Service {
private MyBinder binder;
private MyServiceConnection conn;
private PendingIntent pendingIntent;
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
super.onCreate();
if (binder == null) {
binder = new MyBinder();
}
conn = new MyServiceConnection();
}
@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// LocalService.this.startService(new Intent(LocalService.this,
// RemoteService.class));
// 绑定远程服务
this.bindService(new Intent(LocalService.this, RemoteService.class),
conn, Context.BIND_IMPORTANT);
Notification notification = new Notification(R.drawable.ic_launcher,
"服务启动中", System.currentTimeMillis());
pendingIntent = PendingIntent.getService(this, 0, intent, 0);
notification.setLatestEventInfo(this, "服务", "---", pendingIntent);
return START_STICKY;
}
class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("info", "ServiceConnected");
}
@SuppressLint("InlinedApi")
@Override
public void onServiceDisconnected(ComponentName name) {
// 重新连接远程服务
LocalService.this.startService(new Intent(LocalService.this,
RemoteService.class));
LocalService.this.bindService(new Intent(LocalService.this,
RemoteService.class), conn, Context.BIND_IMPORTANT);
}
}
class MyBinder extends MyProcess.Stub {
@Override
public String getProcessName() throws RemoteException {
return "LocalService";
}
}
}
RemoteService:
package com.study.dn_process.service;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.study.dn_process.R;
import com.study.dn_process.inter.MyProcess;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class RemoteService extends Service {
private MyBinder binder;
private MyServiceConnection conn;
private PendingIntent pendingIntent;
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
super.onCreate();
if (binder == null) {
binder = new MyBinder();
}
conn = new MyServiceConnection();
}
@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// RemoteService.this.startService(new Intent(RemoteService.this,
// LocalService.class));
this.bindService(new Intent(RemoteService.this, LocalService.class),
conn, Context.BIND_IMPORTANT);
Notification notification = new Notification(R.drawable.ic_launcher,
"服务启动中", System.currentTimeMillis());
pendingIntent = PendingIntent.getService(this, 0, intent, 0);
notification.setLatestEventInfo(this, "服务", "---", pendingIntent);
return START_STICKY;
}
class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("info", "ServiceConnected");
}
@SuppressLint("InlinedApi")
@Override
public void onServiceDisconnected(ComponentName name) {
RemoteService.this.startService(new Intent(RemoteService.this,
LocalService.class));
RemoteService.this.bindService(new Intent(RemoteService.this,
LocalService.class), conn, Context.BIND_IMPORTANT);
}
}
class MyBinder extends MyProcess.Stub {
@Override
public String getProcessName() throws RemoteException {
return "RemoteService";
}
}
}
MainActivity:
package com.study.dn_process;
import com.study.dn_process.service.LocalService;
import com.study.dn_process.service.RemoteService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.startService(new Intent(this, LocalService.class));
this.startService(new Intent(this, RemoteService.class));
}
}
tip:清单文件需要注册!