1

I was trying to add a segment to my Android application, which would gather information about the device (like the serial number for example) and use it for digital signing of data, later on. I was planning on using the output of the getprop function, paired with adequate parameters, for instance getprop ro.serialno.

Googling this issue had me on a path of enabling proper permissions, for this to work.

I am having issues with enabling the READ_PHONE_STATE permission on my device. Adding it to the AndroidManifest.xml file does absolutely nothing. After I reused an already working piece of code to request the user's permission after launching, my app started crashing.

App crashes with:

    --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.strujomeri, PID: 17563
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.strujomeri/com.strujomeri.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

All permissions from AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Code that requests permission. Works for other permission but only crashes here:

ActivityCompat.requestPermissions(MainActivity.this,
        new String[]{Manifest.permission.READ_PHONE_STATE},
        STATE_REQUEST);
if (requestCode == STATE_REQUEST) {
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, "State request granted", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "State request granted", Toast.LENGTH_SHORT).show();
    }
}

EDIT: This is the error message I get when trying to read the serial number of the phone:

E/libc: Access denied finding property "ro.serialno"

Any ideas on how to get this type of information ? Other methods besides getprop can be useful as well. Thank you

I was looking to enable the READ_PHONE_STATE permission on my Android device, which I need for executing some system calls in my code, but I am experiencing a blockade in enabling this permission and suspect that there is theory underneath this issue which I don't already know.

1 Answers1

0

seem you are edited your code before show here.

java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

Problem cause is your permission array. To easy check permission :

    @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    // Forward results to EasyPermissions
    EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@AfterPermissionGranted(RC_PHONE)
private void methodRequiresTwoPermission() {
String[] perms = {Manifest.permission.READ_PHONE_STATE , Any more permission in your manifest};
if (EasyPermissions.hasPermissions(this, perms)) {
    // Already have permission, do the thing
    // ...
} else {
    // Do not have permissions, request 
}
}

if you don't want the lib above

prive boolean checkPermission(String [] perms){
for(String perm : perms){
    if(ActivityCompat.checkSelfPermission(getApplicationContext(),perm) !=PackageManager.PERMISSION_GRANTED){
        return false;
    }
}
return true;

} -use

if (!checkPerms(new String[]{
         // //your permissions in manifest

    })){
        requestPermissions(new String[]{
             //your permissions in manifest

        }, REQUEST_CODE);
    }
Jamebes
  • 96
  • 8
  • 1
    Sorry but no, I did not edit my code before pasting, it's exactly from the terminal. Your solution did eliminate the crash, but I still did not get the READ_PHONE_STATE permission approved. This is the error I get when I try to read the serial number: E/libc: Access denied finding property "ro.serialno" – GeorgeRaspberry Dec 03 '22 at 18:27
  • So if you're using Samsung device there is a certain bug with them. This is worth to be tried [link](https://stackoverflow.com/a/44138687/5648093) – Dušan Panić Dec 03 '22 at 21:57