gpt4 book ai didi

android - App关闭时不触发AlarmManager

转载 作者:搜寻专家 更新时间:2023-11-01 09:40:48 26 4
gpt4 key购买 nike

我有一些在特定时间向用户显示的应用程序内通知,但当应用程序关闭时没有显示任何内容。

设置闹钟:

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

list 中与此相关的部分:

<receiver android:name="com.company.utils.ReminderAlarmManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

但是什么也没有出现...我做错了什么?

最佳答案

您的设备不允许您触发警报,因为它需要节省一些电池电量。因此,转到“设置”->“电池/省电”->“应用”,然后选择您的应用,然后选择“无限制”

关于android - App关闭时不触发AlarmManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39979703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com