我编写了代码来读取 android 中的 CPU 温度。但是出现这样的错误
java.io.FileNotFoundException: /sys/devices/virtual/thermal/thermal_zone0/temp: open failed: EACCES (Permission denied)
private double getCurrentCPUTemperature() throws IOException, InterruptedException {
ArrayList <Float> suhu = new ArrayList<>();
Process process;
String line;
BufferedReader reader;
RandomAccessFile reader1;
for (int i = 0; i <= 30 ; i ++ ) {
process = Runtime.getRuntime().exec("cat /sys/devices/virtual/thermal/thermal_zone" + i + "/temp");
process.waitFor();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
line = reader.readLine();
System.out.println("Line = " + line);
if(line != null){
Float temp = Float.parseFloat(line);
suhu.add(temp);
}
if(line == null){
reader1 = new RandomAccessFile("/sys/devices/virtual/thermal/thermal_zone" + i +"/temp", "r");
line = reader.readLine();
if (line != null){
System.out.println(line + " RAM");
Float temp1 = Float.parseFloat(line);
suhu.add(temp1);
}
}
if (i == 30){
reader.close();
process.destroy();
}
}
double temp = 0.0;
for (int i = 0; i < suhu.size() ; i++){
if (suhu.get(i) > temp){
temp = (double) suhu.get(i);
}
}
System.out.println(suhu);
return temp;
}
我有读写外部存储的请求权限。 清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.gpc1">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
在活动中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setSelectedItemId(R.id.page_2);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
requestPermissionStorage();
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == STORAGE_PERMISSION_CODE){
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
requestPermissionStorage();
} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
}
}
}
private void requestPermissionStorage() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
else {
System.out.println("Request External Storage");
}
}
谢谢,感谢您的帮助
答案 0 :(得分:0)
您可以将 TYPE_AMBIENT_TEMPERATURE 用于电池或 CPU 温度。 TYPE_TEMPERATURE 是已弃用的常量。
文档中可用代码的修改版本应该是这样的:
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class TempSensorActivity extends Activity, implements SensorEventListener {
private final SensorManager mSensorManager;
private final Sensor mTempSensor;
public TempSensorActivity() {
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mTempSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mTempSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
}
}
查看sensor documentation了解更多