[英]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.