Skip to content

Instantly share code, notes, and snippets.

@burnix
Created July 16, 2015 11:35
Show Gist options
  • Save burnix/f0be8baa7ffcd3c1a212 to your computer and use it in GitHub Desktop.
Save burnix/f0be8baa7ffcd3c1a212 to your computer and use it in GitHub Desktop.
AlarmTest
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;
PowerManager.WakeLock fullWakeLock,partialWakeLock;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
createWakeLocks();
}
protected void createWakeLocks(){
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK");
partialWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK");
}
protected void onPause(){
super.onPause();
partialWakeLock.acquire();
}
protected void onResume(){
super.onResume();
if(fullWakeLock.isHeld()){
fullWakeLock.release();
}
if(partialWakeLock.isHeld()){
partialWakeLock.release();
}
}
public void wakeDevice() {
fullWakeLock.acquire();
}
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