简体   繁体   English

从KITKAT的最新应用列表中清除后,Android警报将被取消

[英]Android alarm is cancelled when cleared from recent app list in KITKAT

My application targets KITKAT and up android version and i am trying to create repetitive notification daily via Alarm Manager below is my code : 我的应用程序针对KITKAT和更高版本的android版本,我每天尝试通过以下警报管理器创建重复通知,这是我的代码:

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);
        }
    }


}

Main Activity : 主要活动 :

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);

        }
    }

}

Manifest : 清单:

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

Issue is This code runs properly on Kitkat device until app is in memory , there are some delays like as in doc specified : 问题是,此代码可在Kitkat设备上正常运行,直到应用程序在内存中为止,并且存在一些延迟,例如在doc中指定的延迟:

Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. 注意:从API 19(KITKAT)开始,警报传递是不准确的:操作系统将转移警报,以最大程度地减少唤醒和电池消耗。 There are new APIs to support applications which need strict delivery guarantees; 有一些新的API支持需要严格交付保证的应用程序。 see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). 请参见setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。 Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested. targetSdkVersion早于API 19的应用程序将继续看到以前的行为,在该行为中,所有警报均在请求时准确传递。

But When app is not in memory the alarm manager does not show notification . 但是,当应用程序不在内存中时,警报管理器不会显示通知。 I am specifically targeting Kitkat and above version of android. 我专门针对Kitkat及更高版本的android。

I lost 1 month of my time for the same issue. 我因同一问题失去了1个月的时间。 Finally I found the solution. 终于我找到了解决方案。 For the recent Android versions(exact version I am not sure ), an option called 'Auto Launch' have been introduced, which means the user can actually configure whether any app can be automatically started or not. 对于最新的Android版本(我不确定确切的版本),引入了一个名为“自动启动”的选项,这意味着用户可以实际配置是否可以自动启动任何应用程序。 So please check whether your app has permission for auto launch. 因此,请检查您的应用是否具有自动启动权限。 This setting location may vay based on phone manufacturer. 根据手机制造商的不同,该设置位置可能有所不同。 So you need to search for this 'Auto Lanuch' option in your phone settings. 因此,您需要在手机设置中搜索“自动Lanuch”选项。 Even if it doesn't work, try removing your app from the 'Optimizations List' as well, which you can find in the settings. 即使不起作用,也请尝试从“优化列表”中删除您的应用,您可以在设置中找到该应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从最近的任务中删除应用程序时,Android 通知被取消 - Android notification gets cancelled when app is removed from recent tasks 防止应用程序显示在android kitkat 4.4.2上的最新应用程序列表中 - Prevent an app to show on recent application list on android kitkat 4.4.2 从最近的应用程序中删除后,小部件在Android KitKat上停止 - Widget Stop on Android KitKat After Removing It From Recent App 从最近的列表中清除应用程序后,是否可以清除共享首选项中的数据? - Is it possible to clear data from shared preference when app is cleared from recent list.? android-从最新应用中删除的应用取消了前台通知 - android - foreground notification is cancelled on app removed from recent apps 如果从最近的活动android中删除应用程序,则SharedPreferences被清除 - SharedPreferences cleared if remove the app from recent activity android 当用户从最近的应用程序列表中删除应用程序时,如何取消警报管理器警报? - How to cancel alarm manager alarms when user removes the app from recent apps list? 当应用程序从最近清除时如何处理作为前台运行的服务? - How to handle services that run as foreground when an app is cleared from recent? 从Android 4.4 Kitkat设置闹钟 - Set alarm from Android 4.4 Kitkat Android-取消警报时会有延迟吗? - Android - Is There A Delay When An Alarm Has Been Cancelled?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM