App关闭时不触发AlarmManager
AlarmManager is not triggered when App is closed
我有一些在特定时间向用户显示的应用程序内通知,但当应用程序关闭时没有显示任何内容。
设置闹钟:
Intent alarmIntent = new Intent(mMotherActivity, ReminderAlarmManager.class);
if (ReminderNotificationType.CHANGE_LENS.equals(notificationType)) {
alarmIntent.putExtra("NOTIFICATION_TYPE", "REMINDER");
} else {
alarmIntent.putExtra("NOTIFICATION_TYPE", "ORDER");
}
long scTime = alarmDate.getTime();
PendingIntent pendingIntent = PendingIntent.getBroadcast(mMotherActivity, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) mMotherActivity.getSystemService(mMotherActivity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, scTime, pendingIntent);
广播接收者:
public class ReminderAlarmManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String notificationType = intent.getStringExtra("NOTIFICATION_TYPE");
if(notificationType.equalsIgnoreCase("ORDER"))
{
Intent i = new Intent(context, OrderReminderNotificationService.class);
context.startService(i);
}
else if(notificationType.equalsIgnoreCase("REMINDER"))
{
Intent i = new Intent(context, ReminderNotificationService.class);
context.startService(i);
}
}
}
所以说到scTime,即使App关闭了,我也想触发一个通知。所以我通过 PendingIntent 调用服务,如下所示:
public class OrderReminderNotificationService extends IntentService {
public OrderReminderNotificationService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Context context = getApplicationContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setContentTitle("Notification");
mBuilder.setContentText(context.getString(R.string.renewOrderNotificationMsg));
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setSound(uri);
mBuilder.setAutoCancel(true);
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(2, mBuilder.build());
}
清单中涉及的部分:
<receiver android:name="com.company.utils.ReminderAlarmManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
但是什么也没有出现...我做错了什么?
你的AndroidManifest.xml
应该是这样的:
<receiver android:name=".ReminderAlarmManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".OrderReminderNotificationService "/>
警报管理器不会触发某些手机上的 Intent 服务。在我意识到代码没有任何问题之前,我一整天都在 Oneplus 3T 上测试该应用程序。刚刚在 Moto G 上测试了相同的应用程序,它按预期工作。
您的设备不允许您触发闹钟,因为它需要节省一些电池电量。
因此,转到“设置”->Battery/power 保护程序->“应用”,然后选择您的应用,然后 select“无限制”
我有一些在特定时间向用户显示的应用程序内通知,但当应用程序关闭时没有显示任何内容。
设置闹钟:
Intent alarmIntent = new Intent(mMotherActivity, ReminderAlarmManager.class);
if (ReminderNotificationType.CHANGE_LENS.equals(notificationType)) {
alarmIntent.putExtra("NOTIFICATION_TYPE", "REMINDER");
} else {
alarmIntent.putExtra("NOTIFICATION_TYPE", "ORDER");
}
long scTime = alarmDate.getTime();
PendingIntent pendingIntent = PendingIntent.getBroadcast(mMotherActivity, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) mMotherActivity.getSystemService(mMotherActivity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, scTime, pendingIntent);
广播接收者:
public class ReminderAlarmManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String notificationType = intent.getStringExtra("NOTIFICATION_TYPE");
if(notificationType.equalsIgnoreCase("ORDER"))
{
Intent i = new Intent(context, OrderReminderNotificationService.class);
context.startService(i);
}
else if(notificationType.equalsIgnoreCase("REMINDER"))
{
Intent i = new Intent(context, ReminderNotificationService.class);
context.startService(i);
}
}
}
所以说到scTime,即使App关闭了,我也想触发一个通知。所以我通过 PendingIntent 调用服务,如下所示:
public class OrderReminderNotificationService extends IntentService {
public OrderReminderNotificationService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Context context = getApplicationContext();
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setContentTitle("Notification");
mBuilder.setContentText(context.getString(R.string.renewOrderNotificationMsg));
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setSound(uri);
mBuilder.setAutoCancel(true);
Intent notificationIntent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(2, mBuilder.build());
}
清单中涉及的部分:
<receiver android:name="com.company.utils.ReminderAlarmManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
但是什么也没有出现...我做错了什么?
你的AndroidManifest.xml
应该是这样的:
<receiver android:name=".ReminderAlarmManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".OrderReminderNotificationService "/>
警报管理器不会触发某些手机上的 Intent 服务。在我意识到代码没有任何问题之前,我一整天都在 Oneplus 3T 上测试该应用程序。刚刚在 Moto G 上测试了相同的应用程序,它按预期工作。
您的设备不允许您触发闹钟,因为它需要节省一些电池电量。 因此,转到“设置”->Battery/power 保护程序->“应用”,然后选择您的应用,然后 select“无限制”