执行" appops set" Android应用程序命令失败

时间:2016-08-30 07:49:18

标签: android

我想测试一个应用是否可以ignore其他应用' READ_SMS权限,主要活动的源代码将是

...
ShellExecuter se = new ShellExecuter();
...
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_applist);

    ignPermission("com.vrs.smsapp", "READ_SMS");
}

private void ignPermission(String pkg, String perm) {
    String cmd = "appops set " + pkg + " " + perm + " ignore";
    String res = se.Execute(cmd);
    Log.d(LOGTAG, "cmd: " + cmd + ", res: " + res);
}

班级ShellExecuter的代码:

public class ShellExecuter {

    public String Execute(String cmd) {
        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(cmd);
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        String response = output.toString();
        return response;
    }
}

日志文件是

  

cmd:appops set com.vrs.smsapp READ_SMS ignore,res:

然而,在运行此应用程序后,权限仍然存在:

$ adb shell appops get com.vrs.smsapp
READ_SMS: allow; time=+30s557ms ago
READ_ICC_SMS: allow; time=+30s557ms ago
READ_EXTERNAL_STORAGE: allow; time=+1m9s309ms ago
WRITE_EXTERNAL_STORAGE: allow; time=+1m9s309ms ago

如果我在macbook的终端中运行命令,则成功:

$ adb shell appops set com.vrs.smsapp READ_SMS ignore
$ adb shell appops get com.vrs.smsapp
READ_SMS: ignore; time=+20m38s355ms ago
READ_ICC_SMS: allow; time=+20m38s355ms ago; rejectTime=+13m11s708ms ago
READ_EXTERNAL_STORAGE: allow; time=+13m14s688ms ago
WRITE_EXTERNAL_STORAGE: allow; time=+13m14s688ms ago

我想知道如何更改程序以控制其他应用程序的权限。

0 个答案:

没有答案