Search code examples
androidandroid-servicealarmmanager

Alarm Manager shows notification everytime


I have made an android application that displays a notification message at a particular time (say 6:15:00 pm) of the day. I have used the Calendar along with AlarmManager class for this purpose.

However, when I installed the app on my phone to test it, it showed me the message/notification at the specified time but continues to show the same at any time..I mean the notification keeps on coming throughout the day in my phone even though I have set the time for it in my app.

Here is my code 1) Activity class

import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;


public class MainActivity extends Activity 
 { 

  private PendingIntent pendingIntent;

@Override
public void onCreate(Bundle savedInstanceState) 
{

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.DAY_OF_MONTH, 13);

calendar.set(Calendar.HOUR_OF_DAY, 20);
calendar.set(Calendar.MINUTE, 48);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);

Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

} //end onCreate

 }

2) Receiver

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;

   public class MyReceiver extends BroadcastReceiver
    {

@Override
 public void onReceive(Context context, Intent intent)
{
   Intent service1 = new Intent(context, MyAlarmService.class);
   context.startService(service1);

   }

 }

3) Service class

   import android.app.Notification;
   import android.app.NotificationManager;
   import android.app.PendingIntent;
   import android.app.Service;
   import android.content.Intent;
   import android.os.IBinder;


 public class MyAlarmService extends Service 

  {
 private NotificationManager mManager;

 @Override
 public IBinder onBind(Intent arg0)
 {
   // TODO Auto-generated method stub
    return null;
 }

@Override
public void onCreate() 
{
   // TODO Auto-generated method stub  
   super.onCreate();
}

   @SuppressWarnings("static-access")
    @Override
  public void onStart(Intent intent, int startId)
   {
   super.onStart(intent, startId);

   mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
   Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

   Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());

   intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

   PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
   notification.flags |= Notification.FLAG_AUTO_CANCEL;
   notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

   mManager.notify(0, notification);
}

@Override
public void onDestroy() 
{
    // TODO Auto-generated method stub
    super.onDestroy();
}

}

I have added both service and receiver in manifest.xml file. Please can anyone tell me what should I do to display the notification only once during the entire day?


Solution

  • I guess the reason was the onCreate method was called multiple times and so your alarmManager.set was called multiple times. For example, whenever the screen orientation is changed, that activity is destroyed and a new activity starts by onCreate() method. So every time you rotate the screen that activity will be destroyed and a new activity starts by onCreate() method and alarmManager.set was called again.

    You may save a state member in onSaveInstanceState(Bundle bundle) method. Android calls this method whenever the screen is rotated, and that given bundle bundle would be passed to oncreate(Bundle bundle). In your onCreate, check the Bundle is null before try to call alarmManager.set.