AlarmManager不工作(AlarmManager not working)

2019-06-24 15:01发布

我要开始活动AlarmReceiver 10秒(例如)之后。 我需要它不运行应用程序激活。 但无论在应用程序运行或不AlarmReceiver不会被调用。 有什么建议?

Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 111, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

//alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
                                          //+ (10 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

Answer 1:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
          String message = "Hellooo, alrm worked ----";
          Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
          Intent intent2 = new Intent(context, TripNotification.class); 
          intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(intent2);
    }

    public void setAlarm(Context context){
        Log.d("Carbon","Alrm SET !!");

        // get a Calendar object with current time
         Calendar cal = Calendar.getInstance();
         // add 30 seconds to the calendar object
         cal.add(Calendar.SECOND, 30);
         Intent intent = new Intent(context, AlarmReceiver.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

         // Get the AlarmManager service
         AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
         am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    }
}

这是最后的代码,我设法得到工作。 您需要添加

 <receiver  android:process=":remote" android:name="AlarmReceiver"></receiver>

正上方的</application>在清单文件标记。

这将调用方法之后设置一个报警在30秒内以触发SetAlarm()



Answer 2:

截至目前,这是不可能的,但不运行应用程序启动报警,则必须运行一次你们各自的应用程序来激活您的报警。对于这一点....!

在您的ALARM_ACTIVITY:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(ALARM_ACTIVITY.this,ALARM_RECEIVER.class); 

PendingIntent pendingIntent = PendingIntent.getBroadcast(SetReminder.this, ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeInMillis() + 1000, pendingIntent);

在您的ALARM_RECEIVER:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

notification = new Notification(R.drawable.alarmicon, "charSequence", System.currentTimeMillis());

notification.setLatestEventInfo(context, "alarmTitle", "charSequence", pendingIntent);

notification.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(1, notification);


Answer 3:

如果它仍然没有工作摆脱的android:process=":remote"部分可能会有帮助。 为我工作:)



Answer 4:

只要你的应用程序已经运行一次,以建立与报警AlarmManager ,报警触发,即使您的应用程序没有运行你的意图。 唯一的例外是设备重启后。 要启动报警时设备重新启动 ,实现BroadcastReceiver设置报警和接收器添加到您的舱单ACTION_BOOT_COMPLETED

<receiver android:name=".SampleBootReceiver"
        android:enabled="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>


Answer 5:

此外,除了上述情况,我认为在AlarmActivity的方法应该是在发射活动的onCreate方法。在这种情况下,报警Activvity应该是应用的LAUNCHER活动。 这解决了我的问题



文章来源: AlarmManager not working