我的代碼應用程式:殺死當複選框被選中
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每個錶行。
我想如果複選框被選中殺選擇的應用程序,但如果我選擇任何複選框,並按殺按鈕,沒有任何反應。
感謝您的幫助,我是一個機器人的初學者。
內核很可能不會讓你去殺死這些進程,因爲他們不屬於你的應用程序/包。參見['killProcess'](http://developer.android.com/reference/android/os/Process.html#killProcess(int))。 – Overv
你正試圖殺死一個應用程序或只是一個過程? – Peshal
我看不到你在哪裏分配buttonClicked。 –