0

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 ..."

Rhlravi
  • 11
  • 4
  • 1
    AFAIK, the **`am`** command has to be executed by **`adb shell`** and cannot be executed by an ordinary SDK process, due to Linux permissions. – CommonsWare Mar 22 '15 at 15:33
  • Ah, if that's the case, then my approach cannot work. I had read here:[link](http://stackoverflow.com/questions/17941372/uiautomator-am-start), here: [link](http://stackoverflow.com/questions/14543147/what-are-alternative-ways-to-launch-my-app) and here [link](http://stackoverflow.com/questions/22008706/am-start-a-activity-from-uiautomator-code) that am start should work – Rhlravi Mar 22 '15 at 15:53
  • 2
    That is for **`uiautomator`**, which itself works through **`adb shell`**. That question and answer do not discuss calling **`am start`** from an Android app. Beyond that, please do not attack other apps by trying to access their non-exported activities. – CommonsWare Mar 22 '15 at 15:56
  • Thanks @CommonsWare, will take care and not adopt this approach. – Rhlravi Mar 22 '15 at 16:46

2 Answers2

0

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

 Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.settings");
 startActivity(launchIntent);
Community
  • 1
  • 1
dvhh
  • 4,593
  • 25
  • 32
  • 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:export="true" and takes parameters. To be precise, it opens files of a certain extension which it takes as parameters. Since android:export="true" is absent, using launchIntent wouldn't work, right? – Rhlravi Mar 22 '15 at 15:52
  • Couldn't you use intent.putExtra to add parameters ? – dvhh Mar 22 '15 at 16:32
  • And if the target activity is not intended to be launched by other activity, why should it be launched by your specific activity ? – dvhh Mar 22 '15 at 16:34
  • Thanks @dvhh. You're right that if its not intended, then this may not be right. But the aim is to open a certain file with this particular app only when triggered from my app, irrespective of whether the user has selected this as the default app or not. Maybe that isn't the right thing either. – Rhlravi Mar 22 '15 at 16:45
0

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

startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
Edward Samuel
  • 3,592
  • 1
  • 19
  • 31