【问题标题】:AlarmManager not work if activity is kill如果活动被杀死,AlarmManager 不起作用
【发布时间】:2018-02-10 01:54:38
【问题描述】:

我有一个 AlarmManager 使用来自用户输入的日历(存储在一个对象中),我的目标是从这个日历中安排通知,如果应用程序运行一切顺利,但如果它被从最近的应用程序中删除,通知不来。我的代码是:

Intent intent = new Intent(getApplicationContext(), EventReciver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0, intent,0);

Calendar calendar = Calendar.getInstance();
Calendar horaInicial = materiaBuffer.getHoraInicial(); //Take Calendar from materiaBuffer object
calendar.set(Calendar.HOUR_OF_DAY, horaInicial.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, horaInicial.get(Calendar.MINUTE));

pendingIntent = PendingIntent.getBroadcast(this, i, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

我只是在 AndroidManifest 中更改:

<receiver android:name=".etc.EventReciver" android:process=":remote"></receiver>

我的 EventReciver:

public class EventReciver extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
      Log.i("Lauri", "EventReciver");
      // I removed the code for the notification, it works fine
  }
}

谁能帮忙?

【问题讨论】:

  • swipped away from recents apps 有时意味着杀死整个应用进程。
  • @AvatarQing 有什么方法可以保留 AlarmManager?
  • 我没有什么好主意,我认为强制终止进程后没有办法保持闹钟工作,即使在一些著名的第三方闹钟应用程序中也是如此。也许您应该寻找使应用程序永远保持活力的方法。
  • 你在什么设备上测试?
  • @DavidWasser 在华硕 Zenfone 3、Android 7 中

标签: android notifications android-service alarmmanager


【解决方案1】:

我在 Lolipop 上对其进行了测试。此代码工作正常,甚至应用程序没有运行

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 27);
calendar.set(Calendar.SECOND, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

maifest

<receiver
  android:name=".AlarmReceiver"
  android:enabled="true"
  android:exported="true" />

接收器类

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("Alarm Received");
    Toast.makeText(context, "Alerm Received", Toast.LENGTH_SHORT).show();

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setContentTitle("Alarm")
            .setContentText("This code works!")
            .setAutoCancel(true);
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
 }
}

【讨论】:

  • 您能否稍微解释一下您的答案,而不是向我们扔一些代码?
  • 不,不起作用。在应用程序运行时仅显示通知。还有其他想法吗?
  • @TanbirHossen,我忘了给你打电话
  • @TanbirHossen 没什么,我就是搞不定......也许是安卓版本?我正在使用 Android 7 进行测试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多