简体   繁体   English

如何开启和关闭GPS

[英]How to Switch ON and OFF GPS

This question has been asked many a times and i have found many results but none of them really helped me. 这个问题已经问了很多遍了,我发现了很多结果,但是没有一个对我有真正的帮助。

Using the below code to Switch ON GPS 使用以下代码打开GPS

String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.i("GPS IS ON", "GPS IS ON"+provider.contains("gps"));           

if(!provider.contains("gps")){ //if gps is disabled

        Log.i("GPS IS ON", "GPS IS ON");

        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);
    }

and to switch OFF : 并关闭:

String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

        if(provider.contains("gps")){ //if gps is enabled
            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);
        }

i am calling these 2 different methods on 2 different clicks.It should automatically switch on the GPS on the device but its not working. 我在2次不同的clicks.Call上调用这2种不同的方法。它应该会自动打开设备上的GPS,但无法正常工作。

i have even added these permissions : 我什至还添加了以下权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

I have tested these in 4.4 and 4.3 and does on Switch on the GPS automatically. 我已经在4.4和4.3中对它们进行了测试,并在自动打开GPS上进行了操作。

Also LOCATION_PROVIDERS_ALLOWED is Deprecated in 4.4 so what is the alternative for that ? 另外,在4.4中不推荐使用LOCATION_PROVIDERS_ALLOWED,那么还有什么替代方案?

EDIT 编辑

LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener locListener = new MyLocationListener();

        try {
            gps_enabled = locManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
        }
        try {
            network_enabled = locManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }

        // don't start listeners if no provider is enabled
        // if(!gps_enabled && !network_enabled)
        // return false;

        if (gps_enabled) {
            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                    0, locListener);

        }

        if (gps_enabled) {
            location = locManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);

        }

        if (network_enabled && location == null) {
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    0, 0, locListener);

        }

        if (location != null) {

            MyLat = location.getLatitude();
            MyLong = location.getLongitude();

} }

I have had this code working but seems that if internet is Not there it wont work with GPS turned OFF. 我已经有此代码的工作,但似乎如果互联网不存在,它将无法与GPS关闭。

I think one cannot change the user's personal phone settings(Like GPS turn on-off, sound on-off,display etc.) from any application. 我认为无法从任何应用程序更改用户的个人电话设置(例如GPS开关,声音开关,显示等)。 It would be security voilation. 这将是安全破坏。

In my opinion here only option is to check if GPS is on or off and Redirect User to Settings screen. 在我看来,这里唯一的选择是检查GPS是打开还是关闭以及“将用户重定向到设置”屏幕。

LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
        showPopupToRedirectToSettings();
    }

By using following Intent you can launch settings screen: 通过使用以下Intent,可以启动设置屏幕:

startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. 注意:如果您同时使用NETWORK_PROVIDER和GPS_PROVIDER,则只需要请求ACCESS_FINE_LOCATION权限,因为它同时包含两个提供程序的权限。 (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.) (对ACCESS_COARSE_LOCATION的权限仅包括对NETWORK_PROVIDER的权限。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM