Runtime.getRuntime()。exec没有在Android上启动所需的应用程序

时间:2015-03-22 15:26:38

标签: java android android-intent exec

我试图从我的示例应用中打开设置应用。 inCreate中的代码片段:

    Process process;
    try {
        process = Runtime.getRuntime().exec("am start -a android.intent.action.MAIN -n com.android.settings/.Settings");
        process.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String s = reader.readLine();
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }
     catch(Exception e) {
        e.printStackTrace();
    }

当我运行此应用程序时,我的应用程序会打开,但设置应用程序无法启动(我尝试的任何其他应用程序都没有)。为调试而插入的toast显示" Starting:Intent {act = android.intent.action.MAIN cmp = com.android.settings / .Settings}但它实际上并没有启动。在logcat中也没有任何有用的东西(除非我错过了它中的内容)。

相同的命令正在使用adb(adb shell am start -a android.intent.action.MAIN -n com.android.settings / .Settings)

如果重要的话,我会使用Android Studio和Android 4.4设备。我一直在线搜索我可能做错了什么,但在上面的代码中似乎是正确的。有人可以帮忙吗?

更新 抱歉没有提供进一步的细节,但设置不是我最终计划在这个应用程序的实际版本中启动的应用程序,设置只是一个测试。它是另一个没有android:exported =" true"并采取参数。确切地说,它会打开一个特定扩展名的文件作为参数,例如" adb shell am start -W -a android.intent.action.VIEW -d ..."

2 个答案:

答案 0 :(得分:0)

在Android中运行其他应用程序的正确方法是According to this answer

 Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.settings");
 startActivity(launchIntent);

答案 1 :(得分:0)

为什么不按Intent打开设置?像这样:

startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);