NOTES in the below diagram is SELF-EXPLANTORY
MainActivity.java
package isquarebsys.training;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import broadcast.UtilityReceiver;
public class MainActivity extends AppCompatActivity {
private final static String TAG="MainActivity";
private int alarmNotificationId=1;
long timeForAlarmInMilliSecs=10;
long timeToRepeatAlarmInMilliSecs=5;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendNotification(View view){
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Notifications Example")
.setContentText("This is a test notification");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
builder.addAction(new NotificationCompat.Action(R.mipmap.ic_launcher,"Cancel",contentIntent));
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(uri);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("Big View Styles"));
// Add as notification NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
/** * https://stackoverflow.com/questions/30075196/how-can-i-cancel-unshown-notifications-in-android * @param view */
public void setAlarm(View view){
Intent notificationIntent = new Intent(getApplicationContext(), UtilityReceiver.class);
// Use a Pending Intent to Launch the Alarm // alarmNotificationId is the key to cancel the original alarm PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
alarmNotificationId, notificationIntent, 0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
// alarmManager.set(AlarmManager.RTC_WAKEUP,timeForAlarmInMilliSecs, notificationPendingIntent); // Schedule in 5 secs and repeat every 5 secs alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + timeToRepeatAlarmInMilliSecs,
timeToRepeatAlarmInMilliSecs,notificationPendingIntent);
}
/** * 1. https://stackoverflow.com/questions/30075196/how-can-i-cancel-unshown-notifications-in-android [does NOT WORK] * 2. http://android-er.blogspot.in/2012/05/cancel-alarm-with-matching.html [Works] * * Alarm CAN NOT be cancelled after it happened. It can be CANCELLED only before it happens but AFTER scheduling * To TEST, click sendAlarm and cancel before it happens * @param view */ public void cancelAlarm(View view){
Intent notificationIntent = new Intent(getApplicationContext(), UtilityReceiver.class);
// Use a Pending Intent to Launch the Alarm // alarmNotificationId is the key to cancel the original alarm PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
alarmNotificationId, notificationIntent, 0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(notificationPendingIntent);
}
}
UtilityReceiver.java
package broadcast;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import isquarebsys.training.R;
public class UtilityReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
int notificationId = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Alarm Notification")
.setContentIntent(pendingIntent)
.setContentText("Alarm")
.setAutoCancel(true);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(uri);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());
}
}
Comments
Post a Comment