【发布时间】:2017-04-21 22:19:24
【问题描述】:
我正在尝试做一个应用程序,它每天早上都会通过推送通知提醒用户。
我一直在使用 AlarmManager、BroadcastReceiver 和 IntentService。
在下面的代码中,我以 60 秒的间隔测试了我的 AlarmManager。
问题: 一切正常,直到我关闭我的应用程序,警报不再响起。
有什么想法可以下一步去哪里吗?
清单:
<service
android:name=".IntentMentor"
android:exported="false" >
</service>
<receiver android:name=".AlertReceiver"
android:process=":remote">
</receiver>
主活动:
Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
// 1s is only for testing
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 1000*60, pIntent);
接收者:
public class AlertReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "MyReceiver on receive");
Intent i = new Intent(context, IntentMentor.class);
context.startService(i);
}
}
意图服务:
public class IntentMentor extends IntentService {
NotificationManager notificationManager;
int notifID = 33;
private static final String TAG = "MonitorService";
public IntentMentor() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d("TAG", "Service method was fired.");
pushNotification();
}
public void pushNotification(){
NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this);
notificBuilder.setContentTitle("test");
notificBuilder.setContentText("this is text");
notificBuilder.setTicker("this is Ticker?");
notificBuilder.setSmallIcon(R.drawable.ic_info_black_24dp);
notificBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
Intent intent = new Intent(this, Card.class);
TaskStackBuilder tStackBuilder = TaskStackBuilder.create(this);
tStackBuilder.addParentStack(Card.class);
tStackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = tStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
notificBuilder.setContentIntent(pendingIntent);
notificationManager = (NotificationManager) getSystemService((Context.NOTIFICATION_SERVICE));
notificationManager.notify(notifID, notificBuilder.build() );
}
}
【问题讨论】:
-
我在你的链接后面尝试解决方案。谢谢..但我仍然有同样的问题。应用未打开时,不会触发警报。
-
确保您授予 WAKE_LOCK 权限
-
Try this set of code with your. 你可以改变这一行 "alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 1000*60, pIntent);"
-
很抱歉成为这样的菜鸟。但我应该在“intentLR”中使用什么。我不明白那是什么意思。 "piLR = PendingIntent.getBroadcast(context, 0, intentLR, PendingIntent.FLAG_UPDATE_CURRENT);"
标签: android alarmmanager intentservice keep-alive