选中CheckBox时杀死应用程序

时间:2014-02-13 16:27:48

标签: android checkbox

我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_layout);

    ActivityManager actvityManager = (ActivityManager)
    this.getSystemService( ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();

    int i=0;

    // get a reference for the TableLayout
    TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);

    TextView[] tx = new TextView[procInfos.size()];
    TableRow[] row = new TableRow[procInfos.size()];
    CheckBox[] cb = new CheckBox[procInfos.size()];


    for(i = 0; i < procInfos.size(); i++) {         
        // create a new TableRow
        row[i] = new TableRow(this);

        //Styl
        row[i].setBackgroundResource(R.drawable.border);

        // create a new TextView
        tx[i] = new TextView(this);

        //Styl
        tx[i].setTextAppearance(getApplicationContext(), R.style.normalText);

        // set the text to "text xx"   
        tx[i].setText("     "+ procInfos.get(i).processName +"\n");

        // create a CheckBox
        cb[i] = new CheckBox(this);

        // add the TextView and the CheckBox to the new TableRow
        row[i].addView(tx[i]);
        row[i].addView(cb[i]);

        // add the TableRow to the TableLayout
        table.addView(row[i]);
    }

}

public void buttonClicked(View button) {
    ActivityManager actvityManager = (ActivityManager)
    this.getSystemService( ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();

    // get a reference for the TableLayout
    TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);

    TextView[] tx = new TextView[procInfos.size()];
    TableRow[] row = new TableRow[procInfos.size()];
    CheckBox[] cb = new CheckBox[procInfos.size()];

    table.removeAllViewsInLayout();

    for(int i = 0; i < procInfos.size(); i++) { 
         // create a new TableRow
        row[i] = new TableRow(this);

        //Styl
        row[i].setBackgroundResource(R.drawable.border);

        // create a new TextView
        tx[i] = new TextView(this);

        //Styl
        tx[i].setTextAppearance(getApplicationContext(), R.style.normalText);

        // set the text to "text xx"   
        tx[i].setText("     "+ procInfos.get(i).processName +"\n");

        // create a CheckBox
        cb[i] = new CheckBox(this);         

        // add the TextView and the CheckBox to the new TableRow
        row[i].addView(tx[i]);
        row[i].addView(cb[i]);

        // add the TableRow to the TableLayout
        table.addView(row[i]);

        if (((CheckBox) cb[i]).isChecked()) {
            int pid=procInfos.get(i).processName.length();
            android.os.Process.killProcess(pid);
            Toast.makeText(MainActivity.this,
                       "Chx " + i, Toast.LENGTH_LONG).show();
         }

    }
}

我有一个CheckBox数组,每个表行都有1个 如果CheckBox被选中,我试图杀死所选的应用程序,但如果我选择任何CheckBox并按下kill按钮,则没有任何反应。

感谢您的帮助,我是一名机器人初学者。

2 个答案:

答案 0 :(得分:3)

除非您的手机已植根且您的应用具有root权限,否则如果该应用没有相同的用户ID,则无法终止该应用。

答案 1 :(得分:2)

如果您尝试为不属于您自己的应用程序的进程ID或应用程序启动的进程调用它,则方法killProcess将无效。 documentation

中提到的所有内容

这是基本的安全限制。如果你可以从你的应用程序中删除你想要的每个应用程序 - 这是非常有问题的安全问题 - 应用程序可能会在没有用户交互的情况下互相残杀......