4

Android で GPS をプログラムでオン/オフすることに関するこの質問は何度も議論されてきましたが、答えは常に同じです。セキュリティ/プライバシー上の理由からできません。しかし、ルート化されたデバイスがシステム設定を編集してGPSをオンにする方法はありますか..

4

2 に答える 2

2

root化された実用的なソリューションがあります。プロフィールの私の回答の 1 つを参照してください。私は今(病院で)詳しく説明することはできません。ただし、現時点では root と 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

Android 2.2 (API 8) までは、プログラムで GPS のオン/オフを切り替えることができます。

これが私が使用しているコードです

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以下の 2 つの権限をファイルに追加する必要があることを確認してください

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
于 2012-12-28T07:53:51.120 に答える