我有一個三星Galaxy S7,我想要觸摸心臟傳感器(靠近相機的手機),應用程序讀取心率並將其顯示到TextView中。Java Android - 創建一個簡單的心率監測器
我試過了,但沒有運行。
我的活動
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.SensorEventListener;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements SensorEventListener{
public TextView testoLabel;
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e(TAG, "onCreate called");
testoLabel = (TextView) findViewById(R.id.testoLabel);
SensorManager sMgr;
sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
Sensor battito = null;
battito = sMgr.getDefaultSensor(Sensor.TYPE_HEART_RATE);
if(battito != null)
testoLabel.setText("load sensor");
else
testoLabel.setText("no load sensor");
sMgr.registerListener(this, battito,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
String msg = " Value sensor: " + (int)event.values[0];
testoLabel.setText(msg);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
System.out.println("onAccuracyChanged - accuracy: " + accuracy);
}
}
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.giuseppeaccardo.prova" >
<uses-permission android:name="android.permission.BODY_SENSORS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<uses-permission android:name="android.permission.BODY_SENSORS" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
這裏沒有足夠的信息繼續下去。你至少需要調試它併發布logcat跟蹤 –
好的...但是代碼運行,但手機的心臟傳感器沒有檢測到觸摸 –