0

I could not explain my problem in the Title..Here's the detailed explanation of my problem :

I have an application where arrival time of trains are displayed.

I want to add a feature where User can create an alert if the train is 10-5 minutes away from station.

A form is displayed to create an alert (all the values are stored in DB).

 alertID = db.insert_alerts(name,starttime,endtime,booleanrepeat);

I am using alarm manager to create the notification :

if (is_repeat.equals("true")) {
     new AlertsReceiver().setRepeatAlarm(getApplicationContext(), mCalendar, (int)alertID ,10000);
 } else if (is_repeat.equals("false")) {
    new AlertsReceiver().setAlarm(getApplicationContext(), mCalendar,(int)alertID);
 }

below is the Receiver class :

public class AlertsReceiver extends WakefulBroadcastReceiver {

    private int alertId;
    AlarmManager mAlarmManager;
    PendingIntent mPendingIntent;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if(extras == null) {
            Log.d("Service","null");
        } else {

            alertId= extras.getInt("alertId");
        }


       Intent intent1 = new Intent(context,MService.class);
        intent1.putExtra("alertId", alertId);
        context.startService(intent1);


    }

    public void setAlarm(Context context, Calendar calendar, int ID) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(context,AlertsReceiver.class);
        intent.putExtra("alertId", ID);
        mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);


        // Calculate notification time
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime;

        // Start alarm using notification time
        mAlarmManager.set(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + diffTime,
                mPendingIntent);


    }

    public void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Log.e("asetRepeat",ID+"");
        Intent intent = new Intent(context, AlertsReceiver.class);
        intent.putExtra("alertId", ID);
        mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        // Calculate notification timein
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime;

        // Start alarm using initial notification time and repeat interval time
        mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + diffTime,
                RepeatTime , mPendingIntent);


    }

    public void cancelAlarm(Context context, int ID) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);


        Toast.makeText(context,"Cancelled"+ID,Toast.LENGTH_LONG).show();
        // Cancel Alarm using Reminder ID
        mPendingIntent = PendingIntent.getBroadcast(context, ID, new Intent(context, RiderAlertsReceiver.class), 0);
        mAlarmManager.cancel(mPendingIntent);

    }
}

In receiver I am starting a service where I am making a webservice call :

Once an alarm manager is set..A webservice call is made in background service to check the minutes in which train will arrive :

if the minutes < 10 minutes notification is displayed else the webservice call should run again

So, i place the webservice in timer which runs every 1 minute.

The issue is how to stop the timer once the notification is received.

Also, the every time the receiver is starting the same service so only one alert is working at one time.

The user can create multiple alerts and all alerts are stored in DB.

Please can any one tell me the best way to implement this feature.

Appreciate Help...!

anonymous
  • 321
  • 4
  • 16
  • Where is your timer code? `timerTask.cancel()` will stop the timer. – K Neeraj Lal Sep 10 '16 at 11:24
  • It is stopping the time,but if i create another alert simultaneously I am getting an exception which says timer is cancelled – anonymous Sep 10 '16 at 11:25
  • See here http://stackoverflow.com/questions/2098642/pausing-stopping-and-starting-resuming-java-timertask-continuously – K Neeraj Lal Sep 10 '16 at 11:35
  • Thanks for the link..but how can i dynamically create multiple timer tasks..because the user can create multiple alerts – anonymous Sep 10 '16 at 11:39
  • Also,every time I create an alert using alarm manager and broadcast receiver, the receiver starts the same service, is it the correct way? – anonymous Sep 10 '16 at 11:41

0 Answers0