Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果活动被终止,AlarmManager将不工作_Android_Notifications_Android Service_Alarmmanager - Fatal编程技术网

Android 如果活动被终止,AlarmManager将不工作

Android 如果活动被终止,AlarmManager将不工作,android,notifications,android-service,alarmmanager,Android,Notifications,Android Service,Alarmmanager,我有一个AlarmManager,它使用用户输入的日历(存储在一个对象中),我的目标是从这个日历中安排通知,如果应用程序运行正常,但如果它从最近的应用程序中被刷走,则通知不会出现。我的代码是: Intent intent = new Intent(getApplicationContext(), EventReciver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0, intent,0); Cal

我有一个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>

有人能帮忙吗?

我在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" />

从最近的应用程序中退出
有时意味着杀死整个应用程序进程。@AvatarQing如何保持AlarmManager?我没有什么好主意,我认为在强制杀死进程后无法保持闹钟工作,即使是在一些著名的第三方闹钟应用程序中。也许你应该寻找让应用程序永远保持活力的方法。你在测试什么设备?@DavidWasser在华硕Zenfone 3,Android 7中。你能解释一下你的答案吗,而不是仅仅对我们说些代码吗?不,不行。在应用程序运行时仅显示通知。“还有别的想法吗?”坦比霍森,我忘了打电话了you@TanbirHossen没什么,我就是做不到。。。也许是安卓版本?我正在使用安卓7来测试它
<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());
 }
}