在有根设备上打开GPS

时间:2012-12-28 07:42:52

标签: android gps

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

您可以通过编程方式打开/关闭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" />