Last active
December 7, 2016 10:58
-
-
Save bng86/958bd40609830909e689e2d445ba0d37 to your computer and use it in GitHub Desktop.
AlarmManager
This file contains 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 AlarmActivity extends AppCompatActivity{ | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); | |
Calendar calendar = Calendar.getInstance(); | |
// 現在的 18:30:00 | |
calendar.set(Calendar.HOUR, 18); | |
calendar.set(Calendar.MINUTE, 30); | |
calendar.set(Calendar.SECOND, 0); | |
long alarmTimes; | |
// 避免設定到過去的時間,如果是過去的時間 day + 1 | |
if (calendar.getTimeInMillis() > new Date().getTime()) { | |
alarmTimes = calendar.getTimeInMillis(); | |
} else { | |
calendar.add(Calendar.DAY_OF_MONTH, 1); | |
alarmTimes = calendar.getTimeInMillis(); | |
} | |
// 現在三秒後 | |
calendar.add(Calendar.SECOND, 3); | |
PendingIntent pendingIntent = | |
PendingIntent.getBroadcast(this, 0, new Intent(this, AlarmReceiver.class), PendingIntent.FLAG_ONE_SHOT); | |
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); | |
// RTC 真實時間 WAKEUP 要不要喚醒螢幕 | |
// AlarmManager.RTC | |
// AlarmManager.RTC_WAKEUP | |
// ELAPSED_REALTIME 開機時間 WAKEUP 要不要喚醒螢幕 | |
// AlarmManager.ELAPSED_REALTIME | |
// AlarmManager.ELAPSED_REALTIME_WAKEUP | |
} | |
} |
This file contains 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 AlarmReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
NotificationManager notificationManager | |
= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | |
PendingIntent pendingIntent = | |
PendingIntent.getActivity(context, 0, new Intent(context, AlarmActivity.class), PendingIntent.FLAG_ONE_SHOT); | |
Notification notification = new NotificationCompat.Builder(context) | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setContentTitle("起床別睡了") | |
.setContentText("我要把你公諸於世") | |
.setContentIntent(pendingIntent) | |
.build(); | |
notificationManager.notify(0, notification); | |
} | |
} |
This file contains 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
<receiver android:name=".AlarmReceiver"> | |
<intent-filter> | |
<action android:name="android.intent.action.BOOT_COMPLETED"/> | |
</intent-filter> | |
</receiver> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment