Android 安装assets目录下的其他apk

在实际开发中,可能会遇到将其他apk打包在自己的应用里面一起安装,下面来看一下具体步骤:

首先将test.apk拷贝到main/assets目录下,在清单文件中申请读写存储卡的权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
另外6.0以上的手机还需要在代码中申请动态权限:

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
    || ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE);
}		
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
   // super.onRequestPermissionsResult(requestCode, permissions, grantResults);
	if (requestCode == REQUEST_CODE) {
		if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
         ...
        } else {
            finish();
        }
        return;
		}
}	
为了兼容7.0以上的手机,要使用到provider,在清单文件中application节点下添加provider:
<manifest>
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.app.fileprovider" //自定义名字 为避免重复建议设为:包名.fileprovider
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"
                />
        </provider>
        ...
    </application>
</manifest>
然后在res目录下新建xml文件夹,在xml文件夹中新建provider_paths.xml文件(对应provide中的resource):

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!--"."表示所有路径-->
    <external-path name="external_files" path="."/>
	<!--填写自己的包名-->
    <external-path path="Android/data/com.example.app/" name="share_files_path" />
    <external-path path="." name="root_path" />
</paths>


ok,准备工作完成了,下面正式开始:
把test.apk从assets目录下拷贝到存储卡中:

/**
 * 复制文件到SD卡
 * @param context
 * @param fileName 复制的文件名
 * @param path  保存的目录路径
 * @return
 */
public static Uri copyAssetsFile(Context context, String fileName, String path) {
	try {
		InputStream mInputStream = context.getAssets().open(fileName);
		File file = new File(path);
		if (!file.exists()) {
			file.mkdir();
		}
		File mFile = new File(path + File.separator + "test.apk");
		if(!mFile.exists())
			mFile.createNewFile();
		LogUtil.e(TAG,"开始拷贝");
		FileOutputStream mFileOutputStream = new FileOutputStream(mFile);
		byte[] mbyte = new byte[1024];
		int i = 0;
		while((i = mInputStream.read(mbyte)) > 0){
			mFileOutputStream.write(mbyte, 0, i);
		}
		mInputStream.close();
		mFileOutputStream.close();
		Uri uri = null;
		try{
			if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
				//包名.fileprovider
				uri = FileProvider.getUriForFile(context, "com.example.app.fileprovider", mFile);
			}else{
				uri = Uri.fromFile(mFile);
			}
		}catch (ActivityNotFoundException anfe){
			LogUtil.e(TAG,anfe.getMessage());
		}
		MediaScannerConnection.scanFile(context, new String[]{mFile.getAbsolutePath()}, null, null);
		LogUtil.e(TAG,"拷贝完毕:" + uri);
		return uri;
	} catch (IOException e) {
		e.printStackTrace();
		Log.e(TAG, fileName + "not exists" + "or write err");
		return null;
	} catch (Exception e) {
		return null;
	}
}

然后安装test.apk,
/**
 * 安装apk
 * @param uri apk存放的路径
 * @param context
 */
public static void openApk(Uri uri, Context context) {
	Intent intent = new Intent();
	intent.setAction(Intent.ACTION_VIEW);
	if (Build.VERSION.SDK_INT >= 24) {
		intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
		intent.setDataAndType(uri, "application/vnd.android.package-archive");
	} else {
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	}
	intent.setDataAndType(uri, "application/vnd.android.package-archive");
	context.startActivity(intent);
}
最后可以检测有没有安装test.apk:

/**
 * App是否已安装
 * @param mContext
 * @param packageName 包名
 * @return
 */
private boolean isAppInstall(Context mContext, String packageName){
	PackageInfo mInfo;
	try {
		mInfo = mContext.getPackageManager().getPackageInfo(packageName, 0 );
	} catch (Exception e) {
		// TODO: handle exception
		mInfo = null;
		Log.i(TAG, "没有发现安装的包名");
	}
	if(mInfo == null){
		return false;
	}else {
		return true;
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值