开发者

Enable GPS and network location programmatically on rooted phone?

I'd like to write a simple widget that toggles GPS and network location on and off. I've read that it's not possible to do that normally, but presumably on a rooted phone it must be doable. Does anyone know what the code would be? (I only need the code to toggle the location providers, I know how to make the widget.) It doesn't matter that this isn't a 'good' solution, I do开发者_高级运维n't plan to distribute the widget. If it matters, this would be on an HTC Hero, still running v1.5.


I suspect you are looking for LOCATION_PROVIDERS_ALLOWED in android.provider.System.Secure. However, I'm not sure root access will be sufficient to let you invoke those from an SDK application. The relevant methods to modify that setting might be protected by checking the digital signature of the SDK application. AFAIK, that would require you to build and sign your own firmware, then sign the SDK application to match.

You might also be able to figure out what happens in the system when that setting is adjusted, and see if there is a way to affect the same change, via an SDK app on a rooted device, without going through the Secure content provider.


String provider = Settings.Secure.getString(context.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")); 
        context.sendBroadcast(poke);
    }


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    String[] cmds = {"cd /system/bin" ,"settings put secure location_providers_allowed +gps"};
    try {
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
    }
    catch (IOException e){
        e.printStackTrace();
    }

}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜