0

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 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");
            }
            }
        });
    }

THANK YOU.

Adam
  • 2,152
  • 4
  • 35
  • 45

1 Answers1

0

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.

Moog
  • 10,193
  • 2
  • 40
  • 66
  • Thank you. Yes this is good idea, but I have for example 5 activity and each activity contains one button and this button contains one echo. And I only need to set the last clicked button (which contain that echo command) on boot adoption. Because all button contains the same echo but with other values (for example: first echo: echo '1,1,1,1,1,1' > /sys/module/lowmemorykiller/parameters/minfree\n and second echo is: echo '2,2,2,2,2,2' > /sys/module/lowmemorykiller/parameters/minfree\n – Adam Oct 12 '11 at 13:58
  • Why not record your last clicked button in sharedpreferences then when you boot you can read back which was the last one clicked and use String.format() to create your echo command dynamically? – Moog Oct 12 '11 at 14:12
  • Sorry but this is my first application and I never have used sharedpreferences before. Do you have some example? Thanks. – Adam Oct 12 '11 at 17:05
  • Plenty of examples around the web but this [Stack Overflow answer](http://stackoverflow.com/questions/3100881/user-preferences-file-vs-app-preferences-file/3101038#3101038) has a nice explanation. For string.format() you can google. It is basic java stuff and you should read about it. – Moog Oct 12 '11 at 20:44