繁体   English   中英

如何在启动采用时设置运行时(echo)命令?

[英]How set runtime (echo) command on boot adoption?

有人可以帮我如何设置这个echo命令,您可以在启动采用时在下面看到(因为当我仅使用按钮执行该命令时,所以在重启后再次有默认值)? 我知道如何使用BroadcastReceiver做到这一点,但是我有更多按钮,并且每个按钮都包含一个echo命令,并且我只需要在启动采用时设置了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");
            }
            }
        });
    }

谢谢。

这个问题还不太清楚,但是据我所知,广播是设计好的方法。 这将要求您从接收BOOT_COMPLETE广播的服务中启动活动。

您可以像这样从服务启动活动:

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

您可以将活动绑定到您的服务,也可以将“ echo”代码移到静态帮助器中以使其保持整洁。

或者,如果您希望以启动器的形式运行活动,则可以使用

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

但这可能不是您想要的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM