Reputation: 600
I am trying to read Google Fit data in my app. I have written the code and it works great. I created an AlarmMangaer and put the fit data read in there but it seems like even though the alarm is being called, the fit data is not read unless the app is open and in the foreground. Here is the code I have:
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) {
}
Here is the Alarm Service:
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());
}
}
Upvotes: 0
Views: 617
Reputation: 7628
Your google api client is most likely null because you are referencing your client as a static variable, but it is most likely null or disconnected by the time the alarm manager starts your service. You need to start your client in the service and use that live client. Here is where you need to look to make the fix:
private GoogleApiClient mGoogleApiClient = MainActivity.mClient;
Upvotes: 2