アラームアプリケーションを開発したいのですが。アプリケーションは次のように動作するはずです。
- それを起動します
- 活動は私に時間を示します
- アラームをセットできます
- アプリケーションを閉じることができます
- アラーム時刻になると、アクティビティを開始します(デバイスがロックされている場合でも)
このサンプル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);
}
}
これは、サービスとレシーバーがセットアップされるマニフェストの部分です。
<receiver android:name=".OnAlarmReceiver"></receiver>
<service android:name=".AlarmService"></service>
doWakefulWorkメソッドが呼び出されることはありません!