Exception while turning on GPS

266 views Asked by At

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)

2

There are 2 answers

2
yotam hadas On

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.

2
Gopal On

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