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

时间:2016-01-28 08:20:53

标签: android alarmmanager

我的应用程序针对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设备上正常运行,直到app在内存中,有一些延迟,如在doc指定的那样:

  

注意:从API 19(KITKAT)开始,警报传递是不准确的:操作系统   将移动警报以最小化唤醒和电池使用。那里   是支持需要严格交付的应用程序的新API   担保;请参阅setWindow(int,long,long,PendingIntent)和   setExact(int,long,PendingIntent)。应用程序   targetSdkVersion早于API 19将继续看到   以前的行为,其中所有警报都在何时传递   请求。

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

1 个答案:

答案 0 :(得分:0)

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