3

I made code to read cpu temperature in android. But there is an error like this

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; } 

i have make request permission for read and write external storage. Manifest :

<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" 

And in activity

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"); } } 

Thanks and i appreciate your help

2
  • no need to say sorry for your bad english, we're all here from different backgrounds :) Commented Feb 17, 2021 at 9:07
  • Thank You :D Nice to see nice people Commented Feb 17, 2021 at 9:09

2 Answers 2

1

I too couldn't get hold of TYPE_AMBIENT_TEMPERATURE with my phones, so instead decided to monitor the battery temperature:

var batteryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { var temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0); var celsius = temperature / 10.0f; // Battery temperature in Celsius Log.i(TAG, String.format("temperature: %f", celsius)); } }; registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 

Works the way you would expect. Close enough for my use case.

Hope that helps, cheers

Sign up to request clarification or add additional context in comments.

Comments

0

You can use TYPE_AMBIENT_TEMPERATURE for battery or CPU temperature. TYPE_TEMPERATURE is the depcrecated constant.

The modified version of code available at documentation should be like this:

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) { } } 

Look at sensor documentation for more

4 Comments

Thanks sir for answer. I had used TYPE_AMBIENT_TEMPERATURE and TYPE_TEMPERATURE, but they give result null.. I want to read CPU temperature from file like this source link
Depending of model, you may or may not have access to /sys/devices/virtual/thermal directory. In your case, you can't have access because the constructor blocked it. The only official way to get the temperature is the solution using TYPE_AMBIENT_TEMPERATURE , even if not implemented by all constructors
Thank you Sir for the Answer @dgp. i tried with different android. The different android can give value for temperature...
This looks very much like a rip off of this answer, without properly linking it, not nice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.