2014-02-13 105 views
0

我的代碼應用程式:殺死當複選框被選中

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每個錶行。
我想如果複選框被選中殺選擇的應用程序,但如果我選擇任何複選框,並按殺按鈕,沒有任何反應。

感謝您的幫助,我是一個機器人的初學者。

+4

內核很可能不會讓你去殺死這些進程,因爲他們不屬於你的應用程序/包。參見['killProcess'](http://developer.android.com/reference/android/os/Process.html#killProcess(int))。 – Overv

+0

你正試圖殺死一個應用程序或只是一個過程? – Peshal

+1

我看不到你在哪裏分配buttonClicked。 –

回答

2

如果您嘗試將過程ID稱爲不是您自己的應用程序或您的應用程序啓動的過程,則方法killProcess將不起作用。在documentation

這是基本的安全限制,所有提及。如果你可以從你的應用程序的每個應用程序殺了你想要的 - 這是非常有問題的安全問題 - 應用程序可以在沒有用戶交互的互相殘殺......

+0

Ty爲您的答案,但吐司也不工作,我不知道爲什麼 – user3306867

+1

@ user3306867:我沒有看到任何地方buttonClicked方法被調用,調試你的應用程序,也許從來沒有方法被調用 –

+1

就像我說.... –

3

,如果它不具有相同的用戶ID你不能殺的應用除非您的手機已經植入,並且您的應用程序具有root權限,否則您的手機已擁有此權限

相關問題