-1

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)

cipher hex
  • 21
  • 1
  • Check this link https://stackoverflow.com/questions/24072489/java-lang-securityexception-permission-denial-not-allowed-to-send-broadcast-an – Maciej Białorucki Feb 19 '18 at 13:39
  • 2
    you can't do this – Nouman Ch Feb 19 '18 at 13:39
  • 6
    You shouldn't be able to do that. It is not up to you to enable GPS. – Vladyslav Matviienko Feb 19 '18 at 13:41
  • 5
    The whole point of permissions is to allow the user to determine how much potentially personal information they want to give to an app. GPS location is personal information (the user's exact location at a given time), so design your app to ask for this permission and handle any denial gracefully. – Michael Dodd Feb 19 '18 at 13:43
  • 1
    @MichaelDodd: In this case I don't believe there is a permission that can be used to toggle the GPS on/off. OP would have to direct the user to the Android settings app (i.e. using `ACTION_LOCATION_SOURCE_SETTINGS`). – Michael Feb 19 '18 at 13:47
  • 1
    @Michael Aha, I mis-read. However, I just tried this on a GPS-enabled app (Runkeeper in my case), and I got a dialog prompting me to enable GPS, so it must be possible without going into settings. **edit** it's `LocationSettingsRequest` - https://developers.google.com/android/reference/com/google/android/gms/location/LocationSettingsRequest.Builder – Michael Dodd Feb 19 '18 at 13:50
  • thanks for replay me. permissions already taken from user.but when Tracing User's location. GPS enabled automatically. – cipher hex Feb 20 '18 at 05:26
  • If it possible then we can use this also as Security Application. – Hardik Ranpariya Feb 24 '18 at 13:27

1 Answers1

5

How can i On GPS Automatically without asking User?

This is not possible, for obvious privacy and security reasons.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491