【问题标题】:Turn On GPS on rooted devices在有根设备上打开 GPS
【发布时间】:2012-12-28 07:42:52
【问题描述】:

这个关于在 android 上以编程方式打开/关闭 GPS 的问题已经讨论过很多次了,答案总是一样的:出于安全/隐私的原因,您不能这样做。 但是有没有办法让root设备通过系统设置中的一些编辑来打开gps..

【问题讨论】:

    标签: android gps


    【解决方案1】:

    有一个根深蒂固的工作解决方案。请参阅我在个人资料中的一个答案。我现在无法详细说明(在医院)。但目前你需要root和busybox。我试图让它在没有busybox的情况下工作。用 2.3.5 4.0.1 和 4.1.2 测试

    http://rapidshare.com/files/3977125468/GPSToggler-20130214.7z

    【讨论】:

    • 我认为 Busybox 没有必要。更新:rapidshare.com/files/1458124346/GPSToggler-20130222.7z
    • 找不到文件...请您提供另一个链接(也许是保管箱?)
    • 你必须自己从GIT下载它github.com/sms2000/GPSToggler
    • 您好 OGP,您如何管理启用网络定位时显示的 GPS 免责声明消息?是否可以删除或隐藏它?非常感谢!
    • 不确定我是否理解得很好。什么讯息?通知区的那个?它的目的仅仅是使该服务对 Android 很重要。所以它会在Android杀死应用程序后尽快自动重启。
    【解决方案2】:

    您可以通过编程方式打开/关闭 GPS,直至 Android 2.2(API 8)

    这是我正在使用的代码

    public class GpsOnOff extends Activity implements OnClickListener {
        Button onButton;
        Button offButton;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            onButton = (Button) findViewById(R.id.btnON);
            offButton = (Button) findViewById(R.id.btnOFF);
    
            onButton.setOnClickListener(this);
            offButton.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
    
            if(v==onButton){
    
                //this will work upto 2.2 api only
                turnGPSOn();
    
                //this will work for all but it Navigate to 
                             GPS setting screen only 
                //not change settings automatically
                /*Intent in = new  Intent(android.provider.Settings
                             .ACTION_LOCATION_SOURCE_SETTINGS); 
                startActivity(in);*/
    
    
                Toast.makeText(getApplicationContext(),
                                "gps enabled", 1).show();
            }
            else if(v==offButton){
                turnGPSOff();
                Toast.makeText(getApplicationContext(), 
                               "gps disabled", 1).show();
            }
    
        }
    
        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);
            }
        }
    }
    

    确保您需要在manifest 文件中添加以下两个权限

    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
    

    【讨论】:

    • 您可以将目标设置为 2.2,这将正常工作,或者在高于 2.2 的 android 版本中,您只需将用户导航到设备中的 GPS 设置屏幕。
    • 实际上我不希望用户导航到设置屏幕我知道这是一个坏主意.. 但是有没有可能的方法来做到这一点.. 使用根设备并在系统设置中进行一些编辑..跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2011-11-26
    相关资源
    最近更新 更多