Android:为什么AlarmManager不起作用?

时间:2012-07-27 05:50:53

标签: android broadcastreceiver alarmmanager

我有以下代码。应该以一定的间隔在logcat中发送消息但是不起作用。 stackoverflow上有很多类似的帖子,但我无法弄清楚问题。在某处可以帮助我吗?

<receiver android:name="BoopoohooAlarmReceiver"></receiver>

public void startAlarmManager(long interval){
    Context context = getApplicationContext();
    Intent intent = new Intent(context, BoopoohooAlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    Log.i(DEBUG, "hollaa");
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
}

public class BoopoohooAlarmReceiver extends BroadcastReceiver {
    private final String DEBUG = "BoopoohooAlarmReceiver"; 
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(DEBUG, "onReceive");
    }
}

感谢。

2 个答案:

答案 0 :(得分:6)

尝试添加“。” (点)在android清单文件声明中的接收者名称前面。

<receiver android:name=".BoopoohooAlarmReceiver"></receiver>

This也可能会有所帮助。

答案 1 :(得分:0)

请参阅此链接Alarm Manager 1Alarm Manager 2Alarm Manager 3这将有所帮助