android中 捆绑多个apk文件

来源:互联网 发布:淘宝短信链接 编辑:程序博客网 时间:2024/05/01 06:11

有时一个大项目下面会有很多个小模块,如果小模块之间没有联系,这时可以将每个小模块作为单独的项目,生成apk。

这时就涉及到怎么将多个apk放到一个项目中。

首先,将小模块生成的apk放到项目的assets文件夹中


package cn.onecomm.zhenghe.activity;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.BroadcastReceiver;import android.content.ComponentName;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {private ImageView baoxian_zhushou;ArrayList<String> packagNameList;private MyReceiver receiver;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);initpackagNameList();// 监听系统新安装程序的广播receiver = new MyReceiver();IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);// 注册广播机制filter.addDataScheme("package"); // 必须添加这项,否则拦截不到广播registerReceiver(receiver, filter);baoxian_zhushou = (ImageView) findViewById(R.id.baoxian_zhushou);                                // 主页面小模块的图标baoxian_zhushou.setOnClickListener(new OnClickListener() {public void onClick(View v) {// 检查是否已经安装Log.d("time", "clicked start " + System.currentTimeMillis()+ "");boolean installed = detectApk("cn.oncomm.activity");if (installed) {// 已经安装直接起动Log.d("time", "getPackageManager start "+ System.currentTimeMillis() + "");Intent intent = new Intent();// 组件名称,第一个参数是包名,也是主配置文件Manifest里设置好的包名 第二个是类名,要带上包名intent.setComponent(new ComponentName("cn.oncomm.activity","cn.oncomm.activity.MailActivity"));intent.setAction(Intent.ACTION_VIEW);Log.d("time", "setAction start "+ System.currentTimeMillis() + "");startActivity(intent);} else {// 未安装先安装//// get the cacheDir.File fileDir = getFilesDir();final String cachePath = fileDir.getAbsolutePath()+ "/pingAnAccident3.0.apk";retrieveApkFromAssets(MainActivity.this,"pingAnAccident3.0.apk", cachePath);showInstallConfirmDialog(MainActivity.this, cachePath);}}});}// 捆绑安装public boolean retrieveApkFromAssets(Context context, String fileName,String path) {boolean bRet = false;try {File file = new File(path);if (file.exists()) {return true;} else {file.createNewFile();InputStream is = context.getAssets().open(fileName);FileOutputStream fos = new FileOutputStream(file);byte[] temp = new byte[1024];int i = 0;while ((i = is.read(temp)) != -1) {fos.write(temp, 0, i);}fos.flush();fos.close();is.close();bRet = true;}} catch (IOException e) {Toast.makeText(context, e.getMessage(), 2000).show();Builder builder = new Builder(context);builder.setMessage(e.getMessage());builder.show();e.printStackTrace();}return bRet;}/** *提示用户安装程序 */public void showInstallConfirmDialog(final Context context,final String filePath) {AlertDialog.Builder tDialog = new AlertDialog.Builder(context);tDialog.setIcon(R.drawable.info);tDialog.setTitle("未安装该程序");tDialog.setMessage("请安装该程序");tDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {// 修改apk权限try {String command = "chmod " + "777" + " " + filePath;Runtime runtime = Runtime.getRuntime();runtime.exec(command);} catch (IOException e) {e.printStackTrace();}// install the apk.Intent intent = new Intent(Intent.ACTION_VIEW);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(Uri.parse("file://" + filePath),"application/vnd.android.package-archive");context.startActivity(intent);}});tDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {}});tDialog.show();}/** * 检测是否已经安装 *  * @param packageName * @return true已安装 false未安装 */private boolean detectApk(String packageName) {return packagNameList.contains(packageName.toLowerCase());}private void initpackagNameList() {// 初始化小模块列表packagNameList = new ArrayList<String>();PackageManager manager = this.getPackageManager();List<PackageInfo> pkgList = manager.getInstalledPackages(0);for (int i = 0; i < pkgList.size(); i++) {PackageInfo pI = pkgList.get(i);packagNameList.add(pI.packageName.toLowerCase());}}/** *  * 设置广播监听 *  */private class MyReceiver extends BroadcastReceiver {public void onReceive(Context context, Intent intent) {if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {String packName = intent.getDataString().substring(8);Log.e(intent.getDataString() + "====", packName);// package:cn.oncomm.activity cn.oncomm.activity// packName为所安装的程序的包名packagNameList.add(packName.toLowerCase());// 删除file目录下的所有以安装的apk文件File file = getFilesDir();File[] files = file.listFiles();for (File f : files) {if (f.getName().endsWith(".apk")) {f.delete();}}}}}}
主界面:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/layContain" android:layout_width="fill_parent"android:layout_height="fill_parent" android:orientation="horizontal"><!-- android:background="#FFC0CB"--><LinearLayout android:id="@+id/layFirst"android:layout_width="400px" android:layout_height="fill_parent"android:orientation="vertical" android:layout_marginBottom="50dp"><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent" android:orientation="vertical"><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content" android:orientation="horizontal"android:layout_marginTop="30dp"> <LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_weight="1"android:gravity="center_horizontal" android:orientation="vertical"><ImageView android:id="@+id/baoxian_zhushou"android:layout_width="wrap_content" android:layout_height="wrap_content"android:src="@drawable/icon" /><TextView android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="平安助手"  /></LinearLayout></LinearLayout></LinearLayout></LinearLayout></LinearLayout>


原创粉丝点击