How to stop getting notification on every launch of application in android? - android

in my application I have set the notification inside onCreate() of launcher activity. I am getting result successfully but as it is written inside the onCreate it is showing notification every time I launch app also.
How should I resolve this, I want the notification only on given time.
Below is the code I am using :-
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 49);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(Main.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Main.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

Create a new method and call the method when you want your notification to launch.
public void showNotification() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 49);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(Main.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Main.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
Now when you want to call this method simple write:
showNotification();
Hope this helps.

Related

How to cancel the alarm manager daily notifications?

In my app I use alarm manager to daily show notifications, but I want to create an option for users to cancel the daily notifications.On user click the working alarm manager has to permanently stop.
How to achieve this?
This is the method that is being called on app installation and then notification starts appearing daily, how to cancel it permanently?
public void AlaramNotification() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(StartActivity.this, AlarmReceiver.class);
PendingIntent broadcast = PendingIntent.getBroadcast(StartActivity.this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
cal = Calendar.getInstance();
Calendar now = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 15);
cal.add(Calendar.SECOND, 0);
cal.set(Calendar.AM_PM,Calendar.AM);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (now.after(cal)) {
Log.d("Hey","Added a day");
cal.add(Calendar.DATE, 1);
}
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24*60*60*1000, broadcast);
}
else {
}
}
You can try this way:
AlarmManager alarmManager =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(StartActivity.this, AlarmReceiver.class);
PendingIntent broadcast = PendingIntent.getActivity(StartActivity.this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(broadcast);
When user cancels daily notification use cancel() on alarmManager with pendingintent as shown below to stop alarmManager.
alarmManager.cancel(broadcast);
You may need to make broadcast a global variable.

How to trigger a background service in android on specific time?

I know how to trigger a background service in android even on specific time. I have used AlarmManager to achieve this, but I want to start that service daily on that time.
Suppose I want to start it on 12pm, but the service should keep going on after that. I have also used the stopitself() method, but that does not work.
Here is my code:
void alram(Context ctx){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.AM_PM,Calendar.AM);
Intent serviceIntent = new Intent(ctx, ServiceClass.class);
PendingIntent servicePendingIntent =
PendingIntent.getService(ctx,Service_Apps.SERVICE_ID,serviceIntent,0);
AlarmManager alarm = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),servicePendingIntent);
alarm.setRepeating(
AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
servicePendingIntent
);
}
Hi Please use this code it will help to fill your desires.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(this, 0,
new Intent(this, Service_Apps.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);

AlarmManager going off before Set Time

I'm having an issue with my AlarmManger:
The following correctly adds one day (because the time 9:25 has passed) the output is correct yet the AlarmManager calls the Broadcast within a couple seconds. Here's the relevant snippit. Any ideas?
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 21);
cal.set(Calendar.MINUTE, 25);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(this, NotificationReceiver.class);
NotificationReceiver instance = new NotificationReceiver();
intent.setAction(instance.NOTIFICATION_SEND);
intent.putExtra("TYPE",instance.NOTIFICATION_TYPE_PROGRESS);
intent.putExtra("Ticker","Waiting for arrival.");
intent.putExtra("ID",1);
intent.putExtra("title","Check-In");
intent.putExtra("message","A message");
if(System.currentTimeMillis()>cal.getTimeInMillis()){
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY||cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
cal.add(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
Log.d("ALARMS","Set for monday.");
}
else{
cal.add(Calendar.DATE,1);
Log.d("ALARMS","Set for tomorrow. "+cal.get(Calendar.DAY_OF_MONTH));
}
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 24332, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),4000,pendingIntent);
Resolved:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 24, intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager not starting up

Intent myIntent = new Intent(taskiller.this, taskiller.class);
pendingIntent = PendingIntent.getService(taskiller.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//Set taskiller at 20secs
calendar.add(Calendar.SECOND, 20);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() , pendingIntent);
Log.i("Securitas", "Set alarm to be triggered");
I want my service to restart itself after a certain time interval but it doesnt seem to happen.
Can anybody please advice?

android how to set the phone alarm through code

I want to set the alram for phone through program coding. I have an button where by clicking DatePicker come & also time picker. I want what time i set here that time should be set in phones alaram. How can I do that . plz give me the answer . Thank you
Please try this....
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, 0);
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 01);
cal.set(Calendar.SECOND, 0);
Intent intent = new Intent(Alarm.this, Alarm1.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);
PendingIntent sende2 = PendingIntent.getBroadcast(this, 123123, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sende2);
Intent intent1 = new Intent(Alarm.this, Alarm1.class);
PendingIntent sender1 = PendingIntent.getBroadcast(this, 1234567, intent1, 0);
AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
am1.cancel(sender1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Alarm a = new Alarm ();
a.setAlarm();
b1.setText(prod);
}
});

Categories

Resources