android - 在内存杀死或销毁活动时,服务和广播接收器在 MI 手机中不起作用
问题描述
我正在创建一个 RestartServiceBroadcast 以在终止应用程序后保持我的后台服务始终处于活动状态。
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 的 onDestroy() 方法中调用 Broadcast。
override fun onDestroy() {
val broadcastIntent = Intent(ipManager.INTENT_SERVICE_RESTART)
sendBroadcast(broadcastIntent)
super.onDestroy()
}
这是我的清单
<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>
类 ipManager 用于获取 Broadcast 的值
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
MI Note4之logcat杀死应用进程
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);
}
推荐阅读
- python - @property 是否可以用于返回一个值(如果它存在),但如果它不存在则设置该值?
- sql - 在颤动中将图像文件转换为 VARBINARY(max)
- php - 带引导程序的不规则布局
- android - 更新 AppCompat 版本后 OnActivityResult() 结果中的请求代码不正确
- python - Panda3d globalClock.getDt() 函数不起作用,我需要导入一些东西吗?
- python - 散景仪表板中的数据表;带有滑块小部件的反应式过滤
- css - CSS复选框问题应该在我检查它时选择
- coldfusion - 来自 Docker 映像的 Coldfusion LDAP
- excel - Excel VBA - 保存用户表单更改
- scala - for循环查询中的外连接?