【问题标题】:Why starting a service from command line in Android needs root access (su)?为什么在 Android 中从命令行启动服务需要 root 访问权限(su)?
【发布时间】:2020-09-17 14:48:25
【问题描述】:

我正在尝试从我的 Android 应用程序的命令行启动另一个应用程序(不是我的)的服务。但我注意到它只有在我运行“su”时才有效。我的手机当然是“root”了。 也许还有另一种方法可以在不需要执行 shell 命令的情况下启动应用程序的服务?

此代码有效:

   try {
                            Process process = Runtime.getRuntime().exec("su", null,null);
                            OutputStream outputStream = process.getOutputStream();

                            outputStream.write(("am startservice -a com.companyname.notmyapp.TEST --option a 1").getBytes("ASCII"));

                            outputStream.flush();
                            outputStream.close();
                            process.waitFor();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

这个不是:

   try {
                            Process process = Runtime.getRuntime().exec("am startservice -a com.companyname.notmyapp.TEST --option a 1", null,null);
                            //OutputStream outputStream = process.getOutputStream();
                            //outputStream.flush();
                            //outputStream.close();
                            process.waitFor();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

【问题讨论】:

    标签: java android command-line process su


    【解决方案1】:

    Intent 意图 = new Intent(Intent.ACTION_VIEW); String packageName = "com.ang.chapter_2_service"; //你要启动的包名

    String className = "com.ang.chapter_2.poolBinder.BinderPoolService"; //服务全名要启动什么 intent.setClassName(packageName, className); startService(intent);//或 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

    活动与此相同。

    当然,您要启动的服务或活动需要 manifest.xml 中的标记:

    android:exported="true"
    

    【讨论】:

    • 如何使用您提供的代码传递 --option 的参数?
    猜你喜欢
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多