应用内内嵌apk

在应用内安装外部应用:

1.apk形式:

Intent intent = new Intent(Intent.ACTION_VIEW);
//filePath   apk的路径
intent.setDataAndType(Uri.parse("file://" + filePath),
      "application/vnd.android.package-archive");
((Activity) context).startActivityForResult(intent, INSTALL);

2.zip格式

使用一个AsyncTask,下面是doInBackground中的方法。

long extractedSize = 0L;
Enumeration<ZipEntry> entries;
ZipFile zip = null;
try {
   zip = new ZipFile(mInput);
   long uncompressedSize = getOriginalSize(zip);
   //在AsyncTask的doInbacjground方法中调用   主线程中执行
   publishProgress(0, (int) uncompressedSize);

   entries = (Enumeration<ZipEntry>) zip.entries();
   while (entries.hasMoreElements()) {
      ZipEntry entry = entries.nextElement();
      if (entry.isDirectory()) {
         continue;
      }
      File destination = new File(mOutput, entry.getName());
      if (!destination.getParentFile().exists()) {
         destination.getParentFile().mkdirs();
      }
      ProgressReportingOutputStream outStream = new ProgressReportingOutputStream(
            destination);
      //ProgressReportingOutputStream 中重写了write方法,

	/*
	@Override
	public void write(byte[] buffer, int byteOffset, int byteCount)
     	 throws IOException {
   		super.write(buffer, byteOffset, byteCount);
   		mProgress += byteCount;
   		publishProgress(mProgress);
	}
	*/
      extractedSize += copy(zip.getInputStream(entry), outStream);
      outStream.close();
   }
} catch (ZipException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
} finally {
   try {
      zip.close();
   } catch (IOException e) {
      e.printStackTrace();
   }
}


打开外部的应用:

PackageInfo pi;
    try {
	//packageName  应用的包名
pi = context.getPackageManager().getPackageInfo(packageName, 0); Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); resolveIntent.setPackage(pi.packageName);
	//查询到的信息列表
        List<ResolveInfo> apps = context.getPackageManager()
                .queryIntentActivities(resolveIntent, 0);
        if (apps != null && !apps.isEmpty()) {
            ResolveInfo ri = apps.iterator().next();
            if (ri != null) {
		//匹配到的应用
                String mPackageName = ri.activityInfo.packageName;
                String mClassName = ri.activityInfo.name;

                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
		//以外部应用的形式打开
                ComponentName cn = new ComponentName(mPackageName,
                        mClassName);
                intent.setComponent(cn);
                context.startActivity(intent);
            }
        }
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值