4

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

4

2 回答 2

2

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

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

于 2013-02-17T09:52:19.650 回答
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" />
于 2012-12-28T07:53:51.120 回答