How to use ADB SHELL to start service?

moodleemoodlee Member ✭✭
edited October 3 in Xamarin.Android

Before asking this question, i have tried some methods.
1、Get the adb shell by the following code:
var su = Runtime.GetRuntime().Exec("sh");
var os = su.OutputStream;
2、Write start service command.
os.Write(Encoding.UTF8.GetBytes("am startservice -n com.companyname.testapp/.TestApp.FloatService\n"));
os.Write(Encoding.UTF8.GetBytes("exit\n"));
os.Flush();
3、Result
No happened.

In AndroidManifest.xml, we can see:
package="com.companyname.testapp"

By the following code:
var serviceName=(typeof(FloatService)).ToString();
I get the result:TestApp.FloatService

By the following code:
var pName=this.PackageName;
I get the result:com.companyname.testapp

I have tried the following commands:
am startservice -n com.companyname.testapp/.FloatService
am startservice -n com.companyname/.TestApp.FloatService
am startservice -n com.companyname/.FloatService

All failed.

How to start service by adb shell?

Best Answer

  • LeonLuLeonLu Xamurai
    edited October 3 Accepted Answer

    @moodlee
    First of all, We cannot use the code to excute the adb command, I test have root permission, but it have no effect(I get the no permission to do that).

    Here is a workaround about start service by adb shell, please open the Android Adb Command Prompt like following screenshot.

    Then we can start service by foreground-service with following cmd like following runing GIF.

      adb shell am start-foreground-service -n com.xamarin.xample.startedservicesdemo/.MyService 
    

    Why we need use foreground-service to replace the service, becuase In Oreo there are new background execution limits.

    If use use the service, you will get the following error.

    Error while executing: am startservice com.test.myapp/com.android.tools.fd.runtime.InstantRunService
    Starting service: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] 
    cmp=com.test.myapp/com.android.tools.fd.runtime.InstantRunService }
    Error: app is in background uid null
    

    I update my demo, you can test it.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited October 3 Accepted Answer

    @moodlee
    First of all, We cannot use the code to excute the adb command, I test have root permission, but it have no effect(I get the no permission to do that).

    Here is a workaround about start service by adb shell, please open the Android Adb Command Prompt like following screenshot.

    Then we can start service by foreground-service with following cmd like following runing GIF.

      adb shell am start-foreground-service -n com.xamarin.xample.startedservicesdemo/.MyService 
    

    Why we need use foreground-service to replace the service, becuase In Oreo there are new background execution limits.

    If use use the service, you will get the following error.

    Error while executing: am startservice com.test.myapp/com.android.tools.fd.runtime.InstantRunService
    Starting service: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] 
    cmp=com.test.myapp/com.android.tools.fd.runtime.InstantRunService }
    Error: app is in background uid null
    

    I update my demo, you can test it.

  • moodleemoodlee Member ✭✭

    I am so happy. :)
    I followed your steps, and i was successfull.
    Thank you very much, sir.

    Summary.

    0、
    Android 9.0.
    Have root privileges.

    1、
    Add the following code in the front of Service Class.
    [Service(Exported = true, Name = "com.companyname.testapp.FloatService")]

    This step is the most important.

    2、
    Need Root Privileges.
    Use the following code:
    var su = Runtime.GetRuntime().Exec("su");
    var os = su.OutputStream;
    os.Write(Encoding.UTF8.GetBytes("am startservice -n com.companyname.testapp/.FloatService\n"));
    //Or os.Write(Encoding.UTF8.GetBytes("am start-foreground-service -n com.companyname.testapp/.FloatService\n"));
    os.Write(Encoding.UTF8.GetBytes("exit\n"));
    os.Flush();
    su.WaitFor();
    os.Close();
    os.Dispose();
    su.Destroy();

  • moodleemoodlee Member ✭✭

    @LeonLu

    But i have another question, can you give me some advice?

    I want to use "su" to start service with root permission, and open "/dev/input/event3" to write input events.

    In MainActivity.cs, i write the following code:

    var su = Runtime.GetRuntime().Exec("su");
    . . .
    os.Write(Encoding.UTF8.GetBytes("am startservice -n com.companyname.testapp/.FloatService\n"));
    . . .

    And then, i write the following code in TestSo.cpp

    And then, i write the following code in FloatService.cs

    The process is as follows

Sign In or Register to comment.