Skip to content

Instantly share code, notes, and snippets.

@sakurabird
Created October 24, 2011 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakurabird/1308915 to your computer and use it in GitHub Desktop.
Save sakurabird/1308915 to your computer and use it in GitHub Desktop.
Untouchable Timer
// アクティビティからタイマーサービススタート
Intent intent = new Intent( mContext, TimerService.class );
intent.putExtra( "counter", result );
startService( intent );
//呼び出されたServiceプログラムでTimerをスタートします。
if ( timer != null )
timer.cancel();
timer = new Timer();
final android.os.Handler handler = new android.os.Handler();
timer.schedule( new TimerTask() {
@Override
publicvoid run() {
handler.post( new Runnable() {
publicvoid run() {
//そして、タイマーの残り時間がゼロになったらアラーム鳴動処理プログラムを起動します。
if ( counter == -1 ) {
timer.cancel();
if ( wl.isHeld() )
wl.release();
showAlarm();
} else {
//1秒毎に呼び出し元のActivityのタイマー表示メソッドを呼び出して画面を更新しています。
UntouchableTimerActivity.onTimerChanged( counter );
counter = counter - 1;
}
}
} );
}
//timer.schedule( タイマー処理, 0, 1000);で1秒単位で実行
}, 0, 1000 );
//アラーム鳴動処理ではServiceをストップしてからアラーム処理のActivityを呼び出しています。
void showAlarm() {
if ( timer != null )
timer.cancel();
// サービスのストップ
Intent intent = new Intent( mContext, TimerService.class );
mContext.stopService( intent );
// アラーム画面の起動
intent = new Intent( mContext, AlarmActivity.class );
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
mContext.startActivity( intent );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment