安卓系统后台操作系统检索步数



我正在尝试从智能手表中检索步数,并将其推送到API。当我打开应用程序时,我能够检索并推送数据。但一旦它没有被激活,它就不会发送任何数据。我正在尝试使用android服务在后台运行该应用程序,以便它能够连续发送数据。我已授予所有权限并启用了它们。

这是MainActivity.java

package com.example.stepcounter;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent; 
import android.os.Bundle;
public class MainActivity extends AppCompatActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onResume() {
super.onResume();
}
protected void onPause() {
super.onPause();
}
protected void onDestroy() {
super.onDestroy();
}
public void onPressStartService(View v){
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
public void onPressStopService(View v){
stopService(new Intent(getApplicationContext(), MyService.class));
}
}

这是MyService.java

package com.example.stepcounter;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class MyService extends Service implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
private String HelloData;
private TextView mTextView;
private boolean isSensorPresent;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mSensorManager = (SensorManager)this.getSystemService(Context.SENSOR_SERVICE);
if(mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE) != null) {
mSensor = mSensorManager.getDefaultSensor(69680);
isSensorPresent = true;
} else {
isSensorPresent = false;
}
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onSensorChanged(SensorEvent event) {
mTextView.setText("Heart Rate:  " + String.valueOf(event.values[0]));
HelloData = (String) String.valueOf(event.values[0]);
if(!HelloData.contains("0.0")){
postDataUsingVolley(HelloData);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
private void postDataUsingVolley(String ranData) {
String url = "https://test.com";
RequestQueue queue = Volley.newRequestQueue(this);
JSONObject postData = new JSONObject();
try {
postData.put("data", ranData);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, postData, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(jsonObjectRequest);
}
}

我还在AndroidManifest.xml 中添加了以下内容

<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>

它工作30秒,发送数据,一旦手表处于非活动状态,它就会停止发送数据。你知道这是怎么回事吗?

您需要在onPause:期间注销传感器

@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}

此外,如果注销,则需要使用boolean activityRunning

相关内容

  • 没有找到相关文章

最新更新