AppFactory T AppFactory T - 6 months ago 76
Android Question

"Neither user 10046 nor current process has android.permission.ACCESS_COARSE_LOCATION" but it's in my manifest

I added the permission of

android.permission.ACCESS_COARSE_LOCATION
in the Manifest.xml and code is below.

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

GsmCellLocation location = (GsmCellLocation) tm.getCellLocation();
int cellID = location.getCid();
int lac = location.getLac();

Toast.makeText(getApplicationContext(), cellID, Toast.LENGTH_LONG).show();


However, it crashed with the error message below. Please check how to solve this kind of problem.

05-15 23:23:09.844 22602-22602/use.uevision.com.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: use.uevision.com.myapplication, PID: 22602
java.lang.RuntimeException: Unable to start activity ComponentInfo{cctvkim.truevision.com.myapplication/cctvkim.truevision.com.myapplication.MainActivity}: java.lang.SecurityException: getCellLocation: Neither user 10046 nor current process has android.permission.ACCESS_COARSE_LOCATION.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.SecurityException: getCellLocation: Neither user 10046 nor current process has android.permission.ACCESS_COARSE_LOCATION.strong text
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getCellLocation(ITelephony.java:3311)
at android.telephony.TelephonyManager.getCellLocation(TelephonyManager.java:963)
at cctvkim.truevision.com.myapplication.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
05-15 23:23:13.804 22602-22602/cctvkim.truevision.com.myapplication I/Process: Sending signal. PID: 22602 SIG: 9

Answer

Yuo should use RuntimePermissions. Startins with Android M you need to ask permissions at Runtime. Please refer to link.

 if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST);
 }else{
           //do your job
 }