简体   繁体   中英

How can I stop a system service programmatically on a rooted android phone?

I am developing an androd bluetooth telnet(?) server which gets commands via bluetooth OPP. My plan is to monitor incoming Opp push, check if it is from certain user, then starting a worker service which actually performs given work.

So I researched information about receiving bluetooth incoming OPP, and I found that killing BluetoothOppService is a key point in this SO thread . So I wrote the codes below to accept incoming OPP push.

private void KillOriginalService()
    {
        java.lang.Process suProcess=null;
        int pid;
        try
    {
                String[] command = {
                    "/system/bin/sh",
                    "-c",
                    "ps|grep com.android.bl"
                };
                suProcess = Runtime.getRuntime().exec(command);
                DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
                DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
                if (null != os && null != osRes)
            {
                    String line;
                    while (osRes.available() > 0)
                    {
                        line = osRes.readLine();
                        if (line.contains("1002"))
                        {
                            String[] words=line.split(" ");
                            //pid=Integer.parseInt(words[0]);
                        final String p=words[0];
                        new ExecuteAsRootBase(){
                            @Override
                            protected ArrayList<String> getCommandsToExecute()
                            {
                                // TODO: Implement this method
                                ArrayList<String> list=new ArrayList<String>();
                                list.add("system/bin/kill -9 " + p);
                                return list;
                            }
                        }.execute();
                        Log.v(TAG,"Success kill");
                        return;
                    }
                }
            }   
        }
        catch (IOException e)
        {
            Log.e(TAG, "error occured trying to kill opp service ",e);
        }
    }

And the following code to get ServerSocket.

private void getServerSocket()
    {
        boolean fail = true;
        while (fail)
        {
            try
            {
                serversocket = null; 
                Log.v(TAG, "trying to get serverSocket");
                serversocket = mBluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("OPP Listener", UUID.fromString("00001105-0000-1000-8000-00805f9b34fb"));   
                if(serversocket!=null)
                    fail = false;
            }
            catch (IOException e)
            {
                fail = true;
                Log.e(TAG, "Failed to get serversocket " , e);
            }

            if (fail)
            {
                KillOriginalService();
            }           
        }
        //return serversocket;
    }

And this code works but sometimes continually ignore the incomming connection until I restart my service manually, causing the original service to accept the connection, rejecting it (because the incoming file's mime type is null). Also the original service acquires full wakelock, consuming my device's battery significantly. Also even when mine accepts the connection, I have to fail about 2 times before mine accepts the connection instead of the original service. I read the logcat outputs, and found out that the original BtOppService restarts after I kill it printing OnStartCommand logcat.

I solved the battery consuming problem by Administration screenoff api. But the first problem is not solved. How can I make my service, not the original service, to receive every incoming connections?

(I am currently solving this problem by using watchdog thread that restarts my service automatically.)

PS I have a rooted device and the su works properly.

I finally found a way to stop the system services programmatically: Hook the system service!

As I had a rooted device, I could install XPosed Framework and modules. I can create a module that attatches to the target service, and then returning not START_STICKY can prevent it from being restarted.

However, it turned out to be a XY problem. I finally changed my implenentation to not Killing the system service, but Living with it.

I created an file observer to check if the system has received a file. When a file is received, it started a corresponding service.

It creates synergy effect with Auto-Accept , which makes a phone to accept every bluetooth OPP file transfer requests without having to ask the user(No prompts!).

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