1

我想为 Wear OS 编写一个程序,每 30 分钟记录一次心率并将这些数据存储在 txt 文件中。我尝试使用 Job Scheduler,但它不能持续工作。事实上它只工作一次。我必须使用哪个服务或 API?应用程序将在后台运行,有时会检测到过高的心率。我正在使用华为手表 2 LEO-BX9 (API 25)

我的活动:

public class MainActivity extends WearableActivity {

    private TextView output;
    private int counter;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        output = (TextView) findViewById(R.id.output);

        getPermissions();
    }

    public void scheduleJob(View v) {
        ComponentName componentName = new ComponentName(this, ExampleJobService.class);
        JobInfo info = new JobInfo.Builder(123, componentName)
                //.setRequiresCharging(true)
                //.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
                .setPersisted(true)
                .setPeriodic(30 * 60 * 1000)
                .setRequiresBatteryNotLow(true)
                .build();
        JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
        int resultCode = scheduler.schedule(info);
        if (resultCode == JobScheduler.RESULT_SUCCESS) {
            Log.d(TAG, "Job scheduled");
        } else {
            Log.d(TAG, "Job scheduling failed");
        }
    }

    public void cancelJob(View v) {
        JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
        scheduler.cancel(123);
        Log.d(TAG, "Job cancelled");
    }

    public void getPermissions() {
        String[] permission1 = {Manifest.permission.BODY_SENSORS};
        if (ContextCompat.checkSelfPermission(this, permission1[0]) != 0) {

            ActivityCompat.requestPermissions(this, permission1, PermissionInfo.PROTECTION_DANGEROUS);
            Log.d("PERMISSIONS:", "requested, " + ContextCompat.checkSelfPermission(this, permission1[0]));
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1000);
        }
    }
}

我的服务:

public class ExampleJobService extends JobService implements SensorEventListener {

    int counter = 0;

    private static final String TAG = "ExampleJobService";
    private boolean jobCancelled = false;

    private SensorManager sensorManager;
    private Sensor hrSensor;

    private double heartRate;

    @Override
    public boolean onStartJob(JobParameters params) {
        Log.d(TAG, "Job started");

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        hrSensor = sensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);

        doBackgroundWork(params);
        return true;
    }

    private void doBackgroundWork(final JobParameters params) {
        sensorsOn();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 30; i++) {         // czas musi być powyżej 10 sekund aby czujnik się uruchomił
                    Log.d(TAG, "run: " + i);

                    measure();

                    if (jobCancelled) {
                        return;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                counter++;

                Log.d(TAG, "Job finished");
                Log.d(TAG, "counter: " + counter);
                sensorsOff();

                jobFinished(params, false);
            }
        }).start();
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d(TAG, "Job cancelled before completion");
        jobCancelled = true;
        return true;
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Sensor sensor = sensorEvent.sensor;

        if (sensor.getType() == Sensor.TYPE_HEART_RATE) {
            heartRate = Double.valueOf(sensorEvent.values[0]);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public void sensorsOn() {
        sensorManager.registerListener(this, hrSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void sensorsOff() {
        sensorManager.unregisterListener(this, hrSensor);
    }

    public void measure() {
        sensorsOn();
        Log.d(TAG, "hr: " + heartRate);
    }
}
4

0 回答 0