Created
December 27, 2013 01:30
-
-
Save daichan4649/8141197 to your computer and use it in GitHub Desktop.
毎朝10時にアラーム実行(指定Service起動)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void registerAlarm(Context context) { | |
// 現在 | |
final Date nowDate = new Date(); | |
Calendar now = Calendar.getInstance(); | |
now.setTime(nowDate); | |
// 毎朝10:00 | |
int alarmHH = 10; | |
int alarmMM = 0; | |
final String alarmHHMM = userInfo.getAlarm_time(); | |
if (!TextUtils.isEmpty(alarmHHMM)) { | |
int index = alarmHHMM.indexOf(":"); | |
if (index != -1) { | |
alarmHH = convertText2Integer(alarmHHMM.substring(0, index)); | |
alarmMM = convertText2Integer(alarmHHMM.substring(index, alarmHHMM.length())); | |
} | |
} | |
Calendar nextTrigger = Calendar.getInstance(); | |
nextTrigger.setTime(nowDate); | |
nextTrigger.set(Calendar.HOUR_OF_DAY, alarmHH); | |
nextTrigger.set(Calendar.MINUTE, alarmMM); | |
nextTrigger.clear(Calendar.SECOND); | |
if (nextTrigger.before(now)) { | |
// 翌日 | |
nextTrigger.add(Calendar.DATE, 1); | |
} | |
// アラーム登録 | |
registerAlarm(context, now, nextTrigger); | |
} | |
public static void registerAlarm(Context context, Calendar now, Calendar nextTrigger) { | |
// 起動日時(1日毎に実行) | |
int type = AlarmManager.ELAPSED_REALTIME_WAKEUP; | |
long firstTime = SystemClock.elapsedRealtime() + (nextTrigger.getTimeInMillis() - now.getTimeInMillis()); | |
long intervalMillis = AlarmManager.INTERVAL_DAY; | |
//15秒毎 | |
// long intervalMillis = 15000; | |
final PendingIntent operation = createPendingIntent(context); | |
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | |
am.cancel(operation); | |
am.setRepeating(type, firstTime, intervalMillis, operation); | |
// //TODO debug(30秒後に1shot) | |
// // adb shell am broadcast -a android.intent.action.PACKAGE_REPLACED | |
// int type = AlarmManager.ELAPSED_REALTIME_WAKEUP; | |
// final PendingIntent operation = createPendingIntent(context); | |
// AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | |
// am.cancel(operation); | |
// am.set(type, SystemClock.elapsedRealtime() + 30000, operation); | |
} | |
public static void cancelAlarm(Context context) { | |
final PendingIntent pending = createPendingIntent(context); | |
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | |
am.cancel(pending); | |
} | |
private static PendingIntent createPendingIntent(Context context) { | |
final Intent sender = new Intent(context, TestService.class); | |
return PendingIntent.getService(context, 0, sender, 0); | |
} | |
private static int convertText2Integer(String intText) { | |
if (TextUtils.isEmpty(intText)) { | |
return 0; | |
} | |
try { | |
return Integer.parseInt(intText); | |
} catch (Exception e) { | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestService extends Service { | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
startTestTask(); | |
// service stop | |
stopSelf(); | |
return super.onStartCommand(intent, flags, startId); | |
} | |
private void startTestTask() { | |
new TestTask().execute(); | |
} | |
private class TestTask extends AsyncTask<Void, Void, Void> { | |
@Override | |
protected Void doInBackground(Void... params) { | |
//TODO | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Void result) { | |
showTestActivity(); | |
} | |
} | |
private void showTestActivity() { | |
Intent intent = new Intent(getApplicationContext(), TestActivity.class); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
startActivity(intent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment