【问题标题】:Google Fit can't be read in background?Google Fit 无法在后台读取?
【发布时间】:2018-07-13 14:07:40
【问题描述】:

我正在尝试在我的应用中读取 Google Fit 数据。我已经编写了代码并且效果很好。我创建了一个 AlarmMangaer 并将读取的拟合数据放在那里,但似乎即使正在调用警报,除非应用程序打开并在前台,否则不会读取拟合数据。这是我的代码:

public class MainActivity extends AppCompatActivity  implements OnDataPointListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

private static final int REQUEST_OAUTH = 1;
private static final String TAG = "FITTEST";
AlarmManager alarmMgr;
PendingIntent pendingIntent;


private static final String AUTH_PENDING = "auth_state_pending";
private boolean authInProgress = false;

public static GoogleApiClient mClient = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState != null) {
        authInProgress = savedInstanceState.getBoolean(AUTH_PENDING);
    }

    buildFitnessClient();
}

private void buildFitnessClient() {
    // Create the Google API Client
    mClient = new GoogleApiClient.Builder(this)
            .addApi(Fitness.HISTORY_API)
            .addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
            .addConnectionCallbacks(this)
            .enableAutoManage(this, 0, this)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    // Connect to the Fitness API
    Log.i(TAG, "Connecting...");
    mClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mClient.isConnected()) {
        mClient.disconnect();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_OAUTH) {
        authInProgress = false;
        if (resultCode == RESULT_OK) {
            // Make sure the app is not already connected or attempting to connect
            if (!mClient.isConnecting() && !mClient.isConnected()) {
                mClient.connect();
            }
        }
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

private void registerFitnessDataListener(DataSource dataSource, DataType dataType) {

    SensorRequest request = new SensorRequest.Builder()
            .setDataSource( dataSource )
            .setDataType( dataType )
            .setSamplingRate( 3, TimeUnit.SECONDS )
            .build();

    Fitness.SensorsApi.add( mClient, request, (OnDataPointListener) this)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    if (status.isSuccess()) {
                        Log.e( "GoogleFit", "SensorApi successfully added" );
                    }
                }
            });
}

@Override
public void onDataPoint(DataPoint dataPoint) {
    for( final Field field : dataPoint.getDataType().getFields() ) {
        final Value value = dataPoint.getValue( field );
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "Field: " + field.getName() + " Value: " + value, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Toast.makeText(this, "OnConnected", Toast.LENGTH_SHORT).show();

    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
    startAlarmManager();
}

public void startAlarmManager() {
    Intent dialogIntent = new Intent(getBaseContext(), AlarmReceiver.class);

    alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    pendingIntent = PendingIntent.getBroadcast(this, 0, dialogIntent,PendingIntent.FLAG_CANCEL_CURRENT);

    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), 10000, pendingIntent);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

这里是警报服务:

public class AlarmReceiver extends BroadcastReceiver {

String steps = "";
private GoogleApiClient mGoogleApiClient = MainActivity.mClient;
private Context mContext;

@Override
public void onReceive(Context context, Intent intent) {
    this.mContext = context;
    Log.d("ALARM", "JR Alarm Service running");
    sendNotification("JR Fitness Alarm!!");

    new ViewTodaysStepCountTask().execute();
}

private void displayStepDataForToday() {
    if (mGoogleApiClient != null ) {
        DailyTotalResult result = Fitness.HistoryApi.readDailyTotal(mGoogleApiClient, DataType.TYPE_STEP_COUNT_DELTA).await(1, TimeUnit.MINUTES);
        showDataSet(result.getTotal());
    } else {
        Toast.makeText(mContext, "Google Client Empty", Toast.LENGTH_SHORT).show();
    }
}

private void showDataSet(DataSet dataSet) {
    Log.e("History", "Data returned for Data type: " + dataSet.getDataType().getName());
    DateFormat dateFormat = DateFormat.getDateInstance();
    DateFormat timeFormat = DateFormat.getTimeInstance();
    for (DataPoint dp : dataSet.getDataPoints()) {
        for(Field field : dp.getDataType().getFields()) {
            if (field.getName().equals("steps")) {
                String steps = dp.getValue(field).toString();
                if (! steps.isEmpty()) {
                    Log.e("Alarm Receiver History", "\tField: " + field.getName() +
                            " Value: " + dp.getValue(field));

                    Log.e ("Alarm", "Steps Count: " + steps);
                    ServerIntf serverIntf = new ServerIntf();
                    serverIntf.SendStepCount(mContext, steps);
                }
            }
        }
    }
}

private class ViewTodaysStepCountTask extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        displayStepDataForToday();
        return null;
    }
    protected void onPostExecute(Long result) {
        Log.e ("STEPS", "Steps returned");
    }
}
public void sendNotification(String text) {


    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(mContext)
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setContentTitle("My notification")
                    .setContentText("Hello World! " + text );



    NotificationManager mNotificationManager =

            (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    mNotificationManager.notify(002, mBuilder.build());
}
}

【问题讨论】:

    标签: android alarmmanager google-fit background-service android-googleapiclient


    【解决方案1】:

    您的 google api 客户端很可能为 null,因为您将客户端作为静态变量引用,但在警报管理器启动您的服务时,它很可能为 null 或断开连接。您需要在服务中启动您的客户端并使用该实时客户端。这里是你需要寻找修复的地方:

    private GoogleApiClient mGoogleApiClient = MainActivity.mClient;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多