Android系统权限需要的总结

本文总结了Android系统权限的几个关键应用场景,包括如何在代码中和通过命令行重启设备,修改系统时间(需要root权限),以及使用PMUtils进行静默安装和卸载应用的方法。对于需要此类权限的操作,详细阐述了实现步骤和可能遇到的问题。

Android中需要系统权限方法总结


重启设备

1.0使用在代码中方法

Intent reboot = new Intent(Intent.ACTION_REBOOT);
            reboot.putExtra("nowait", 1);
            reboot.putExtra("interval", 1);
            reboot.putExtra("window", 0);
            MyApp.getContext().sendBroadcast(reboot);

需要系统的权限,如果不给权限,则会报错:错误如下:

 打印错误java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.REBOOT from pid=24279, uid=10055  // 系统权限不够    

2.0使用命令行重启设备:

String cmd ="su -c reboot " 
Runtime.getRuntime().exec("su -c reboot");
修改系统时间(设备有root权限)

1.0在代码中方法

 public static void setTime(String time){

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date myDate = null;
    try {
        myDate = formatter.parse(time);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Calendar cal= Calendar.getInstance();
    cal.setTime(myDate);
    long when = cal.getTimeInMillis();

    if (when / 1000 < Integer.MAX_VALUE) {
        SystemClock.setCurrentTimeMillis(when);
    }
}

参考的链接如下: http://www.cnblogs.com/Free-Thinker/p/6627813.html

2.0使用命令行方法

//命令行修改系统时间

public void testDate(String time){
    try {
        Process process = Runtime.getRuntime().exec("su");
        String datetime="20171010.112800"; //测试的设置的时间【时间格式 yyyyMMdd.HHmmss】
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("setprop persist.sys.timezone GMT\n");
        os.writeBytes("/system/bin/date -s "+datetime+"\n");
        os.writeBytes("clock -w\n");
        os.writeBytes("exit\n");
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
添加代码PMUtils实现静默安装和卸载
 import android.util.Log;

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

  /**
 ** 创建时间: 2017/9/21 0021.
 ** 编写人:Tina
 ** 邮箱:1208156801@qq.com
 ** 功能描述:
 */

public class PmUtils {
private static  String TAG = PmUtils.class.getSimpleName();




private void  toStarCommand(){
    runShellCommand("pm list features");//执行命令
}
private void runShellCommand(String command) {
    Process process =null;
    BufferedReader bufferedReader =null;
    Log.d(TAG, "runShellCommand: -------");
    StringBuilder mShellCommand = new StringBuilder();
    Log.d(TAG, "runShellCommand: "+command);
    mShellCommand.delete(0,mShellCommand.length());
    try {
        String[] cmd = new String[] { "/system/bin/sh", "-c", command }; //调用bin文件

        byte b[] = new byte[1024];
        process = Runtime.getRuntime().exec(cmd);
        bufferedReader = new BufferedReader(new InputStreamReader( process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            mShellCommand.append(line); }
        Log.d("wenfeng", "runShellCommand result : " + mShellCommand.toString());
        process.waitFor();
    }catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    finally {
        if (bufferedReader != null) {
            try { bufferedReader.close();
            } catch (IOException e) { // TODO: handle exception
                }
        } if (process != null) {
            process.destroy();
        }

            }
        }




        private static void exeCommand(String command) throws IOException{
            Runtime runtime = Runtime.getRuntime();
            Process pro = runtime.exec(command);
            try {
                if (pro.waitFor()!=0){

                }
                BufferedReader in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
                StringBuffer stringBuffer = new StringBuffer();
                String line =null;
                while ((line = in.readLine()) != null) {
                    stringBuffer.append(line+"");
                }
                Log.d(TAG, "exeCommand: 123"+stringBuffer.toString());

            } catch (InterruptedException e) {
                e.printStackTrace();
                Log.d(TAG, "exeCommand: 456"+e);
            } finally {

                pro.destroy();
            }
        }

   public  static  void StartCommand(String command,String path){
       String commande = command;
       String pathApk =path;
       if (commande.equals("install")){//静默安装
           try {
//                   pm install -r/mnt/sdcard/test.apk
               exeCommand("pm su");
               exeCommand("pm install -r"+pathApk);
           } catch (IOException e) {
               e.printStackTrace();
           }

       }else if (commande.equals("uninstall")){//静默卸载
           String cmd = "pm uninstall "+ pathApk;
           Log.d(TAG, "StartCommand: -----------"+cmd);

           try {
//                   pm install -r/mnt/sdcard/test.apk
               exeCommand("pm su");
               exeCommand(cmd);
           } catch (IOException e) {
               e.printStackTrace();
           }


       }else {

       }

   }

    public static     Runnable runable = new Runnable() {
            @Override
            public void run() {
                try {
                    exeCommand("pm su");
                    exeCommand("ifconfig eth0");
//

//                        exeCommand("getprop net.eth0.dns1");
//                        exeCommand("getprop net.eth0.gateway");
 //
//                        exeCommand("getprop net.mask");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        };



/**
 ** 执行具体的静默安装逻辑,需要手机ROOT。
 ** @param apkPath
 **          要安装的apk文件的路径
 ** @return 安装成功返回true,安装失败返回false。
 */
public static boolean install(String apkPath) {
    boolean result = false;
    DataOutputStream dataOutputStream = null;
    BufferedReader errorStream = null;
    try {
        // 申请su权限
        Process process = Runtime.getRuntime().exec("su");
        dataOutputStream = new DataOutputStream(process.getOutputStream());
        // 执行pm install命令
 //            String command = "pm install -r " + apkPath + "\n";
        String command = "pm uninstall "+ apkPath;
        Log.d(TAG, "install: ----"+command);
        dataOutputStream.write(command.getBytes(Charset.forName("utf-8")));
        dataOutputStream.flush();
        dataOutputStream.writeBytes("exit\n");
        dataOutputStream.flush();
        process.waitFor();
        errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        String msg = "";
        String line;
        // 读取命令的执行结果
        while ((line = errorStream.readLine()) != null) {
            msg += line;
        }
        Log.d(TAG, "install msg is " + msg);
        // 如果执行结果中包含Failure字样就认为是安装失败,否则就认为安装成功
        if (!msg.contains("Failure")) {
            result = true;
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    } finally {
        try {
            if (dataOutputStream != null) {
                dataOutputStream.close();
            }
            if (errorStream != null) {
                errorStream.close();
            }
        } catch (IOException e) {
            Log.e("TAG", e.getMessage(), e);
        }
    }
    return result;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值