-1

I'm trying to call google fit API in a background service, but when I clean the recent apps, the service stops. Maybe, use the alarm is the best way to do the service unstoppable, but it crash without leaving an error in the log. Is this the best way to do this? If not, which is the best way?

The class what extends from Service:

public class ServiceGFA extends Service {

    Alarm alarm = new Alarm();

    public void onCreate(){
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        alarm.setAlarm(this);
        return START_STICKY;
    }

    @Override
    public void onStart(Intent intent, int startId){
        alarm.setAlarm(this);
    }

    @Override
    public IBinder onBind(Intent intent){
        return null;
    }

}

The Alarm class:

public class Alarm extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    scs.execute();
}

public void setAlarm(Context context){
    scs = new stepscountservice();
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context,0,i,0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000*6*1, pi);
    Toast.makeText(context, "service started", Toast.LENGTH_SHORT).show();
}

public void cacelAlarm(Context context){
    Intent intent = new Intent(context, Alarm.class);
    PendingIntent sender = PendingIntent.getBroadcast(context,0,intent,0);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}

private class stepscountservice extends AsyncTask<Object, Object, Long> {

    boolean cent;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        cent = true;
    }

    @Override
    public Long doInBackground(Object... params) {
        while (cent) {
            long total = 0;
            com.google.android.gms.common.api.PendingResult<DailyTotalResult> result = Fitness.HistoryApi.readDailyTotal(mApiClient, DataType.TYPE_STEP_COUNT_DELTA);
            DailyTotalResult totalResult = result.await(1, TimeUnit.SECONDS);
            if (totalResult.getStatus().isSuccess()) {
                DataSet totalSet = totalResult.getTotal();
                if (totalSet != null) {
                    total = totalSet.isEmpty() ? 0 : totalSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS).asInt();
                    pasos = total;
                }
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Object... values) {
        super.onProgressUpdate(values);
        Toast.makeText(context, "Steps:" + ""+values[0], Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        cent = false;
    }
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
zasaz
  • 2,404
  • 2
  • 15
  • 27

1 Answers1

0

You don't have to use AsyncTask here as the service always runs in a background thread. Just move all the code from your doInBackground method to the onReceive method.

public class UpdateFitnessData extends Service {

    private static final String TAG = "Service : FitnessData";

    private final int TIME_DELAY_15_MINUTES = 900000;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand: ");
    begin();
    setAlarm();
    return Service.START_STICKY;
}

private void begin() {
    Log.d(TAG, "begin: ");
    //Put your doInBackground Code here

}


private void setAlarm() {
    Intent intent = new Intent(this, UpdateFitnessData.class);

    PendingIntent pendingIntent = PendingIntent.getService(this , 0, intent, PendingIntent.FLAG_ONE_SHOT);

    //Following code will restart your application after 2 seconds
    AlarmManager mgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + TIME_DELAY_15_MINUTES, pendingIntent);
}

}
  • Thaks for the answer, I really don't know if this will work, however, i could solve the problem, using an IntentService. Thanks anyway! – zasaz Jan 26 '17 at 16:00