安卓终端APP获取到Root权限后,更新后静默安装与自启
2017-04-13 本文已影响312人
丨Fan
最近公司需要为终端设备定制一款APP,除了展示的功能,还需要开机自动启动、静默安装后自动启动的功能。终端设备在出场之前就已经Root了,网上也还是有很多Root的代码,今天就不说Root了。话不多说,直接为小伙伴们一一揭开面纱吧。
1.开机自启(较为简单)
首先写一个广播类
public class BootBroadcastReceiver extends BroadcastReceiver {
static final String action_boot="android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(action_boot)){
Intent ootStartIntent=new Intent(context,MainActivity.class);
ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//这句话一定要加上
context.startActivity(ootStartIntent);
}
}
}
在manifest注册好后,就OK了
//开机自启动广播
<receiver android:name=".widget.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
2.静默安装后自动启动,这里使用命令安装(需要Root)
/**
* 静默安装apk
*/
private boolean installUseRoot(String filePath) {
if (TextUtils.isEmpty(filePath))
throw new IllegalArgumentException("Please check apk file path!");
boolean result = false;
Process process = null;
OutputStream outputStream = null;
BufferedReader errorStream = null;
try {
process = Runtime.getRuntime().exec("su");
outputStream = process.getOutputStream();
String command = "pm install -r " + filePath + "\n";
outputStream.write(command.getBytes());
outputStream.flush();
outputStream.write("exit\n".getBytes());
outputStream.flush();
process.waitFor();
errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuilder msg = new StringBuilder();
String line;
while ((line = errorStream.readLine()) != null) {
msg.append(line);
}
if (!msg.toString().contains("Failure")) {
result = true;
}
} catch (Exception e) {
Log.e("------>", e.getMessage(), e);
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (errorStream != null) {
errorStream.close();
}
} catch (IOException e) {
outputStream = null;
errorStream = null;
process.destroy();
}
}
return result;
}
安装完成后使用AlarmManager发送广播,达到自动启动的目的
AlarmManager的详细介绍 http://blog.csdn.net/wangxingwu_314/article/details/8060312
Intent intent= new Intent(mContext, MyBroadcastReceiver.class);
intent.setAction("INSTALL_AND_START");
PendingIntent sender = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager ALARM = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);
ALARM.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10*1000, sender);
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("INSTALL_AND_START")) {
start(context);
}
}
private void start(Context context){
if (!isRunningForeground(context)){
startAPP("com.example.newtownterminal", context);//字符串中输入自己app的包名
}
}
/**
* 启动一个app
*/
public void startAPP(String appPackageName, Context context){
try{
Intent intent = context.getPackageManager().getLaunchIntentForPackage(appPackageName);
context.startActivity(intent);
}catch(Exception e){
Toast.makeText(context, "尚未安装", Toast.LENGTH_LONG).show();
}
}
/**
* 判断app是否在前台运行
* @param context
* @return
*/
private boolean isRunningForeground (Context context) {
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String currentPackageName = cn.getPackageName();
if(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) {
return true ;
}
return false ;
}
}
<receiver
android:name=".receiver.MyBroadcastReceiver"
android:exported="false" >
<intent-filter>
<action android:name="INSTALL_AND_START"/>
</intent-filter>
</receiver>