睡眠期間未收到鬧鈴。睡眠期間未接收到鬧鈴
我有一個鬧鐘設置爲午夜來更新我的視圖,一旦鬧鐘響起。警報在設備醒來時工作 - 已通過將時間更改爲午夜等來測試此功能。但是,在我的手機上進行測試時,它在睡眠期間不起作用。我在午夜前幾個小時讓我的手機進入睡眠狀態,然後在早上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
感謝。我在清單文件中添加了該行並更改了意圖代碼。然而,該應用程序現在停止加載。我得到「不幸的應用程序名稱已停止」。如果我從意圖中刪除參數,我不會得到錯誤 – Santiago
發佈日誌輸出。 – Ralgha
發佈日誌輸出@Ralgha – Santiago