How set runtime (echo) command on boot adoption?
Could anybody help me how can I set this echo command which you can see below on boot adoption (because when I only execute it with button so after restart there are default values again)? I know how make it with BroadcastReceiver, but I have more buttons and each button contains one echo command and I need only last clicked button with echo command set on boot adoption.
Here is example:
public class MyActivity e开发者_JAVA百科xtends 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");
}
}
});
}
THANK YOU.
The question is slightly unclear but from what I can comprehend broadcast is the designed method. This would require that you launch your activity from the service receiving the BOOT_COMPLETE broadcast.
You can launch an activity from your service like this:
Intent intent = new Intent(MyService.this,MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
You could either bind the activity to your service or move your "echo" code into a static helper to keep it tidy.
Alternatively if you wish to run your activity as a launcher you can use
<category android:name="android.intent.category.LAUNCHER" />
but that may not be your inteded solution.
精彩评论