简体   繁体   中英

Runtime.getRuntime().exec not launching desired app on Android

I'm trying to open the settings app from inside my sample app. Code snippet inside onCreate:

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

When I run this, my app opens, but settings app does not start (neither does any other app I try). The toast inserted for debugging displays "Starting: Intent { act=android.intent.action.MAIN cmp=com.android.settings/.Settings } but it doesn't actually start. Nothing useful visible in logcat either (unless I've missed something in it).

Same command is working using adb (adb shell am start -a android.intent.action.MAIN -n com.android.settings/.Settings)

I'm using Android Studio and an Android 4.4 device if that matters. I've been searching online to figure out what I might be doing wrong, but things seem correct in the above code. Could someone please help?

Update: Apologies for not giving further details, but Settings is not the app that I finally plan to launch in the actual version of this app, Settings was just a test. It is another app which doesn't have android:exported="true" and takes parameters. To be precise, it opens files of a certain extension which it takes as parameters like "adb shell am start -W -a android.intent.action.VIEW -d ..."

The right way to run another application in Android would be According to this answer

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

Why don't you open Settings by Intent ? Something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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