安装模式及安装自启动
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安装模式及安装自启动相关的知识,希望对你有一定的参考价值。
1:下载apk
InputStream in=null;
FileOutputStream out = null;
try {
URL url = new URL(urlStr);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.setConnectTimeout(10 * 1000);
urlConnection.setReadTimeout(30 * 1000);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
urlConnection.connect();
long bytetotal = urlConnection.getContentLength();
long bytesum = 0;
int byteread = 0;
in = urlConnection.getInputStream();
//保存 apk
File apkFile = StorageUtils.getCacheFile(this,appName,apkVersion);
out = new FileOutputStream(apkFile);
byte[] buffer = new byte[BUFFER_SIZE];
int oldProgress = 0;
while ((byteread = in.read(buffer)) != -1) {
bytesum += byteread;
out.write(buffer, 0, byteread);
int progress = (int) (bytesum * 100L / bytetotal);
// 如果进度与之前进度相等,则不更新,如果更新太频繁,否则会造成界面卡顿
if (progress != oldProgress) {
updateProgress(progress);
}
oldProgress = progress;
}
2:一般user安装
/**
* 点击系统通知栏安装
* @param apkFile
* @throws IOException
*/
public static void installByNotification (Context context,File apkFile) throws IOException {
NotificationManager mNotifyManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
String appName=context.getString(context.getApplicationInfo().labelRes);
int icon=context.getApplicationInfo().icon;
mBuilder.setContentTitle(appName).setSmallIcon(icon);
mBuilder.setContentText("下载成功").setProgress(0, 0, false);
Intent installAPKIntent = new Intent(Intent.ACTION_VIEW);
//如果没有设置SDCard写权限,或者没有sdcard,apk文件保存在内存中,需要授予权限才能安装
String[] command = {"chmod","777",apkFile.toString()};
ProcessBuilder builder = new ProcessBuilder(command);
builder.start();
installAPKIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
//installAPKIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//installAPKIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//installAPKIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, installAPKIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
Notification noti = mBuilder.build();
noti.flags = Notification.FLAG_AUTO_CANCEL;
mNotifyManager.notify(0, noti);
}
3:取得root权限静默安装
/**
* 取得root权限实现静默安装
* @param apkPath
* @return
*/
private static boolean rootClientInstall(String apkPath){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
// Log.v("apkPath--",apkPath);
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.println("chmod 777 "+apkPath);
PrintWriter.println("export LD_LIBRARY_PATH=/vendor/lib:/system/lib");
PrintWriter.println("pm install -r " + apkPath);
// PrintWriter.println("exit");
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
/**
* 判断手机是否有root权限
*/
private static boolean hasRootPermission(){
PrintWriter PrintWriter = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");//执行su是向系统请求root权限,赋给当前进程
PrintWriter = new PrintWriter(process.getOutputStream());
PrintWriter.flush();
PrintWriter.close();
int value = process.waitFor();
return returnResult(value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
}
return false;
}
4:内置rom静默安装
/**
* 系统内置rom静默安装
* @param apkPath
* @return
*/
private static String systemClientInstall(String apkPath){
String[] args = { "pm", "install", "-r", apkPath };
String result = "";
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
InputStream errIs = null;
InputStream inIs = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int read = -1;
process = processBuilder.start();
errIs = process.getErrorStream();
while ((read = errIs.read()) != -1) {
baos.write(read);
}
baos.write(Integer.parseInt("/n"));
inIs = process.getInputStream();
while ((read = inIs.read()) != -1) {
baos.write(read);
}
byte[] data = baos.toByteArray();
result = new String(data);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (errIs != null) {
errIs.close();
}
if (inIs != null) {
inIs.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return result;
}
5:判断软件类型
/**
* 是否为系统软件
* @param pInfo
* @return
*/
private static boolean isSystemApp(PackageInfo pInfo) {
if(pInfo == null ) return false ;
return ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}
/**
* 是否为系统更新软件
* @param pInfo
* @return
*/
private static boolean isSystemUpdateApp(PackageInfo pInfo) {
if(pInfo == null ) return false ;
return ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0);
}
/**
* 是否为用户软件
* @param pInfo
* @return
*/
private static boolean isUserApp(PackageInfo pInfo) {
if(pInfo == null ) return false ;
return (!isSystemApp(pInfo) && !isSystemUpdateApp(pInfo));
}
6:安装完毕广播自启动
if(intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)){
String packageName = intent.getDataString().substring(8);
Log.v("install app----", packageName);
Intent newIntent = new Intent();
newIntent.setClassName("com.example.********","com.example.*******"+ ".ui.activity.SplashActivity");
newIntent.setAction("android.intent.action.MAIN");
newIntent.addCategory("android.intent.category.LAUNCHER");
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
checkDownloadApk(context);
}
广播注册
<receiver android:name=".ui.java.AppBroadcastServer">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
<!-- 注意!! 这句必须要加,否则接收不到BroadCast -->
</intent-filter>
</receiver>
安装权限添加
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
以上是关于安装模式及安装自启动的主要内容,如果未能解决你的问题,请参考以下文章