android apk开机执行shell 命令

一、三个文件

AndroidManifest.xml
BootCompletedReceiver.java
ShellExe.java


二、代码实现

AndroidManifest.xml

    android:sharedUserId="android.uid.system"
<receiver android:name="com.bsm_wqy.validationtools.autouninstallapk.BootCompletedReceiver">  
    <intent-filter>    
        <action android:name="android.intent.action.BOOT_COMPLETED" />    
    </intent-filter>  
</receiver>  

BootCompletedReceiver.java

package com.bsm_wqy.validationtools.autouninstallapk;

import android.util.Log;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;

public class BootCompletedReceiver extends BroadcastReceiver {  

    @Override  
    public void onReceive(Context context, Intent intent) {  
        // TODO Auto-generated method stub  
            try{       
                ShellExe.execCommand("echo 1024 820 > /proc/meminfo"); //shell命令在这里执行
            }catch(Exception ex)
                  {
                   // Log.d("BootCompletedReceiver","抛异常了");
                  }
    }  
}  

ShellExe.java

package com.bsm_wqy.validationtools.autouninstallapk;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class ShellExe {
    private static final String TAG = "ShellExe";
    private static final boolean DEBUG = true;

    public static final String ERROR = "ERROR";
    public static final int RESULT_SUCCESS = 0;
    public static final int RESULT_FAIL = -1;
    public static final int RESULT_EXCEPTION = -2;
    private static StringBuilder sResultBuilder = new StringBuilder("");

    /**
     * Get shell command output
     * 
     * @return Shell command output
     */
    public static String getOutput() {
        return sResultBuilder.toString();
    }

    /**
     * Execute shell command
     * @param command Command string need to execute
     * @return Result
     * @throws IOException Throws when occurs #IOException
     */
    public static int execCommand(String command) throws IOException {
        return execCommand(new String[] { "sh", "-c", command });
    }

    /**
     * Execute shell command
     * @param command Shell command array
     * @return Result
     * @throws IOException Throws when occurs #IOException
     */
    public static int execCommand(String[] command) throws IOException {
        int result = RESULT_FAIL;
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec(command);
        BufferedReader bufferedReader = null;
        sResultBuilder.delete(0, sResultBuilder.length());
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(proc
                    .getInputStream(), Charset.defaultCharset()));
            if (proc.waitFor() == 0) {
                String line = bufferedReader.readLine();
                if (line != null) {
                    sResultBuilder.append(line);
                    while (true) {
                        line = bufferedReader.readLine();
                        if (line == null) {
                            break;
                        } else {
                            sResultBuilder.append('\n');
                            sResultBuilder.append(line);
                        }
                    }
                }
                result = RESULT_SUCCESS;
            } else {
                Log.i(TAG, "exit value = " + proc.exitValue());
                sResultBuilder.append(ERROR);
                result = RESULT_FAIL;
            }
        } catch (InterruptedException e) {
            Log.i(TAG, "exe shell command InterruptedException: "
                    + e.getMessage());
            sResultBuilder.append(ERROR);
            result = RESULT_EXCEPTION;
        } finally {
            if (null != bufferedReader) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    Log.w(TAG, "close reader in finally block exception: " + e.getMessage());
                }
            }
        }
        return result;
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值