【问题标题】:Call google fit api background service调用google fit api后台服务
【发布时间】:2017-06-08 07:57:07
【问题描述】:

我正在尝试在后台服务中调用 google fit API,但是当我清理最近的应用程序时,该服务会停止。也许,使用警报是使服务无法停止的最好方法,但它会崩溃而不会在日志中留下错误。这是最好的方法吗?如果没有,哪种方法最好?

从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;
    }

}

报警类:

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;
    }
}

【问题讨论】:

    标签: android google-fit background-service


    【解决方案1】:

    您不必在此处使用 AsyncTask,因为该服务始终在后台线程中运行。只需将所有代码从您的 doInBackground 方法移动到 onReceive 方法即可。

    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);
    }
    
    }
    

    【讨论】:

    • 感谢您的回答,我真的不知道这是否可行,但是,我可以使用 IntentService 解决问题。还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多