Created
July 16, 2015 12:07
-
-
Save kutukoff/62b4bfe8cf8c5ffc866d to your computer and use it in GitHub Desktop.
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
| import android.app.Activity; | |
| import android.app.AlarmManager; | |
| import android.app.Dialog; | |
| import android.app.Notification; | |
| import android.app.NotificationManager; | |
| import android.app.PendingIntent; | |
| import android.app.TimePickerDialog; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.os.Bundle; | |
| import android.os.PowerManager; | |
| import android.os.SystemClock; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.view.WindowManager; | |
| import android.widget.TextClock; | |
| import android.widget.TimePicker; | |
| import java.util.Calendar; | |
| public class MainActivity extends Activity { | |
| final String LOG_TAG = "myLogs"; | |
| NotificationManager nm; | |
| AlarmManager am; | |
| TimePicker timePicker; | |
| int hours; | |
| int minutes; | |
| TimePickerDialog timePickerDialog; | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.main); | |
| nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
| am = (AlarmManager) getSystemService(ALARM_SERVICE); | |
| } | |
| public void onClick1(View view) { | |
| openTimePickerDialog(false); | |
| Calendar cal = Calendar.getInstance(); | |
| cal.setTimeInMillis(System.currentTimeMillis()); | |
| cal.set(Calendar.HOUR_OF_DAY, hours); | |
| cal.set(Calendar.MINUTE, minutes); | |
| } | |
| private void openTimePickerDialog(boolean is24r){ | |
| Calendar calendar = Calendar.getInstance(); | |
| timePickerDialog = new TimePickerDialog( | |
| MainActivity.this, | |
| onTimeSetListener, | |
| calendar.get(Calendar.HOUR_OF_DAY), | |
| calendar.get(Calendar.MINUTE), | |
| is24r); | |
| timePickerDialog.setTitle("Set Alarm Time"); | |
| timePickerDialog.show(); | |
| } | |
| TimePickerDialog.OnTimeSetListener onTimeSetListener | |
| = new TimePickerDialog.OnTimeSetListener(){ | |
| @Override | |
| public void onTimeSet(TimePicker view, int hourOfDay, int minute) { | |
| Calendar calNow = Calendar.getInstance(); | |
| Calendar calSet = (Calendar) calNow.clone(); | |
| calSet.set(Calendar.HOUR_OF_DAY, hourOfDay); | |
| calSet.set(Calendar.MINUTE, minute); | |
| calSet.set(Calendar.SECOND, 0); | |
| calSet.set(Calendar.MILLISECOND, 0); | |
| if(calSet.compareTo(calNow) <= 0){ | |
| //Today Set time passed, count to tomorrow | |
| calSet.add(Calendar.DATE, 1); | |
| } | |
| setAlarm(calSet); | |
| }}; | |
| private void setAlarm(Calendar targetCal){ | |
| Intent intent = new Intent(this, MainActivity.class); | |
| PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT); | |
| ((AlarmManager) getSystemService(ALARM_SERVICE)).set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); | |
| } | |
| public void onClick2(View view) { | |
| } | |
| void sendNotif(int id, PendingIntent pIntent) { | |
| Notification notif = new Notification(R.mipmap.ic_launcher, "Alarm set " | |
| , System.currentTimeMillis()); | |
| notif.setLatestEventInfo(this, "Title " + id, "Content " + id, pIntent); | |
| nm.notify(id, notif); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment