I am trying to add below functionalities to my application. 1. One time alarm 2. repeat alarm 3. cancel alarm 4. alarm after reboot. 5. alarm even after phone is locked.
Except 5 all are working. and current behavior is if the phone is lock/screen off state and alarm time is up, it is not waking the device.
Second, if user sets time at 7.30AM for repeat, i am calling setrepeat with 1000*60*60*24 is this right??
Third, after reboot, calling setrepeat so that alarm persist and will continue with same time. Does my steps correct here ? where I am making mistake ???. If alarm set, I want to notify for lock & reboot etc.. I read somewhere that for wake up, I many need to use wakelock but not sure where I need to put it exactly.
In manifest file, add user permissions: WAKE_LOCK, RECEIVE_BOOT_COMPLETED And action BOOT_COMPLETED for for the activity.
Below is the code: AlarmSchedular.java:
public class AlarmScheduler extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup_alarm);
timePicker = (TimePicker)findViewById(R.id.picker);
optRepeat = (CheckBox)findViewById(R.id.optrepeat);
textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt);
alarmTitleView = (TextView)findViewById(R.id.alarmTitle);
sp = PreferenceManager.getDefaultSharedPreferences(this);
spe = sp.edit();
updateAlarmManagerData();
buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog);
buttonstartSetDialog.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calSet.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
hour = timePicker.getCurrentHour();
min = timePicker.getCurrentMinute();
spe.putInt("ALARM_HOUR", hour);
spe.putInt("ALARM_MIN", min);
spe.commit();
if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
setAlarm(calSet, optRepeat.isChecked());
}
});
buttonCancelAlarm = (Button)findViewById(R.id.cancel);
buttonCancelAlarm.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
cancelAlarm();
}
});
}
private void setAlarm(Calendar targetCal, boolean repeat){
ComponentName receiver = new ComponentName(getBaseContext(), AlarmReceiver.class);
PackageManager pm = getBaseContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
spe.putBoolean("alarmSet", true);
if(repeat){
Log.i(TAG,"Repeat alarm is checked");
spe.putBoolean("repeatAlarm", true);
//TimeUnit.MINUTES.toMillis( getInterval())
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), (1000 * 60 * 60 * 24), pendingIntent);
//textAlarmPrompt.setText("Repeat Alarm is set@ " + targetCal.getTime());
} else {
Log.i(TAG,"Repeat alarm is NOT checked");
spe.putBoolean("repeatAlarm", false);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
//textAlarmPrompt.setText("One time Alarm is set @ " + targetCal.getTime());
}
spe.commit();
updateAlarmManagerData();
}
private void cancelAlarm(){
//textAlarmPrompt.setText("Cancelled Alarm");
ComponentName receiver = new ComponentName(getBaseContext(), AlarmReceiver.class);
PackageManager pm = getBaseContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmTitleView.setText("Alarm Cancelled!!!");
spe.putBoolean("repeatAlarm", false);
spe.commit();
spe.putBoolean("alarmSet", false);
spe.commit();
updateAlarmManagerData();
}
public int getInterval(){
int days = 1;
int hours = 24;
int minutes = 60;
// int seconds = 60;
//int milliseconds = 1000;
int repeatMS = days * hours * minutes;
return repeatMS;
}
public void updateAlarmManagerData() {
repeatAlarm = sp.getBoolean("repeatAlarm", false);
alarmSet = sp.getBoolean("alarmSet", false);
if(alarmSet == true) {
Log.i(TAG, "alarm is set");
hour = sp.getInt("ALARM_HOUR", 0);
min = sp.getInt("ALARM_MIN", 0);
if(repeatAlarm == true) {
Log.i(TAG, "repeat alarm is set");
if(min < 10) {
alarmTitleView.setText("Alarm Set for: <" + hour + ":0" + min + "> (Repeat)");
} else {
alarmTitleView.setText("Alarm Set for :<" + hour + ":" + min + "> (Repeat)");
}
} else {
Log.i(TAG, "One time alarm is set");
if(min < 10) {
alarmTitleView.setText("Alarm Set for: <" + hour + ":0" + min + ">");
} else {
alarmTitleView.setText("Alarm Set for :<" + hour + ":" + min + ">");
}
}
} else {
Log.i(TAG, "alarm is NOT set");
alarmTitleView.setText("Alarm is not set");
}
}
}
Below is alarm receiver code: AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
// Notification ID to allow for future updates
public static final String TAG ="LOGTAG";
private static final int MY_NOTIFICATION_ID = 1;
SharedPreferences sp;
SharedPreferences.Editor spe;
boolean repeatAlarm;
boolean alarmSet;
@SuppressLint("NewApi")
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
CharSequence tickerText = "Time to readmessage";
CharSequence contentTitle = "Hi";
CharSequence contentText = "Its Time.";
Intent mNotificationIntent = new Intent(context, MainActivity.class);
PendingIntent mContentIntent = PendingIntent.getActivity(context, 0, mNotificationIntent, 0);
Notification.Builder notificationBuilder = new Notification.Builder(
context)
.setTicker(tickerText)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(mContentIntent);
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
// Pass the Notification to the NotificationManager:
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MY_NOTIFICATION_ID, notification);
sp = PreferenceManager.getDefaultSharedPreferences(context);
//sp = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
spe = sp.edit();
repeatAlarm = sp.getBoolean("repeatAlarm", false);
if(repeatAlarm == false) {
Log.i(TAG, "Alarmreceiver: repeatAlarm is flase clearing alarmSet value.");
spe.putBoolean("alarmSet", false);
} else {
Log.i(TAG, "Alarmreceiver: repeatAlarm is true.");
}
spe.commit();
if(intent == null) {
Log.i(TAG, "intent is null in alarm receiver");
}
if(intent.getAction() == null) {
Log.i(TAG, "intent action is null in alarm receiver");
} else {
Log.i(TAG, "intent action: " + intent.getAction());
}
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
if(repeatAlarm) {
// Setting the alarm here
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
int hour = sp.getInt("ALARM_HOUR", 0);
int min = sp.getInt("ALARM_MIN", 0);
Log.i(TAG, "hour " + hour + "min " + min);
calSet.set(Calendar.HOUR_OF_DAY, hour);
calSet.set(Calendar.MINUTE, min);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), (1000 * 60 * 60* 24), pendingIntent);
}
}
}
}
0 comments:
Post a Comment