1

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)

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84

2 Answers2

1

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

Community
  • 1
  • 1
Gopal
  • 1,734
  • 1
  • 14
  • 34
  • But while using default Google Map,if ur GPS is off,then it will turn ON automatically showing only a alert dialogue msg...it will not take to the settings to make it ON – SATYAJIT TARAFDAR Dec 29 '16 at 10:37
  • This is the code to ON gps manually and its working perfectly Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(myIntent); but I dont want to make it ON through settings. If any code is there,help me. Thanx in advance – SATYAJIT TARAFDAR Dec 29 '16 at 10:37
0

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.

Community
  • 1
  • 1
yotam hadas
  • 702
  • 3
  • 14
  • But while using default Google Map,if ur GPS is off,then it will turn ON automatically showing only a alert dialogue msg...it will not take to the settings to make it ON. – SATYAJIT TARAFDAR Dec 29 '16 at 10:26
  • This is the code to ON gps manually and its working perfectly Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(myIntent); but I dont want to make it ON through settings. If any code is there,help me. Thanx in advance – SATYAJIT TARAFDAR Dec 29 '16 at 10:29