我正在尝试设置一个警报管理器,使其每15分钟运行一次,检查是否有任何内容已添加到数据库中,然后显示通知。然而,虽然我只是测试这个,但我试图在60秒后显示通知。
以下代码在应用程序打开或最小化时完美运行,但在应用程序关闭时从不调用。
主要活动(onCreate())
Intent alarm = new Intent(this.context, AlarmReceiver.class);
boolean alarmRunning = (PendingIntent.getBroadcast(this.context, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
if (!alarmRunning) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 60000, pendingIntent);
}
警报接收器
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent background = new Intent(context, BackgroundService.class);
context.startService(background);
}
}
后台服务
public class BackgroundService extends Service {
private final String TAG = getClass().getName();
private Boolean isRunning;
private Context context;
private Thread backgroundThread;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
this.context = this;
this.isRunning = false;
this.backgroundThread = new Thread(myTask);
}
private Runnable myTask = new Runnable() {
@Override
public void run() {
Log.d(TAG, "THE BACKGROUND SERVICE IS RUNNING");
//Run actions
showNotification(context);
//
stopSelf();
}
};
private void showNotification(Context context) {
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Notification!") // title for notification
.setContentText("Alarm Received")
.setSound(soundUri)// message for notification
.setAutoCancel(true); // clear notification after click
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
@Override
public void onDestroy() {
this.isRunning = false;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!this.isRunning) {
this.isRunning = true;
this.backgroundThread.start();
}
return START_STICKY;
}
}
清单
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name=".notification.BackgroundService" />
<receiver android:name=".notification.AlarmReceiver"
android:process=":remote"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
我已阅读了几篇帖子,但似乎找不到如何解决这个问题的答案,以便在关闭应用时仍然可以显示/检查通知,关闭我的意思是用户从运行中移除应用的位置应用列表。
答案 0 :(得分:0)
尝试更改闹钟代码:
final int ALARM_ID = 1234; // your alarm id
startAlarm(600); // trigger after 10 minutes
public void startAlarm(int seconds) { // set alarm in seconds
Intent intent = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, ALARM_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (seconds * 1000), pendingIntent);
}
public void cancelAlarm() { // to cancel alarm before trigger
Log.d("lucas", "cancelAlarm");
Intent intent = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, ALARM_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
public boolean hasAlarm() { // check alarm is running
Intent intent = new Intent(this, AlarmReceiver.class);
return (PendingIntent.getBroadcast(this, ALARM_ID, intent, PendingIntent.FLAG_NO_CREATE) != null);
}
不调用 stopSelf()
方法,他可能会杀死应用程序。后台运行的服务使应用程序进程保持活动状态。如果没有服务运行,系统可能会终止您的应用,删除通知并取消正在运行的警报。
提示:点击或取消通知时调用方法stopSelf()
。
答案 1 :(得分:0)
通过使用onTaskRemoved()重新启动服务来管理解决此问题。
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.i(TAG, "onTaskRemoved()");
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
super.onTaskRemoved(rootIntent);
}