睡眠期间未收到警报

时间:2013-01-24 23:33:26

标签: android android-intent android-alarms android-broadcast

睡眠期间未收到警报。

警报响起后,我将警报设置为午夜以更新我的视图。当设备处于唤醒状态时,警报会起作用 - 已经通过将时间更改为午夜等进行了测试。但是,当我在手机上进行测试时,它在睡眠期间无效。我在午夜前几个小时睡着了手机,然后在早上8点左右检查了应用程序。但是视图没有更新,所以我认为没有收到警报。

我在模拟器上尝试了以下操作 - 让设备在午夜之前进入睡眠状态,然后在午夜后唤醒设备。但是,我可以看到正在更新的数据,并在我的日志中收到警报。

关于我在下面的代码中做错了什么想法?

的活动:

@Override
    protected void onResume(){
        super.onResume();
        updateReceiver = new AlarmReceiver(new AlarmReceiverCallback(){
            @Override 
            public void onAlarmReceived(Intent intent) {
                //update view code
            }
        });
        IntentFilter updateIntentFilter = new IntentFilter("alarm");
        registerReceiver(updateReceiver,updateIntentFilter);
    }

    @Override
    protected void onPause(){
        super.onPause();
        if(this.updateReceiver!=null){
            unregisterReceiver(updateReceiver);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //set up view 

        setRecurringDailyAlarm();
    }

    private void setRecurringDailyAlarm(){

        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeInMillis(System.currentTimeMillis());

        //daily alarm at midnight
        updateTime.set(Calendar.HOUR_OF_DAY,00);
        updateTime.set(Calendar.MINUTE,00);
        updateTime.set(Calendar.SECOND,00);
        Log.d(TAG, "Alarm time is: " + updateTime.getTime());
        Intent myIntent = new Intent();
        myIntent.setAction("alarm");
        PendingIntent recurringAlarm = PendingIntent.getBroadcast(MainActivity.this, ALARM_ID, myIntent, 
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarms = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

        alarms.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), 
                AlarmManager.INTERVAL_DAY, recurringAlarm);

    }

清单:

                          

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<receiver android:name=".receiver.BootUpReceiver" android:enabled="true" android:exported="false" 
    android:permission="android.premission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
 <receiver android:name="com.test.receiver.RestartReceiver" android:enabled="true" android:process=":remote"></receiver>

== EDIT

如果我将代码更改为Intent myIntent = new Intent(this, AlarmReceiver.class);

这会在我的应用程序中引发错误,因为我已经实现了如下警报接收器。我正在使用“回调”来更新我的UI

警报接收器类:

    interface AlarmReceiverCallback{
    public void onAlarmReceived(Intent intent);
}

public class AlarmReceiver extends BroadcastReceiver{

    private AlarmReceiverCallback callback;

    public AlarmReceiver(AlarmReceiverCallback callback){
        this.callback = callback;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        callback.onAlarmReceived(intent);
    }
}

错误日志:

01-25 16:05:38.655: D/dalvikvm(810): newInstance failed: no <init>()
01-25 16:05:38.686: D/AndroidRuntime(810): Shutting down VM
01-25 16:05:38.686: W/dalvikvm(810): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-25 16:05:38.785: E/AndroidRuntime(810): FATAL EXCEPTION: main
01-25 16:05:38.785: E/AndroidRuntime(810): java.lang.RuntimeException: Unable to instantiate receiver com.test.receiver.AlarmReceiver: java.lang.InstantiationException: can't instantiate class com.test.receiver.AlarmReceiver; no empty constructor
01-25 16:05:38.785: E/AndroidRuntime(810):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2357)
01-25 16:05:38.785: E/AndroidRuntime(810):  at android.app.ActivityThread.access$1500(ActivityThread.java:141)
01-25 16:05:38.785: E/AndroidRuntime(810):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
01-25 16:05:38.785: E/AndroidRuntime(810):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 16:05:38.785: E/AndroidRuntime(810):  at android.os.Looper.loop(Looper.java:137)
01-25 16:05:38.785: E/AndroidRuntime(810):  at android.app.ActivityThread.main(ActivityThread.java:5039)
01-25 16:05:38.785: E/AndroidRuntime(810):  at java.lang.reflect.Method.invokeNative(Native Method)
01-25 16:05:38.785: E/AndroidRuntime(810):  at java.lang.reflect.Method.invoke(Method.java:511)
01-25 16:05:38.785: E/AndroidRuntime(810):  at

1 个答案:

答案 0 :(得分:3)

您应该注册用于捕获清单中警报的接收器以及其他接收器。然后创建启动AlarmReceiver的意图。

当您在代码中执行此操作时,它已在onPause中取消注册,因此如果您的活动在警报触发时未处于前台,则不会要抓住它。

清单:

<receiver android:name="com.test.receiver.AlarmReceiver"
/>

代码:

private void setRecurringDailyAlarm(){

    Calendar updateTime = Calendar.getInstance();
    updateTime.setTimeInMillis(System.currentTimeMillis());

    //daily alarm at midnight
    updateTime.set(Calendar.HOUR_OF_DAY,00);
    updateTime.set(Calendar.MINUTE,00);
    updateTime.set(Calendar.SECOND,00);
    Log.d(TAG, "Alarm time is: " + updateTime.getTime());

    Intent myIntent = new Intent( this, AlarmReceiver.class );
    PendingIntent recurringAlarm = PendingIntent.getBroadcast( MainActivity.this, ALARM_ID, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarms = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

    alarms.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), 
            AlarmManager.INTERVAL_DAY, recurringAlarm);
}