[RK3399][Android7.1.1] 静默安装应用及安装完成自启动

测试平台

Platform: RK3399
OS: Android 7.1.1

需求

静默安装应用

解决方案

  1. 实现静默安装应用的话需要系统权限:
    在AndroidManifest.xml中添加android:sharedUserId=“android.uid.system”.
    使用系统签名文件(platform.pk8、platform.x509.pem,签名文件位于build/target/product/security/目录下)对apk进行签名

  2. 在AndroidManifest.xml添加相应需要的权限

    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
  3. 添加函数代码,调用函数进行应用安装

    int result = installSilent("com.firefly.testslinet","/mnt/sdcard/123.apk");
    

    前面包名是你实现静默安装的应用的包名,比如我的是testslinet 应用来静默安装/sdcrad/123.apk.那么就是 installSilent(“com.firefly.testslinet”,"/mnt/sdcard/123.apk");

         /** 
          * 静默安装
          * @param packageName   调用installSilent函数的应用包名
          * @param filePath     静默安装应用的apk路径
          * @return 0 安装成功
          *         1 文件不存在
          *         2 安装失败 
          */  
         public static int installSilent(String packageName,String filePath) {  
                 File file = new File(filePath);  
                 if (filePath == null || filePath.length() == 0 || file == null || file.length() <= 0 || !file.exists() || !file.isFile()) {  
                         return 1;  
                 }  
                 //pm install -i 包名 --user 0 apkpath
                 String[] args = { "pm", "install","-i",packageName,"--user","0", "-r", filePath };  
                 ProcessBuilder processBuilder = new ProcessBuilder(args);  
                 Process process = null;  
                 BufferedReader successResult = null;  
                 BufferedReader errorResult = null;  
                 StringBuilder successMsg = new StringBuilder();  
                 StringBuilder errorMsg = new StringBuilder();  
                 int result;  
                 try {  
                         process = processBuilder.start();  
                         successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));  
                         errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));  
                         String s; 
                         while ((s = successResult.readLine()) != null) {  
                                 successMsg.append(s);
                                 Log.d("installSilent", "while successMsg s:" + s);  
                         } 
                         while ((s = errorResult.readLine()) != null) {  
                                 errorMsg.append(s);  
                                 Log.d("installSilent", "while errorMsg s:" + s);  
                         }  
                 } catch (IOException e) {  
                         e.printStackTrace();  
                 } catch (Exception e) {  
                         e.printStackTrace();  
                 } finally {  
                         try {  
                                 if (successResult != null) {  
                                         successResult.close();  
                                 }  
                                 if (errorResult != null) {  
                                         errorResult.close();  
                                 }  
                         } catch (IOException e) {  
                                 e.printStackTrace();  
                         }  
                         if (process != null) {  
                                 process.destroy();  
                         }  
                 }  
    
                 if (successMsg.toString().contains("Success") || successMsg.toString().contains("success")) {  
                         result = 0;  
                 } else {  
                         result = 2;  
                 }  
                 Log.d("installSilent", "successMsg:" + successMsg + ", ErrorMsg:" + errorMsg);  
                 return result;  
         }
    
    
    
    
  4. 有客户提出应用静默安装升级本身后希望可以自启动,下面提供自启动方案:

// AndroidManifest.xml
        <receiver android:name="com.example.testfile.BootBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REPLACED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />
                <data android:scheme="package" />
            </intent-filter>
        </receiver>


//BootBroadcastReceiver.java
package com.example.testfile;

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

public class BootBroadcastReceiver extends BroadcastReceiver{
	private static final String TAG = "sjft";

	public static final String EXTRA_VOLUME_STATE =
			"android.os.storage.extra.VOLUME_STATE";

	public static final int STATE_UNMOUNTED = 0;
	public static final int STATE_CHECKING = 1;
	public static final int STATE_MOUNTED = 2;
	public static final int STATE_MOUNTED_READ_ONLY = 3;
	public static final int STATE_FORMATTING = 4;
	public static final int STATE_EJECTING = 5;
	public static final int STATE_UNMOUNTABLE = 6;
	public static final int STATE_REMOVED = 7;
	public static final int STATE_BAD_REMOVAL = 8;

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String action = intent.getAction();
		if (action.equals("android.intent.action.PACKAGE_REPLACED")){
			String packageName = intent.getData().getSchemeSpecificPart();
			Log.v(TAG,"BootBroadcastReceiver packageName:"+packageName);
			if(context.getPackageName().equals(packageName)){
				Intent launchIntent = new Intent(context, MainActivity.class);//重新启动应用
				//此处如果不想写死启动的Activity,也可以通过如下方法获取默认的启动Activity
				//Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
				intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(launchIntent);
			}
		}
	}

你可能感兴趣的:(Android)