AlarmManager无法正常工作

时间:2012-05-21 11:08:55

标签: android alarmmanager

我需要在10秒后启动活动AlarmReceiver(例如)。我需要在不运行应用程序的情况下激活它。但无论应用程序是否运行,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();

5 个答案:

答案 0 :(得分:31)

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>标记之上。

这将设置一个警报,在调用方法SetAlarm()

后30秒内触发

答案 1 :(得分: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);

答案 2 :(得分:3)

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

答案 3 :(得分:0)

只要您的应用运行了一次以AlarmManager建立警报,即使您的应用未运行,警报也会激发您的意图。设备重启后例外。要start an alarm when the device restarts,请实施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>

答案 4 :(得分:-1)

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