Why starting a service from command line in Android needs root access (su)?

Viewed 251

I am trying to start a service of an another app (not mine) from command line in my Android app. But I've noticed that it works only if I run "su". My phone of course is "rooted". Maybe there is another way to start a service of an app without needing to execute a shell command?

This code works:

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

This one it doesn't:

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

Intent intent = new Intent(Intent.ACTION_VIEW); String packageName = "com.ang.chapter_2_service"; //the package name what you want to start

String className = "com.ang.chapter_2.poolBinder.BinderPoolService"; //service full name what you want to start intent.setClassName(packageName, className); startService(intent);//or bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

the activity is same to this.

Of course the service or activity what you want to start need a tag in manifest.xml:

android:exported="true"
Related