当从KITKAT的最近应用程序列表中清除时,Android警报被取消

当从KITKAT的最近应用程序列表中清除时,Android警报被取消,android,alarmmanager,Android,Alarmmanager,我的应用程序以KITKAT及以上android版本为目标,我正试图通过Alarm Manager每天创建重复通知。下面是我的代码: public class AlarmReceiver extends BroadcastReceiver { public static final String MY_ACTION = "mymasterpeice.foreverservice.myaction"; @Override public void onReceive(Cont

我的应用程序以KITKAT及以上android版本为目标,我正试图通过Alarm Manager每天创建重复通知。下面是我的代码:

public class AlarmReceiver extends BroadcastReceiver {
    public static final String MY_ACTION = "mymasterpeice.foreverservice.myaction";

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(MY_ACTION)) {
            // Do something here
            // For our recurring task, we'll just display a message
            Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
            // displayNotification(context, "Checkout New Articles");
            sendNotification(context, "Checkout New Articles - Team");

        }


    }

    /**
     * Issues a notification to inform the user that server has sent a message.
     */
    private static void sendNotification(Context context, String message) {
        if (message != null && !TextUtils.isEmpty(message)) {
            int icon = R.mipmap.ic_launcher;
            long when = System.currentTimeMillis();
            NotificationManager notificationManager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            String title = context.getString(R.string.app_name);
            Intent notificationIntent = new Intent(context, MainActivity.class);
            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent intent =
                    PendingIntent.getActivity(context, 0, notificationIntent, 0);
            Notification.Builder builder = new Notification.Builder(context);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
            builder.setSmallIcon(icon)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setContentIntent(intent);
            Notification notification = builder.getNotification();
            notificationManager.notify(icon, notification);
        }
    }


}
主要活动:

public class MainActivity extends AppCompatActivity {

    private String TAG=MainActivity.class.getSimpleName();
    public static final String MY_ACTION = "mymasterpeice.foreverservice.myaction";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRecusrringTimer();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void setRecusrringTimer() {
        Intent myIntent = new Intent(MY_ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                this, 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        Calendar firingCal = Calendar.getInstance();
        Calendar currentCal = Calendar.getInstance();
        firingCal.set(Calendar.HOUR, 7); // At the hour you wanna fire
        firingCal.set(Calendar.MINUTE, 0); // Particular minute
        firingCal.set(Calendar.SECOND, 0); // particular second
        long intendedTime = firingCal.getTimeInMillis();
        long currentTime = currentCal.getTimeInMillis();
        if (intendedTime >= currentTime) // you can add buffer time too here to ignore some small differences in milliseconds
        {
            alarmManager.setRepeating(AlarmManager.RTC,
                    intendedTime, AlarmManager.INTERVAL_DAY,
                    pendingIntent);
            Log.d(TAG, "setRecusrringTimer ");
        } else {
            intendedTime = firingCal.getTimeInMillis();
            Log.d(TAG, "setRecusrringTimer ");
            alarmManager.setRepeating(AlarmManager.RTC,
                    intendedTime, AlarmManager.INTERVAL_DAY,
                    pendingIntent);

        }
    }

}
舱单:

    <receiver android:name=".AlarmReceiver">
        <intent-filter>
            <action android:name="mymasterpeice.foreverservice.myaction"/>
        </intent-filter>
    </receiver>

问题是此代码在Kitkat设备上正常运行,直到应用程序进入内存,存在一些延迟,如指定的文档中所述:

注意:从API 19(KITKAT)开始,警报传递是不准确的:操作系统 将切换警报,以尽量减少唤醒和电池使用。那里 是支持需要严格交付的应用程序的新API 担保;请参见设置窗口(int、long、long、pendingent)和 setExact(整型、长型、悬挂型)。应用程序 targetSdkVersion早于API 19,将继续看到 以前的行为,其中所有报警都在 请求


但当应用程序不在内存中时,报警管理器不会显示通知。我的目标是Kitkat及以上版本的android。

我为同一问题浪费了1个月的时间。最后我找到了解决办法。对于最近的Android版本(我不确定确切的版本),引入了一个名为“自动启动”的选项,这意味着用户可以实际配置任何应用程序是否可以自动启动。因此,请检查您的应用程序是否具有自动启动权限。此设置位置可能因手机制造商而异。因此,您需要在手机设置中搜索此“自动Lanuch”选项。即使它不起作用,也可以尝试从“设置”中找到的“优化列表”中删除你的应用程序。

我因为同样的问题浪费了1个月的时间。最后我找到了解决办法。对于最近的Android版本(我不确定确切的版本),引入了一个名为“自动启动”的选项,这意味着用户可以实际配置任何应用程序是否可以自动启动。因此,请检查您的应用程序是否具有自动启动权限。此设置位置可能因手机制造商而异。因此,您需要在手机设置中搜索此“自动Lanuch”选项。即使它不起作用,也可以尝试从“设置”中找到的“优化列表”中删除你的应用程序。

听起来,当你从“最近的应用程序列表”中删除应用程序时,你正在测试的设备实际上在强制停止应用程序,据我所知,这正是一些供应商实现的方式。如果是这种情况,那么您的警报将被清除,应用程序将恢复到停止状态。尝试从最近的应用程序中滑动应用程序,然后检查“设置”中应用程序信息页面中的“强制停止”按钮是否变灰。此外,请检查您的报警是否仍显示在
dumpsys报警中,如中所述。@MikeM。我试图从最近的应用程序中清除,并不想实际进入设置并强制关闭该应用程序,但它仍然不起作用。任何线索为什么会这样,即使应用程序被强制关闭,报警管理器是否会删除所有报警?我需要这个理由。“我试图从最近的应用程序中清除,但不想实际进入设置并强制关闭应用程序,但它仍然不起作用。”-对不起,我不明白你在这里说什么。“…即使应用程序被强制关闭,报警管理器是否会删除所有报警?”-如果应用程序被强制关闭,其报警将被清除。另外,在停止状态下,静态注册的接收器无论如何都不会工作,直到用户再次启动你的应用程序。那么基本上有没有办法让一些android组件在后台创建通知?我看到的一个很好的例子是一些著名的游戏,比如candy crush,在将来某个时间点创建通知,提醒用户即使应用程序被强制关闭,并且在某个时间点处于停止状态,它也会创建通知。我不知道candy crush是如何做到这一点的,如果确实有可能的话。听起来你正在测试的设备在你将其从最近的应用列表中删除时,实际上是在强制停止应用程序,据我所知,这正是一些供应商的实现方式。如果是这种情况,那么您的警报将被清除,应用程序将恢复到停止状态。尝试从最近的应用程序中滑动应用程序,然后检查“设置”中应用程序信息页面中的“强制停止”按钮是否变灰。此外,请检查您的报警是否仍显示在
dumpsys报警中,如中所述。@MikeM。我试图从最近的应用程序中清除,并不想实际进入设置并强制关闭该应用程序,但它仍然不起作用。任何线索为什么会这样,即使应用程序被强制关闭,报警管理器是否会删除所有报警?我需要这个理由。“我试图从最近的应用程序中清除,但不想实际进入设置并强制关闭应用程序,但它仍然不起作用。”-对不起,我不明白你在这里说什么。“…即使应用程序被强制关闭,报警管理器是否会删除所有报警?”-如果应用程序被强制关闭,其报警将被清除。另外,在停止状态下,静态注册的接收器无论如何都不会工作,直到用户再次启动你的应用程序。那么基本上有没有办法让一些android组件在后台创建通知?我看到的一个很好的例子是一些著名的游戏,比如candy crush,在将来某个时间点创建通知,提醒用户即使应用程序被强制关闭,并且在某个时间点处于停止状态,它也会创建通知。我不知道candy crush是如何做到这一点的,如果这确实可行的话。