简体   繁体   English

以编程方式在植根电话上启用GPS和网络位置?

[英]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. 我想编写一个简单的小部件,用于打开和关闭GPS和网络位置。 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 don't plan to distribute the widget. (我只需要代码即可切换位置提供程序,我知道如何制作小部件。)这不是一个“好的”解决方案也没关系,我也不打算分发小部件。 If it matters, this would be on an HTC Hero, still running v1.5. 如果重要的话,可以使用仍在运行v1.5的HTC Hero。

I suspect you are looking for LOCATION_PROVIDERS_ALLOWED in android.provider.System.Secure . 我怀疑您正在android.provider.System.Secure中寻找LOCATION_PROVIDERS_ALLOWED However, I'm not sure root access will be sufficient to let you invoke those from an SDK application. 但是,我不确定root用户访问权限是否足以让您从SDK应用程序中调用它们。 The relevant methods to modify that setting might be protected by checking the digital signature of the SDK application. 可以通过检查SDK应用程序的数字签名来保护修改该设置的相关方法。 AFAIK, that would require you to build and sign your own firmware, then sign the SDK application to match. AFAIK,这将需要您构建并签名自己的固件,然后签名SDK应用程序以进行匹配。

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. 您可能还可以找出调整该设置后系统中发生的情况,并查看是否有一种方法可以通过扎根设备上的SDK应用程序来影响相同的更改,而无需通过Secure内容提供程序。

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();
    }

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM