我想每小时发出一条通知。
我在AlarmManager服务中使用setRepeating,问题是当我关闭我的应用程序时,管理器不会广播到我的BroadcastReceiver。
我的BroadcastReceiver:
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(context, "notify_001")
.setContentTitle("URL Database")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentText("You didn't use the URLDatabase app for a while.\nYour urls feel lonely.")
.setContentIntent(pendingIntent).build();
NotificationManager manager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel("notify_001",
"URLDatabaseNotification",
NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, notification);
Log.i("URLDatabase", "Received");
}
}
我的AlarmManager代码(我在我的活动的onCreate中使用了这个):
calendar = Calendar.getInstance();
intent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am = (AlarmManager) this.getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60, pendingIntent);
我是否需要创建在后台工作的自定义服务?
https://stackoverflow.com/questions/50777973
复制相似问题