I working on alarm application. I tried to implement alarm notification and got notifications for every day but I need to set alarm for 7 days, after that it has to cancel. I cancelled it in broadcast receiver whenever seven days out, but still I'm getting notifications after seven days also.
public class MyTest extends AppCompatActivity {
AlarmManager alarmManager;
SharedPreferences preferences;
SharedPreferences.Editor editor;
PendingIntent pendingIntent;
int RequestCode =777;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AlarmReciever alarm = new AlarmReciever();
// alarm.setAlarm(this);
int alarmId = 0;
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(MyTest.this, AlarmReciever.class); // AlarmReceiver1 = broadcast receiver
alarmIntent.putExtra("alarmId", alarmId);
pendingIntent = PendingIntent.getBroadcast(MyTest.this, RequestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// alarmIntent.setData((Uri.parse("custom://" + System.currentTimeMillis())));
Calendar alarmStartTime = Calendar.getInstance();
Calendar now = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 00);
alarmStartTime.set(Calendar.MINUTE, 00);
alarmStartTime.set(Calendar.SECOND, 0);
Log.d("Alarm", now.toString());
/// int count= 0;
// last i added for comparision
if (now.after(alarmStartTime)) {
//second
Log.d("Alarm", "Added a day");
alarmStartTime.add(Calendar.DATE, 1);
// count++;
}
/* System.out.println(count);
if(count==7){
alarmManager.cancel(pendingIntent);
}*/
// for(int i=0;i<7;i ++) {
// alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis() + 1000 * 60 * 60 * 24 /*AlarmManager.INTERVAL_DAY*/, pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis() , AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("Alarm", "Alarms set for everyday 8 am.");
// }
}
}
Broadcast Receiver
public class AlarmReciever extends BroadcastReceiver {
SharedPreferences preferences;
SharedPreferences.Editor editor;
int count=0;
AlarmManager alarmManager;
PendingIntent pendingIntent;
int RequestCode =777;
@Override
public void onReceive(Context context, Intent intent) {
// String rec = intent.getDataString();
// Log.d("Alarm",rec);
Intent service1 = new Intent(context, NotificationService.class);
// service1.setData((Uri.parse("custom://" + System.currentTimeMillis())));
context.startService(service1);
count++;
preferences = context.getSharedPreferences("MyCount", Context.MODE_PRIVATE);
editor=preferences.edit();
editor.putInt("COUNT", count);
editor.commit();
incrementSum(context);
}
public void incrementSum(Context context){
int cou = preferences.getInt("COUNT",0);
// System.out.println(cou);
// int c=0;
// int cou = preferences.getInt("COUNT",0);
int co = preferences.getInt("C",0);
if(cou==1){
cou=co+1;
editor = preferences.edit();
editor.putInt("C",cou);
editor.commit();
}
int c = preferences.getInt("C",0);
// System.out.println("count"+cou);
// int count = preferences.getInt("COUNT",0);
// System.out.println("last"+count);
System.out.println("sum" + c);
if(c>=7) {
Intent alarmIntent = new Intent(context, AlarmReciever.class);
pendingIntent = PendingIntent.getBroadcast(context, RequestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager!= null) {
alarmManager.cancel(pendingIntent);
}
// alarmManager.cancel(pendingIntent);
}
// AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
}
Notification service
public class NotificationService extends IntentService {
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
public NotificationService() {
super("testing.amaze.com.mytest");
}
@Override
protected void onHandleIntent(Intent intent) {
Context context = this.getApplicationContext();
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, ActivityTwo.class);
Bundle bundle = new Bundle();
bundle.putString("test", "test");
mIntent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notification = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
//.setSmallIcon(R.drawable.ic_launcher)
//.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker("ticker value")
.setAutoCancel(true)
.setPriority(8)
// .setSound(soundUri)
.setContentTitle("Notif title")
.setContentText("Text").build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xFFFFA500;
notification.ledOnMS = 800;
notification.ledOffMS = 1000;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.i("notif", "Notifications sent.");
}