1

睡眠期间未收到警报。

我将闹钟设置为午夜,以便在闹钟响起后更新我的视图。当设备处于唤醒状态时,警报会起作用 - 已经通过将时间更改为午夜等进行了测试。但是在我的手机上进行测试时,它在睡眠期间不起作用。我在午夜前几个小时让手机处于睡眠状态,然后在早上 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>

==编辑

如果我将代码更改为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
4

1 回答 1

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);
}
于 2013-01-24T23:47:46.610 回答