我想開發報警應用程序。 應用程序應該像這樣工作:開發報警應用程序
- 推出它
- 活動給我時間
- 我可以設置報警
- 當報警時間已到,我可以關閉應用程序
- ,它開始一個活動(即使設備被鎖定)
我試過改編這個樣本https://github.com/commonsguy/cwac-wakeful但我當鬧鐘時間到時,無法啓動活動。
我使用此代碼來設置警報(用於測試我已插入該代碼上活性的onCreate
法):
Intent intent = new Intent(this, OnAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 10);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(),
pendingIntent);
這是OnAlarmReceiver類:
public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(ClockActivity.LOG_TAG, "OnAlarmReceiver::onReceive");
WakefulIntentService.sendWakefulWork(context, AlarmService.class);
}
}
這是服務分類:
public class AlarmService extends WakefulIntentService {
public AlarmService(String name) {
super(name);
}
@Override
protected void doWakefulWork(Intent intent) {
Log.i(ClockActivity.LOG_TAG, "AlarmService::doWakefulWork");
Intent newIntent = new Intent(getApplicationContext(), ClockActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.setAction(ClockActivity.ALARM_ACTION);
getApplicationContext().startActivity(newIntent);
}
}
這是Manifest的一部分,其中有setu p服務和接收器:
<receiver android:name=".OnAlarmReceiver"></receiver>
<service android:name=".AlarmService"></service>
doWakefulWork方法永遠不會被調用!
發佈您的代碼,最好是您嘗試啓動您的活動,並且還報告說,你得到的任何錯誤...這會幫助我們幫助你。當屏幕開啓時, – st0le 2010-12-19 07:46:15
是否有效?我只是試圖找出問題是否與喚醒鎖 – Andrew 2012-05-09 09:00:38
相關,因爲@PayPal_Tim在「答案」中詢問(而不是在此添加註釋)「Log.i(ClockActivity.LOG_TAG,」OnAlarmReceiver :: onReceive「 );'跑? – 2013-04-03 13:06:38