Android Alarm从未收到过

时间:2012-03-14 22:06:31

标签: android alarmmanager android-alarms

我试图让我的应用程序在指定的时间运行,但我无法让它运行。我可以在运行adb shell dumpsys alarm时看到待处理的警报,并且每次运行AlarmActivity时警报都会增加。

我添加了adb shell dumpsys alarm输出,我们可以看到警报。我看不出有什么问题 谢谢你的帮助

这里有我的AlarmActivity代码:

package net.dradge.alarm;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class AlarmActivity extends Activity{
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);
    }

    protected void onResume(){
        super.onResume();
        this.setAlarm();
    }

    public class AlarmReceiver extends BroadcastReceiver{
        public void onReceive(Context context, Intent intent){
            Log.w("net.dradge.alarm", "Alarm received");
            AlarmActivity.this.setAlarm();
        }
    }

    private synchronized void setAlarm(){
        Calendar date = Calendar.getInstance();

        AlarmManager manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(this, AlarmReceiver.class);
        PendingIntent operation = PendingIntent.getBroadcast(AlarmActivity.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        manager.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis() + 30 * 1000, operation);

        DateFormat df = DateFormat.getDateTimeInstance();
        Log.w("net.dradge.alarm", "Alarm set to " + df.format(new Date(date.getTimeInMillis() + 30 * 1000)));
    }
}

这里有Manifest代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.dradge.alarm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AlarmActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".AlarmActivity.AlarmReceiver"></receiver>
    </application>

</manifest>

adb shell dumpsys警报     当前的警报管理器状态:

  Realtime wakeup (now=1332181365617):
  RTC_WAKEUP #2: Alarm{45260f00 type 0 com.android.providers.calendar}
    type=0 when=1332231022777 repeatInterval=0 count=0
    operation=PendingIntent{4521fdf8: PendingIntentRecord{45260e88 com.android.providers.calendar broadcastIntent}}
  RTC_WAKEUP #1: Alarm{451d51b8 type 0 android}
    type=0 when=1332201600000 repeatInterval=0 count=0
    operation=PendingIntent{44f98c98: PendingIntentRecord{45084d80 android broadcastIntent}}
  RTC_WAKEUP #0: Alarm{45222b40 type 0 com.google.android.gsf}
    type=0 when=1332182126363 repeatInterval=1800000 count=0
    operation=PendingIntent{45285370: PendingIntentRecord{44f58970 com.google.android.gsf broadcastIntent}}
  RTC #1: Alarm{45142d08 type 1 android}
    type=1 when=1332241200000 repeatInterval=0 count=0
    operation=PendingIntent{4504aba8: PendingIntentRecord{4504ab70 android broadcastIntent}}
  RTC #0: Alarm{451c1b90 type 1 android}
    type=1 when=1332181380000 repeatInterval=0 count=0
    operation=PendingIntent{450363c0: PendingIntentRecord{450548d8 android broadcastIntent}}

  Elapsed realtime wakeup (now=505486995):
  ELAPSED #0: Alarm{45201280 type 3 android}
    type=3 when=505988995 repeatInterval=0 count=0
    operation=PendingIntent{44ff4190: PendingIntentRecord{44f6b9a0 android broadcastIntent}}

  Broadcast ref count: 0

  Alarm Stats:
  net.dradge.alarm
    23ms running, 4 wakeups
    4 alarms: flg=0x4 cmp=net.dradge.alarm/.AlarmActivity$AlarmReceiver
  com.google.android.gsf
    13965ms running, 280 wakeups
    280 alarms: flg=0x4
  com.android.providers.calendar
    195ms running, 6 wakeups
    6 alarms: act=com.android.providers.calendar.SCHEDULE_ALARM flg=0x4
  android
    105577ms running, 7 wakeups
    6 alarms: act=android.intent.action.DATE_CHANGED flg=0x20000004
    2 alarms: act=com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KEYGUARD flg=0x4
    561 alarms: act=android.intent.action.TIME_TICK flg=0x40000004
    305 alarms: act=com.android.server.ThrottleManager.action.POLL flg=0x4
    5 alarms: act=com.android.service.Watchdog.CHECKUP flg=0x4

1 个答案:

答案 0 :(得分:2)

我认为您的接收器应该声明为

<receiver android:name=".AlarmActivity$AlarmReceiver"></receiver>

因为它是一个内部阶级。