在Jellybean中打开GPS

时间:2014-05-21 05:40:13

标签: android gps

我知道这个问题已被问过一百万次了。不幸的是,我仍然对它毫无头绪。我也正在开发一个应用程序,告诉我打开GPS并将其关闭。我尝试过各种各样的建议:

1)

private void turnGpsOn (Context context) {
        String beforeEnable = Settings.Secure.getString (context.getContentResolver(),
                                                        Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        String newSet = String.format ("%s,%s",
                                       beforeEnable,
                                       LocationManager.GPS_PROVIDER);
        try {
            Settings.Secure.putString (context.getContentResolver(),
                                       Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
                                       newSet); 
        } catch(Exception e) {}
    }

2。)

                  locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                    MIN_DISTANCE_CHANGE_FOR_UPDATES,this);

不幸的是,这些建议都不起作用。我在AndroidManifest.xml中也有WRITE_SECURE_SETTINGS。还是不行。有没有解决这个问题的方法,或者可以打开GPS。

谢谢, 乌代

1 个答案:

答案 0 :(得分:1)

Just Refer my below code
String provider = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

        if (!provider.contains("gps")) { // if gps is disabled
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            sendBroadcast(poke);
        }