首页 > 解决方案 > 在内存杀死或销毁活动时,服务和广播接收器在 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

帮助将不胜感激,在此先感谢。

标签: androidandroid-serviceandroid-broadcast

解决方案


在服务类中使用此方法

@Override 
    public void onTaskRemoved(Intent rootIntent){
        Intent broadcastIntent = new Intent(ipManager.INTENT_SERVICE_RESTART);
        sendBroadcast(broadcastIntent);
    }

推荐阅读