This question already has answers here:
android app crash on android 4.4 due to gps permission denial
(2 answers)
Closed 6 years ago.
This is my code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
}
}
This is the error in logcat:
FATAL EXCEPTION: main
Process: com.example.satyajittarafdar.gps_on_automatic, PID: 20469
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.satyajittarafdar.gps_on_automatic/com.example.satyajittarafdar.gps_on_automatic.MainActivity}:
java.lang.SecurityException: Permission Denial: not allowed to send
broadcast android.location.GPS_ENABLED_CHANGE from pid=20469,
uid=10240
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5459)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.SecurityException: Permission Denial: not allowed
to send broadcast android.location.GPS_ENABLED_CHANGE from pid=20469,
uid=10240
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at
android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3128)
at android.app.ContextImpl.sendBroadcast(ContextImpl.java:767)
at
android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:396)
at
com.example.satyajittarafdar.gps_on_automatic.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6259)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2382)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5459)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
This is not available as public API, and you cannot change GPS state programmatically from Android 4.4 and above. you have prepare intent and redirect user to settings.
startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
here are some references:
https://stackoverflow.com/a/22529296/3758024
https://stackoverflow.com/a/20236331/3758024
You cannot change the gps state programmaticly since android version 4.4.
Good explanation why: Read this post
What you should so is using intent to send the user to the phoen setting and request him to enable it manually.
Related
I'm using Foreground Services for notification and it's working on other mobile phones as well it's working on an emulator also but in MI phone it's not working it shows error like this.
2020-07-01 17:47:25.783 4338-4338/com.example.notification_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.notification_app, PID: 4338
java.lang.RuntimeException: Unable to create service com.example.notification_app.my_service: java.lang.SecurityException: Permission Denial: startForeground from pid=4338, uid=10175 requires android.permission.FOREGROUND_SERVICE
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3577)
at android.app.ActivityThread.access$1400(ActivityThread.java:200)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1689)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.SecurityException: Permission Denial: startForeground from pid=4338, uid=10175 requires android.permission.FOREGROUND_SERVICE
at android.os.Parcel.createException(Parcel.java:1953)
at android.os.Parcel.readException(Parcel.java:1921)
at android.os.Parcel.readException(Parcel.java:1871)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5104)
at android.app.Service.startForeground(Service.java:695)
at com.example.notification_app.my_service.startMyOwnForeground(my_service.java:57)
at com.example.notification_app.my_service.onCreate(my_service.java:44)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3565)
at android.app.ActivityThread.access$1400(ActivityThread.java:200)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1689)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
2020-07-01 17:47:25.826 4338-4338/? I/Process: Sending signal. PID: 4338 SIG: 9
and in line number 48, I called a function startForeground(1, new Notification());
If you read carefully in the end of second line
requires android.permission.FOREGROUND_SERVICE
I was also facing the same error in one of my project I resolve it by adding permission in the manifest file. So just simply add foreground permission in the manifest file.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"></uses-permission>
Hope it's work for you.
I've tried this code but It's not working Properly.
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent, Manifest.permission.WRITE_SECURE_SETTINGS);
Getting this Error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.setting/com.setting.activity.TraceLocationActivity}:
java.lang.SecurityException: Permission Denial: not allowed to send
broadcast android.location.GPS_ENABLED_CHANGE from pid=28258,
uid=10395
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: java.lang.SecurityException: Permission Denial: not allowed to send
broadcast android.location.GPS_ENABLED_CHANGE from pid=28258,
uid=10395
at android.os.Parcel.readException(Parcel.java:1665)
at android.os.Parcel.readException(Parcel.java:1618)
at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3533)
at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1012)
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:426)
at com.setting.activity.TraceLocationActivity.onCreate(TraceLocationActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6915)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
How can i On GPS Automatically without asking User?
This is not possible, for obvious privacy and security reasons.
I'm using Android policy manager and the following code works on Android 5.0.x mobile devices.
Intent deviceAdminIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
deviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cdmDeviceAdmin);
deviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getResources().getString(R.string.device_admin_enable_alert));
startActivityForResult(deviceAdminIntent, ACTIVATION_REQUEST);
However, when I deploying the same on Android TV image with the same OS version, it throws the following exception.What could be the issue? Where can I find the source1 for Android TV images?
E/AgentApplication: UncaughtExceptionHandler got an exception
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxxxx}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.app.action.ADD_DEVICE_ADMIN (has extras) }
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: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.app.action.ADD_DEVICE_ADMIN (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
1. https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/admin/DevicePolicyManager.java
We use our app on only one device type and we get this error pretty often on it.
The only instance for PackageManager is a BootupReceiver:
public class RestartHelper extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
Logger.getInstance().Log("Restart received");
Intent i = context.getPackageManager().getLaunchIntentForPackage( context.getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
Exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: java.lang.RuntimeException: Package manager has died
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Package manager has died
at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:239)
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:301)
at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:281)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:152)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:60)
at com.cotris.roosapp.StandardActivity.onCreate(StandardActivity.java:49)
at com.cotris.roosapp.MainActivity.onCreate(MainActivity.java:64)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
... 11 more
Caused by: android.os.DeadObjectException
at android.os.BinderProxy.transact(Native Method)
at android.content.pm.IPackageManager$Stub$Proxy.getActivityInfo(IPackageManager.java:1791)
at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:234)
... 20 more
Is this error hardware/system related?
Obviously there has another PackageManger call but not only in RestartHelper.java:
at com.cotris.roosapp.StandardActivity.onCreate(StandardActivity.java:49)
You man need to check the code detail in that.
The android.os.DeadObjectException means that you link to something, that does not exist any more.
the solution for my case was a hardware related problem, which created all kind of crashes of system processes in the background on the device.
Fatal Exception: java.lang.SecurityException: Unable to start service Intent { act=com.google.firebase.crash.internal.service.FirebaseCrashReceiverService.SAVE cmp=com.sample.android/com.google.firebase.crash.internal.service.FirebaseCrashReceiverService (has extras) }: Unable to launch app com.sample.android/10124 for service Intent { act=com.google.firebase.crash.internal.service.FirebaseCrashReceiverService.SAVE cmp=com.sample.android/com.google.firebase.crash.internal.service.FirebaseCrashReceiverService }: process is bad
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1720)
at android.app.ContextImpl.startService(ContextImpl.java:1693)
at android.content.ContextWrapper.startService(ContextWrapper.java:516)
at aii.run(:com.google.android.gms.DynamiteModulesC:181)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Firebase crash is set up perfectly as we are getting reports of the crashes BUT i'm seeing this on Crashlytics and this ultimately leads to crashing the app.
Does anyone know what this problem is?