0

I m having the problem while getting the IMEI no,i have tride almost all code from google,but still stuck on the same error,

Menifest

 <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

MainActivity.java

  import android.content.Context;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TelephonyManager tel;
    TextView imei;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        imei = (TextView) findViewById(R.id.textView2);
        imei.setText(tel.getDeviceId().toString());

    }

}

content_main.xml

 <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:textColor="#4169E1"
        android:textSize="18sp"
        android:text="IMEI Number" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="15sp"
        android:text="IMEI" />

logcat:

 FATAL EXCEPTION: main
                                                                        Process: com.example.a3spl.emieno, PID: 5402
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a3spl.emieno/com.example.a3spl.emieno.MainActivity}: java.lang.SecurityException: getDeviceId: Neither user 10096 nor current process has android.permission.READ_PHONE_STATE.
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                         Caused by: java.lang.SecurityException: getDeviceId: Neither user 10096 nor current process has android.permission.READ_PHONE_STATE.
                                                                            at android.os.Parcel.readException(Parcel.java:1599)
                                                                            at android.os.Parcel.readException(Parcel.java:1552)
                                                                            at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4175)
                                                                            at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:706)
                                                                            at com.example.a3spl.emieno.MainActivity.onCreate(MainActivity.java:31)
                                                                            at android.app.Activity.performCreate(Activity.java:6237)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                            at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Please help me to solve my problem

  • please follow below link http://stackoverflow.com/questions/33097891/app-crashes-with-securityexception-on-android-m – Riskhan Jul 20 '16 at 10:29
  • This issue will occur on Android 6.0 Permission Error , So check for Requesting Permissions at Run Time https://developer.android.com/training/permissions/requesting.html – Android Surya Jul 20 '16 at 10:30

1 Answers1

1

This is because you have to ask permissions to user in android version 6.0 for android.permission.READ_PHONE_STATE

int permission = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.READ_PHONE_STATE);
if(permission != PackageManager.PERMISSION_GRANTED) {
    // ask permission here
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.READ_PHONE_STATE}, 101);
} else {
    // do your work here
}

the above code should be in onCreate method of activity.

and check if user has granted the permission in onRequestPermissionsResult method of activity

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    if (requestCode == 101) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the
            // IMEI Getting task here

        } else {
             // permission denied,
             //  don't do the functionality that depends on this permission.
        }
    }
}

Hope this'll help.

Reference from this

ELITE
  • 5,815
  • 3
  • 19
  • 29