Reputation: 73
I have an Alarm Manager that puts the current Time in a message and outputs that message in a Toast Message.
What I want to do is to output a notification instead of a toast message and have been struggling to do so.
The Alarm Manager works. The Notification also works currently when it is pressed by a button (currently unconnected to the alarm manager).
Here is my button that is getting the toast from the Broadcast Receiver:
public void onetimeTimer(View view){
Context context = this.getApplicationContext();
if(alarm != null){
alarm.setOnetimeTimer(context);
}else{
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}
Here is my Alarm Manager Broadcast Receiver:
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
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 update the widget/remote views.
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();
if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){
msgStr.append("One time Timer : ");
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
// msgStr.append(formatter.format(new Date()));
//msgStr.append();
Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
//Release the lock
wl.release();
}
Here is the method in the Broadcast receiver:
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);
}
Here is the Notification that is working seperately behind a different button that I would like to be set off by the alarm manager button being pressed.
Button btnNotifyWorkout = (Button)findViewById(R.id.btnNotifyWorkout);
btnNotifyWorkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(HomeActivity.this, 0, intent, 0);
Notification noti = new Notification.Builder(HomeActivity.this)
.setTicker("TickerTitle")
.setContentTitle("Lukes App")
.setContentText("You have a workout due today")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, noti);
}
});
Upvotes: 0
Views: 922
Reputation: 336
Try putting your notification code inside "onReceive" in Alarm manager receiver
But with a few tweaks:
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification noti = new Notification.Builder(context)
.setTicker("TickerTitle")
.setContentTitle("Lukes App")
.setContentText("You have a workout due today")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).getNotification();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(0, noti);
Upvotes: 1