How to turn on the GPS on Android

后端 未结 7 613
[愿得一人]
[愿得一人] 2020-12-08 08:52

I am developing an android app which needs to activate the GPS.

I read a lot of topics in a lot of forums and the answer I\'ve found is:

it\'

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 08:54

        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(R.string.gps_disabled_message)
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(intent);                  
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    

    This creates an alert and allows the user to go to the settings screen and hit the back button to come right back to your app. The power widget exploit doesn't work beyond 2.3 to my knowledge.

提交回复
热议问题