2011-10-11 114 views
0

任何人都可以幫助我如何設置這個回聲命令,您可以在啓動採用下面看到(因爲當我只用按鈕執行它,所以重啓後有再次默認值)?我知道如何使用BroadcastReceiver,但我有更多的按鈕,每個按鈕包含一個回顯命令,我只需要在引導採用時設置回顯命令的最後單擊按鈕。如何在啓動採用時設置運行時(echo)命令?

這裏是例如:

public class MyActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.my_layout); 

     Button b=(Button)findViewById(R.id.btn_button); 
     b.setOnClickListener(new OnClickListener(){ 
      public void onClick(View paramView){ 

      Process b; 
     try { 
      b = Runtime.getRuntime().exec("su"); 

      DataOutputStream os = new DataOutputStream(b.getOutputStream()); 
      os.writeBytes("echo '1,1,1,1,1,1' > /sys/module/lowmemorykiller/parameters/minfree\n"); 
      os.writeBytes("exit\n"); 
      os.flush(); 
      try { 
       b.waitFor(); 
       if (b.exitValue() != 255) { 
        // TODO Code to run on success 
        toastMessage("root"); 
        } 
       else { 
        // TODO Code to run on unsuccessful    
        toastMessage("not root"); 
        } 
      } catch (InterruptedException e) { 
       // TODO Code to run in interrupted exception  
       toastMessage("not root"); 
       } 
     } catch (IOException e) { 
      // TODO Code to run in input/output exception 
      toastMessage("not root"); 
      } 
      } 
     }); 
    } 

謝謝。

回答

0

問題有點不清楚,但從我能理解的廣播是設計的方法。這需要您從接收BOOT_COMPLETE廣播的服務啓動您的活動。

您可以從您的服務推出的活動是這樣的:

Intent intent = new Intent(MyService.this,MyActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent); 

您既可以綁定活動到您的服務或移動您的「回聲」的代碼放到一個靜態輔助,以保持它的整潔。

另外,如果你想運行一個啓動的活動,你可以使用

<category android:name="android.intent.category.LAUNCHER" /> 

但是這可能不是你的inteded解決方案。

+0

謝謝。是的,這是個好主意,但我有5個活動,每個活動都包含一個按鈕,並且此按鈕包含一個回顯。我只需要在引導採用時設置最後點擊的按鈕(其中包含該echo命令)。因爲所有的按鈕都包含相同的回聲但是具有其他值(例如:第一個回聲:回聲'1,1,1,1,1,1'>/sys/module/lowmemorykiller/parameters/minfree \ n 並且第二個回聲是:echo'2,2,2,2,2,2'>/sys/module/lowmemorykiller/parameters/minfree \ n – Adam

+0

爲什麼不在共享首選項中記錄上次點擊的按鈕,然後在啓動時可以讀回最後一次是最後一次一個點擊並使用String.format()動態創建你的echo命令 – Merlin

+0

抱歉,這是我的第一個應用程序,我從來沒有使用sharedpreferences之前,你有一些例子嗎?謝謝 – Adam

相關問題