即使使用root权限也无法终止进程

时间:2015-02-03 08:11:28

标签: android root

我的应用旨在杀死特定进程。我在具有root权限的各种设备上测试过。它可以工作,但在某些设备上,即使设备已植根,也无法终止进程。可能有什么不对?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

int pid;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button B = (Button) findViewById(R.id.button);
    TextView PI = (TextView) findViewById(R.id.tV);

    if (isAppRunning("com.mcent.app")) {
        Toast.makeText(this, "Running", Toast.LENGTH_SHORT).show();
        PI.setText("Mcent is running at PID - "+ Integer.toString(getPid("com.mcent.app")));
        B.setText("Go Kill !");
        ActivityManager A = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        B.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                try {
                    Process process = Runtime.getRuntime().exec("adb shell am force-stop com.mcent.app");
                    Toast.makeText(MainActivity.this, "Killed!", Toast.LENGTH_SHORT).show();
                    InputStreamReader reader = new InputStreamReader(process.getInputStream());
                    BufferedReader bufferedReader = new BufferedReader(reader);
                    int numRead;
                    char[] buffer = new char[5000];
                    StringBuffer commandOutput = new StringBuffer();
                    while ((numRead = bufferedReader.read(buffer)) > 0) {
                    commandOutput.append(buffer, 0, numRead);
                    }
                    bufferedReader.close();
                    process.waitFor();
                    return;
                    } catch (IOException e) {
                    throw new RuntimeException(e);
                    } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                    }

            }
        });

    }else{

        Toast.makeText(this, "Not Running", Toast.LENGTH_SHORT).show(); 
        PI.setText("Mcent is not running");
        B.setText("Keep Calm !");
}   }


public boolean isAppRunning (String aApplicationPackageName)
{
            ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
                if (activityManager == null)
                    {
                        return false;
                    }
                    List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
                    for(int idx = 0; idx < procInfos.size(); idx++)
                        {   

                            if(procInfos.get(idx).processName.equals(aApplicationPackageName))
                            {   
                                    return true;
                            }
                        }
                                return false;
}

public int getPid (String packageName){
    ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
    int processid = 0;
    for(int i = 0; i < pids.size(); i++) {
        ActivityManager.RunningAppProcessInfo info = pids.get(i);

        Log.i("PID",pids.get(i) + "");
        Log.i("PID Package",info.processName);

        if(info.processName.equalsIgnoreCase(packageName)){
            processid = info.pid;
            return processid;
        } 
    }
    return -1;
}

}

1 个答案:

答案 0 :(得分:1)

某些服务可能被编程为在被杀后自动启动。

http://developer.android.com/reference/android/app/Service.html#START_STICKY