25

我需要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();
4

5 回答 5

42
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()

于 2012-05-21T13:44:31.497 回答
6

截至目前,不运行应用程序就无法启动警报,您必须运行各自的应用程序才能激活警报..为此....!!

在您的 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);
于 2012-05-21T12:18:27.753 回答
3

如果它仍然不起作用,则摆脱该android:process=":remote"部分可能会有所帮助。为我工作:)

于 2017-01-20T15:45:40.790 回答
-1

另外,除了上述之外,我认为AlarmActivity中的方法应该在LAUNCHER活动的oncreate方法中。在这种情况下,Alarm Activity应该是应用程序的LAUNCHER活动。这解决了我的问题

于 2016-06-20T19:31:07.453 回答
-1

只要您的应用程序运行一次以建立警报AlarmManager,即使您的应用程序没有运行,警报也会触发您的意图。设备重启后例外。要在设备重新启动时启动警报,请实现 aBroadcastReceiver来设置警报,并将接收器添加到您的清单中ACTION_BOOT_COMPLETED

<receiver android:name=".SampleBootReceiver"
        android:enabled="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>
于 2018-11-12T14:59:43.500 回答