Search code examples
javaandroidtelephonymanagerandroid-securityexception

Getting java.lang.SecurityException when requesting for SIM info


I am trying to make an app that gives one the Sim card number, IMEI number and phone number. However when I do it I get the following error:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hadi.sim_info/com.example.hadi.sim_info.MainActivity}: java.lang.SecurityException: getLine1NumberForDisplay: Neither user 10034 nor current process has android.permission.READ_SMS

I have written the permission in the manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Here is my code for the app:

public class MainActivity extends AppCompatActivity {

    TextView tlf, sim, IMEI;

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

        tlf = (TextView) findViewById(R.id.Tlf_nr);
        sim = (TextView) findViewById(R.id.sim);
        IMEI = (TextView) findViewById(R.id.IMEI);

        TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        String number = tm.getLine1Number();
        String simNR = tm.getSimSerialNumber();
        String IMEINR = tm.getDeviceId();

        tlf.setText("Tlf. Nr.: "+number);
        sim.setText("SIM Nr.: "+simNR);
        IMEI.setText("IMEI Nr.: "+ IMEINR);
    }
}

EDIT:

This is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.hadi.sim_info"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Solution

  • Add following code to your Activity's onCreate method if you are running your application on Android 6.x or higher.

            if (ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_PHONE_STATE},
                        CONST_ID);
            }
    

    CONST_ID is a constant you define to identify this request.

    This will popup a request permission dialog to user if the permission is not granted. You can then track whether the permission was granted or not by overriding following method.

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

    Based on the permission state you can then execute rest of your code.