开启关闭GPS机器人 [英] turn on off GPS android

查看:189
本文介绍了开启关闭GPS机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/1051649/how-to-programmatically-enable-gps-in-android-cupcake">How以编程方式启用GPS在Android的蛋糕

我目前正在写的Andr​​oid的应用程序,与GPS的工作原理。目前,我能够制定出GPS是否启用。我的问题是,我希望能够在应用程序启动时的GPS,如果它被禁用。我怎样才能做到这一点programmaticaly? 另外,我要创建一个打开和关闭GPS,我读了计算器中的所有线程有关,我试过,我得到一个unfortanly您的应用程序必须停止(我没有忘记添加许可)这可是所有的功能函数

I'm currently writing an app in Android that works with the GPS. At the moment I'm able to work out whether the GPS is enabled. My problem is that I want to enable the GPS on app startup if it is disabled. How can I do this programmaticaly? also, i want to create functions that turn on and off the GPS, i read all the threads on stackoverflow about it however all the functions that i tried i got a "unfortanly your app must stopped" (i didnt forget to add permission)

有人可以帮我一个工作的功能开启或关闭GPS?

can someone help me with a working function to enable or disable GPS?

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

首先,我用这些功能:

at first, i used these functions:

 private void turnGPSOn(){
        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);
        }
    }

    private void turnGPSOff(){
        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);
        }
    }

然后我尝试使用:

then i tried use:

ENABLE GPS:

Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
DISABLE GPS:

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);

这两个工作不适合我。

both are not working for me

任何人有想法?

推荐答案

您不能以编程方式打开GPS和关闭。最好你能做的就是向用户发送到设置屏幕,可以让他们自己做。

you cannot programmatically turn GPS on and off. the best you can do is send the user to the settings screen that allows them to do it themselves.

final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
     new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
     startActivity(intent);
}

存在着黑客打开GPS /关闭编程,但他们的工作只是在旧版本的Andr​​oid。即使你可以,不这样做。用户可能关闭GPS,因为他们不想让应用程序来跟踪它们precisely。这是非常不好的形式,试图强行改变自己的决定。

there existed hacks to turn GPS on / off programmatically, but they work only in older versions of Android. even if you could, don't do this. the user may have turned off GPS because they didn't want to allow apps to track them precisely. it's extremely bad form to try and force change their decision.

如果您需要GPS启用,检查它,当你的应用程序启动和保释,如果用户不启用它。

if you require GPS to be enabled, check for it when your app starts and bail if the user won't enable it.

这篇关于开启关闭GPS机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆