运行 Android 每 20 秒后使用 AlarmManager 的服务不会在从应用列表中杀死该应用时重新启动

Running an Android Service after every 20 sec using AlarmManager doesn't restart on killing the app from the app list

我正在尝试 运行 一个 Android 服务,该服务 运行 每 20 秒在后台运行一次,并将用户的经纬度数据发送到服务器进行跟踪。当我启动我的应用程序时,它第一次起作用。现在,如果我单击主页按钮,它仍然在后台 运行s。但是,现在如果我使用主页按钮从应用程序列表中终止我的应用程序。并使用启动器图标重新启动我的应用程序。现在服务没有启动。我正在使用警报管理器每 20 秒触发一次我的服务。但是在重新启动时,我的警报已设置但未在广播接收器上注册,因此未调用我的服务。 以下是我的代码:- MyFragment.java 的 onCreateView() 我在其中设置我的闹钟:-

Intent alarm = new Intent(mContext, AlarmReceiver.class);
    boolean alarmRunning = (PendingIntent.getBroadcast(mContext, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
    if (alarmRunning == false) {
        Log.e("In OnCreateView DDFrag", "AlarmRunning == False");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, alarm, 0);
        AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 20000, pendingIntent);
    } else{
        Log.e("In OnCreateView DDFrag", "AlarmRunning == True");
    }

AlarmReceiver.class:-

    public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent background = new Intent(context, MyService.class);
        Log.e("AlarmReceiver", "Broadcasr Receiver started");
        context.startService(background);
    }
}

MyService.class:-

    public class MyService extends Service {

    public boolean isServiceRunning;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        this.isServiceRunning = false;
    }



    @Override
    public void onDestroy() {
        this.isServiceRunning = false;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(!this.isServiceRunning) {
            sendDataToServer();
            this.isServiceRunning = true;
        }
        return START_STICKY;
    }


    private void sendDataToServer() {
        // Performing my operation in this method..
    // On Success of the method performed I am calling the below method and setting the below variables:
    stopSelf();
        this.isServiceRunning = false;
    }
}

此外,我在 manifest.xml 文件 中将我的服务和接收器定义为:-

<service android:name="com.mypackagename.services.MyService" />

    <receiver android:name="com.mypackagename.services.AlarmReceiver" />

请帮我解决这个问题,或者指出我在做什么。 作为第一次。由于未设置我的警报管理器,它会设置并在适当的 20 秒后调用该服务。但是,如果我终止我的应用程序并再次启动它,那么我的警报将被设置,因此它不会启动或再次设置。现在我的 AlarmReceiver class 从未收到 Alarm BroadcastReceiver。

这里有一些事情:

请删除设置闹钟的 "alarmRunning" 代码。一直这样做。如果已经设置了闹钟,再次设置只会取消旧的闹钟并设置新的闹钟。这不是问题。

您不能依赖 PendingIntent 的存在来确定是否在警报管理器中设置了警报。这很可能是您在终止并重新启动您的应用程序后没有收到警报的原因。

此外,您无法使用 setRepeating() 可靠地安排每 20 秒发出一次警报。 Android 具有严格的电源管理规则,在大多数设备上不会可靠地触发少于 2 分钟的重复警报。您可能会在 2、3 或 5 分钟而不是 20 秒后看到此警报响起,具体取决于电源管理设置、电池电量、设备繁忙程度、是否处于睡眠状态等。

如果你真的想要每 20 秒 运行 的东西,那么你应该设置一个闹钟,当那个闹钟响起时,处理它并从现在开始设置下一个闹钟 20 秒。