3

I am making an application with a feature of alarms in it. I am using service for this which keeps checking the current time of device against the times in my DB.

My problem is that this service stops if the app removed from the background or if the device is rebooted. I have used START_STICKY to keep it running in background and used a broadcast receiver to start it on reboot.

The major concern is that whatever I have coded is working on a MOTO G device. Reboot, clearing from background, everything, the service is running fine. But in Xiomi phones and Huawei Honour, It stops once cleared from background or rebooted.

The Service code:

public class RemindService extends Service {

final long delayMillis=500;
Handler h=null;
Runnable r;
SharedPreferences sp;

PendingIntent pendingIntent;

private static final int NOTIFY_ME_ID=1337;
@Override
  public void onCreate() {

    h=new Handler(Looper.getMainLooper());
}


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

@Override
public int onStartCommand(Intent intent, int flags,int startId) {



    r = new Runnable() {

           public void run() {


          //SOME OF MY IF-ELSE CONDITIONS


            Intent myIntent = new Intent(RemindService.this, ReminderPopUp.class);
            int randomPIN = (int)(Math.random()*9000)+1000;
            pendingIntent = PendingIntent.getActivity(RemindService.this, randomPIN, myIntent,PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmManager = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC|AlarmManager.RTC_WAKEUP, System.currentTimeMillis() , pendingIntent);



            h.postDelayed(this, delayMillis);

        }
      };

    h.post(r);
    return Service.START_STICKY;

}
@Override
  public void onDestroy() {
    h.removeCallbacks(r);

}

}

My Manifest declarations:

<service
        android:name="test.aguai.medieazy.RemindService"
        android:enabled="true" />

    <intent-filter>
        <action android:name="test.aguai.medieazy.START_SERVICE" />
    </intent-filter>

Has anybody else faced this problem? I think it is a problem of modified OS, but anyways my app is not working properly. Please Help.

Anusha Harish
  • 382
  • 3
  • 14
Prakhar
  • 710
  • 6
  • 24

3 Answers3

2

Rather than poll the device database constantly, I would make use of the AlarmManager service as I described in this answer:

Android Polling from a Server periodically

Set up the alarm to fire at the first scheduled time. When it fires, set up the next time and so on. There is no need to set up every alarm at once as only one can ever fire at a time.

When the alarm fires, you can start a service to perform whatever task you need (including the setting of the next alarm)

Community
  • 1
  • 1
Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • But I can not be sure, when will it ring next time, what the next alarm time will be.. as it is checking after a fixed time period – Prakhar Jun 29 '15 at 05:51
  • I f you don't know when the next alarm will be, exactly what are you checking in your service to know that you need to "fire" your alarm event? At some point, you must be setting the time you want to fire an alarm. This is where you set the AlarmManager. – Kuffs Jun 29 '15 at 05:54
  • ok I got it.. but the problem is that my service gets stopped.. So how will it set the alarm manager. – Prakhar Jun 29 '15 at 05:56
  • Alarm Manager does not need a service to be running. You just set the alarm and it will fire at the scheduled time. You can then do whatever you need to do. but most likely you will want to start a service to perform a task (which would exit as soon as the task is complete) Reads the docs here: http://developer.android.com/reference/android/app/AlarmManager.html – Kuffs Jun 29 '15 at 06:00
  • ok.. so how do I do it.. I think in my service also I have used alarm manager only.. and If i clear it from background, will it work? – Prakhar Jun 29 '15 at 06:23
  • The documentation contains everything you need – Kuffs Jun 29 '15 at 06:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81836/discussion-between-prakhar-and-kuffs). – Prakhar Jun 29 '15 at 07:06
0

Try Lik this it will Work

// for Every 6 minutes exact repeating service
                    Intent myIntent2 = new Intent(sign_in.this,MyAlarmService.class);
                    pendingintent3 = PendingIntent.getService(sign_in.this, 2,myIntent2, 2);
                    AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
                    Calendar calendar2 = Calendar.getInstance();
                    calendar2.setTimeInMillis(System.currentTimeMillis());
                    calendar2.add(Calendar.SECOND, 30);

                    alarmManager2.set(AlarmManager.RTC_WAKEUP,calendar2.getTimeInMillis(), pendingintent3);

                    alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,calendar2.getTimeInMillis(), 360 * 1000,pendingintent3);

manifest permission

 <!-- Web data Sync Service -->
        <service android:name="com.example.MyAlarmService" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </service>
Android Dev
  • 421
  • 8
  • 26
  • But I don't want to repeat because I am working on alarms.. If alarm comes once and again rings after every 5 minutes, It will be very annoying. – Prakhar Jun 29 '15 at 05:44
0

If you want your service to be running until explicitly stopped then consider calling startService(), to start the service. This allows the service to run indefinitely, and also allow a client to bind to the service by calling bindService().

Remember you must explicitly stop the service, by calling stopSelf() or stopService().

codeFreak
  • 353
  • 1
  • 3
  • 15