0

I have tried to set up an alarm that will give notification at a particular time of a particular date just for once.But it is giving notification continuously after the given time.And the notification is not working when the application is closed.But I want to run the notification process on background.Essential permissions have been added to the manifest and broadcast receiver also been used. The code is given below.

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, Calendar.SEPTEMBER);
    //calendar.set(Calendar.YEAR, 2016);
    calendar.set(Calendar.DAY_OF_MONTH, 15);

    calendar.set(Calendar.HOUR_OF_DAY, 22);
    calendar.set(Calendar.MINUTE, 30);
    calendar.set(Calendar.SECOND, 05);
    //calendar.set(Calendar.AM_PM,Calendar.PM);
    final int id = (int) System.currentTimeMillis();
    Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent= PendingIntent.getBroadcast(MainActivity.this, id, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), pendingIntent);
    // If the alarm has been set, cancel it.
    /*if (alarmManager!= null) {
        alarmManager.cancel(pendingIntent);
    }*/



} //end onCreate

}

Murtaza
  • 11
  • 3

5 Answers5

0

Instead of doing this alarm work in an activity, use a Service so it will run in the background without the need of a layout for GUI. This would solve your notification problem. You could start here

0

check this link Alarm Manager Example and replace this am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); by this am.set(AlarmManager.RTC_WAKEUP, 1000 * 60 * 10, pi);

Community
  • 1
  • 1
shubham goyal
  • 537
  • 3
  • 3
0
new java.util.Timer().schedule(
        new java.util.TimerTask() {
            @Override
            public void run() {
                //instert youre function here
            }
        },
        50000//or any time until it happend
);

hope this work for you...

0

Try this

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue Handler

Handler handler = new Handler();

    Runnable runnable = new Runnable() {
    public void run() {
       //your code 
    handler.postDelayed(runnable, 1000);
    }
};
handler.post(runnable);
Benjamin
  • 2,257
  • 1
  • 15
  • 24
0

Yes I was tested your code, notification not repeated.

  1. Alarm Manager hold the all alarms until restart the device. once restart the device (switch off and on), it releases the all alarms. So you should set the alarm additionally after "boot_completed".

    public class DeviceBootReceiver extends BroadcastReceiver {
    
    private static final String TAG = "DeviceBootReceiver";
    private Context mContext;
    
     @Override
     public void onReceive(Context context, Intent intent) {
     mContext = context;
     if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
     // set the Alarm again here...
    setReminderAlarm(context);
      }
     }
    
  2. you should register in the Android manifest File.

     <receiver  android:name="com.tcalender.calendartelugu.activity.DeviceBootReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
    </receiver>
    

3 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); should execute only once.

  1. Even we close the application it should work.
Kona Suresh
  • 1,836
  • 1
  • 15
  • 25