项目总结

1,在项目当中嵌入APK;

2,隐藏调用到的APK图标;

在项目中嵌入了两个开源的游戏,之前用最愚蠢的办法把所有代码都复制粘贴到了项目里,费时费力。后来了解到可以在项目当中嵌入APK然后直接调用,这下省了不少力气。

在assets目录下存放你的APK,记得把后缀名换掉,不然在审核过程中会被认定为插件,后缀名是要再代码里修改的。

例如把a.apk换成a.mp3

在这里我们使用输入输出流来控制文件的操作,代码如下:

public void intallApp(Context context,String apkName) {
		try {
		String path = context.getFilesDir().getAbsolutePath()+ "/"+apkName+".akp";  //从assets中解压到这个目录

		File f = new File(path);
		if (!f.exists()) {
		f.createNewFile();
		}
		InputStream is = context.getAssets().open(apkName+".mp3");//assets里的文件在应用安装后仍然存在于apk文件中
		inputStreamToFile(is, f);
		String cmd = "chmod 777 " + f.getAbsolutePath();
		Runtime.getRuntime().exec(cmd);
		cmd = "chmod 777 " + f.getParent();
		Runtime.getRuntime().exec(cmd);
		// 尝试提升上2级的父文件夹权限,在阅读插件下载到手机存储时,刚好用到了2级目录
		// /data/data/packagename/files/这个目录下面所有的层级目录都需要提升权限,才可安装apk,弹出安装界面
		cmd = "chmod 777 " + new File(f.getParent()).getParent();
		Runtime.getRuntime().exec(cmd);
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(android.content.Intent.ACTION_VIEW);

		String type = "application/vnd.android.package-archive";
		/* 设置intent的file与MimeType */
		intent.setDataAndType(Uri.fromFile(f), type);
		context.startActivity(intent);
		} catch (ActivityNotFoundException e) {
		e.printStackTrace();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}


		public void inputStreamToFile(InputStream inputStream,File file){
		///InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
		// read this file into InputStream
		//inputStream = new FileInputStream("test.txt");
		 
		// write the inputStream to a FileOutputStream
		outputStream = new FileOutputStream(file);
		 
		int read = 0;
		byte[] bytes = new byte[1024];
		 
		while ((read = inputStream.read(bytes)) != -1) {
		outputStream.write(bytes, 0, read);
		}
		 
		System.out.println("Done!");
		 
		} catch (IOException e) {
		e.printStackTrace();
		} finally {
		if (inputStream != null) {
		try {
		inputStream.close();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}
		if (outputStream != null) {
		try {
		// outputStream.flush();
		outputStream.close();
		} catch (IOException e) {
		e.printStackTrace();
		}
		 
		}
		}
		}

 在点击启动文件APK是需要执行的方法如下:

//这些代码是启动另外的一个应用程序的主Activity,当然也可以启动任意一个Activity  
	        ComponentName componetName2 = new ComponentName(  
	                //这个是另外一个应用程序的包名  
	                "com.ikras.findsister",  
	                //这个参数是要启动的Activity  
	                "com.ikras.findsister.StartActivity");  
	         
	            try {  
	              //  Intent intent = new Intent();  
	                intent.setComponent(componetName2);  
	                startActivity(intent);  
	            } catch (Exception e) {  
              Toast.makeText(getApplicationContext(), "安装后开始游戏~", 0).show();  
              intallApp(this,game02);   //调用上面的方法,安装文件 
	            } 

 到此完成APK的调用,并安装成功!

安装之后,开发者可能不想让用户在桌面看到自己默认让用户安装的程序,那么我们可以把图标给隐藏了,这个就简单多了。

一个标签搞定。前提是你修改的是嵌入的APK里的AndroidManifest.xml文件,然用户安装的APK图标隐藏。

在启动页的  <intent-filter>  </intent-filter>标签里面加入  <data android:host="WelActivity" android:scheme="whu.iss.sric.android" />

Run as ,图标消失了,要想卸载去应用程序里面就可以看到图标了。

原文地址:https://www.cnblogs.com/yangcong/p/3429990.html