记得使用AIDL定义接口完成进程间Service的通信
清单文件
<application>
<service
android:name=".service.RemoteService"
android:process="com.example.remote"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.myappliucation.REMOTE" />
</intent-filter>
</service>
<service
android:name=".service.JournalismService"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="com.example.myappliucation.JOURNALISM" />
</intent-filter>
</service>
<service android:name=".service.JournalismService$InnerService" />
</application>
Activity
Intent intent = new Intent("com.example.myappliucation.JOURNALISM");
intent.setPackage("com.example.journalism");
startService(intent);
Intent remote = new Intent("com.example.myappliucation.REMOTE");
remote.setPackage("com.example.journalism");
startService(remote);
服务
package com.example.formwork.service;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import androidx.annotation.Nullable;
import com.example.formwork.IMyAidlInterface;
import com.example.formwork.R;
public class JournalismService extends Service {
private static final String TAG = "JournalismService";
public JournalismService() {
}
private class JournalismBinder extends IMyAidlInterface.Stub {
@Override
public String getString() throws RemoteException {
return RemoteService.class.getSimpleName();
}
}
@Override
public IBinder onBind(Intent intent) {
return new JournalismService.JournalismBinder();
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
try {
Log.d( "JournalismService",""+iMyAidlInterface.getString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.d( "JournalismService","JournalismService被系统回收,回调函数被执行");
Intent intent = new Intent("com.example.myappliucation.REMOTE");
intent.setPackage("com.example.journalism");
startService(intent);
bindService(intent, serviceConnection, BIND_ABOVE_CLIENT);
}
};
@Override
public void onCreate() {
super.onCreate();
Intent intent = new Intent("com.example.myappliucation.REMOTE");
intent.setPackage("com.example.journalism");
startService(intent);
bindService(intent, serviceConnection, BIND_ABOVE_CLIENT);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
Log.i(TAG, "onStartCommand: " + intent.getStringExtra("test"));
} else {
Log.i(TAG, "onStartCommand: ");
}
super.onStartCommand(intent, flags, startId);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.night_mode);
builder.setContentTitle("今日头条正在运行...");
startForeground(22, builder.build());
Intent service = new Intent(this, InnerService.class);
startService(service);
return START_STICKY;
}
public static class InnerService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startForeground(22, new Notification());
stopSelf();
return START_STICKY;
}
}
}
保活服务
package com.example.formwork.service;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.example.formwork.IMyAidlInterface;
public class RemoteService extends Service {
public RemoteService() {
}
private class RemoteBinder extends IMyAidlInterface.Stub {
@Override
public String getString() throws RemoteException {
return RemoteService.class.getSimpleName();
}
}
@Override
public IBinder onBind(Intent intent) {
return new RemoteBinder();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
try {
Log.d( "RemoteService",""+iMyAidlInterface.getString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.d( "RemoteService","RemoteService被系统回收,回调函数被执行");
Intent intent = new Intent("com.example.myappliucation.JOURNALISM");
intent.setPackage("com.example.journalism");
startService(intent);
bindService(intent,serviceConnection , BIND_ABOVE_CLIENT);
}
};
@Override
public void onCreate() {
super.onCreate();
Intent intent = new Intent("com.example.myappliucation.JOURNALISM");
intent.setPackage("com.example.journalism");
startService(intent);
bindService(intent,serviceConnection , BIND_ABOVE_CLIENT);
}
}
adb 命令
adb -s emulator-5554 shell ps //查询 emulator-5554 这个设备所有的进程
adb -s emulator-5554 shell kill 6875 //查询 emulator-5554 这个设备 6875 进程
adb devices //查询所有设备