mercredi 14 janvier 2015

Open Android Notifications on Watch and Phone on Repeating Time and Week Day from Database

I'm currently working on an application, which allows me to show notifications either on my smartphone or smartwatch (android wear). By saving a specific time (timepicker), a description (edittext) and the weekdays (toggle buttons) the data will be saved to the database.


Set a new notification


My problem is to show the right notification at the right time, which was created before. Saving to the database works, but showing the notification on the right time doesn't work. I tried the solution with the AlarmManager many times. But I'm not very sure how to work with it. My database looks like:



public static final String KEY_ID_ACTION = "id";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_HOUR = "time_hour";
public static final String KEY_MINUTE = "time_minute";
public static final String KEY_MONDAY = "monday";
public static final String KEY_TUESDAY = "tuesday";
public static final String KEY_WEDNESDAY = "wednesday";
public static final String KEY_THURSDAY = "thursday";
public static final String KEY_FRIDAY = "friday";
public static final String KEY_SATURDAY = "saturday";
public static final String KEY_SUNDAY = "sunday";


And my AlarmManagerBroadcastReceiver looks like this:



public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
DatabaseHandler db;

@Override
public void onReceive(Context context, Intent intent) {
db = new DatabaseHandler(context);

PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
// Acquire the lock
wl.acquire();

// You can do the processing here.
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();

if (extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)) {
// Make sure this intent has been sent by the one-time timer button.
msgStr.append("One time Timer : ");
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
msgStr.append(formatter.format(new Date()));

Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();

// Release the lock
wl.release();
}

public void SetAlarm(Context context, String hour, String minute) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance(Locale.GERMAN);
cal.set(Calendar.HOUR, Integer.valueOf(hour));
cal.set(Calendar.MINUTE, Integer.valueOf(minute));
cal.set(Calendar.SECOND, 0);
//cal.set(Calendar.DAY_OF_WEEK, 1);
cal.set(Calendar.DAY_OF_WEEK, 3);

Long alarmTime = cal.getTimeInMillis();

Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

// After after 5 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime,
1000 * 5, pi);
}

public void CancelAlarm(Context context) {
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent
.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}

public void setOnetimeTimer(Context context) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
}


With following code, I can set an alarm and also the toast will be shown.



public void onetimeTimer(View view) {
Context context = getActivity().getApplicationContext();
if (alarm != null) {
alarm.setOnetimeTimer(context);
} else {
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}


Also the usage of the weekdays works already, but it's only possible to create one notification, which doesn't even take the data from the database, and also that one notification won't be repeated weekly. I don't know how to stop the alarm for the notification after I deleted it from the database.


Aucun commentaire:

Enregistrer un commentaire