我正在创建一个 RestartServiceBroadcast 以在终止应用程序后让我的后台服务始终保持 Activity 状态。
public class RestartServiceBroadcast extends BroadcastReceiver {
Context ctx;
private PreferencesProviderWrapper prefProviderWrapper;
@Override
public void onReceive(Context context, Intent intent) {
this.ctx= context;
System.out.println("RestartServiceBroadcast:");
if(intent.getAction().equals(ipManager.INTENT_SERVICE_RESTART)){
startOneService();
}
}
private void startOneService() {
}
}
在这里,我还创建了一个连接服务器 IP 的服务。在其中我还触发了广播以重新启动相同的服务。我也在服务中使用 START_STICKEY
public class IpService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("ipStack : onDestroy");
Intent broadcastIntent = new Intent(ipManager.INTENT_SERVICE_RESTART);
sendBroadcast(broadcastIntent);
}
}
我在 mainActivity 中调用 Broadcast 的 onDestroy() 方法。
override fun onDestroy() {
val broadcastIntent = Intent(ipManager.INTENT_SERVICE_RESTART)
sendBroadcast(broadcastIntent)
super.onDestroy()
}
这是我的 list
<service
android:name=".service.ipService"
android:enabled="true"
android:exported="false"
android:permission="demo.magic.mobile.android.permission.CONFIGURE_IP"
android:process=":ipStack"
android:stopWithTask="false">
<intent-filter>
<action android:name="demo.magic.mobile.service.ipService" />
<action android:name="demo.magic.mobile.service.ipConfiguration" />
</intent-filter>
</service>
<receiver
android:enabled="true"
android:exported="true"
android:name=".service.receiver.RestartServiceBroadcast"
android:process=":ipStack">
<intent-filter>
<action android:name="demo.magic.mobile.service.RestartService" />
</intent-filter>
</receiver>
用于获取 Broadcast 值的 ipManager 类
public final class ipManager {
public static final String INTENT_SIP_SERVICE_RESTART = "demo.magic.mobile.service.RestartService";
}
所有代码在我的 motoG5 和 MI Note5 Pro 设备上运行良好。但在 MI note4 和 MI4 设备中,服务和广播在从后台删除应用程序后已被终止。
MotoG5等设备Logcat我杀应用的时候是这样
2019-04-25 15:27:00.220 2345-3735/? W/ActivityManager: Scheduling restart of crashed service demo.magic.mobile/.service.ipService in 20942ms
2019-04-25 15:27:21.192 2345-2410/? I/ActivityManager: Start proc 23954:demo.magic.mobile:ipStack/u0a247 for service demo.magic.mobile/.service.ipService
2019-04-25 15:27:21.490 23954-23954/demo.magic.mobile:ipStack I/System.out: ipStack : onstart
小米Note4 logcat kill应用进程
2019-04-25 15:26:04.584 1604-2918/? I/ActivityManager: Start proc 10971:demo.magic.mobile:ipStack/u0a643 for service demo.magic.mobile/.service.ipService caller=demo.magic.mobile
2019-04-25 15:36:04.216 1604-2785/? I/ActivityManager: Start proc 19393:demo.magic.mobile:ipStack/u0a643 for service dailer.demo.magic.mobile/.service.ipService caller=demo.magic.mobile
帮助将不胜感激,提前致谢。
最佳答案
在服务类中使用该方法
@Override
public void onTaskRemoved(Intent rootIntent){
Intent broadcastIntent = new Intent(ipManager.INTENT_SERVICE_RESTART);
sendBroadcast(broadcastIntent);
}
关于android - 在 Memory kill 或 Destroy Activity 时服务和 BroadcastReceiver 在 MI 手机中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55847610/