android中实现多个apk文件。

来源:互联网 发布:手机淘宝 我的尺码 编辑:程序博客网 时间:2024/05/22 06:41


http://blog.csdn.net/zhuyouleixuexi/article/details/7450055


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

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

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


java代码:

[html] view plain
  1. package cn.onecomm.zhenghe.activity;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import android.app.Activity;  
  11. import android.app.AlertDialog;  
  12. import android.app.AlertDialog.Builder;  
  13. import android.content.BroadcastReceiver;  
  14. import android.content.ComponentName;  
  15. import android.content.Context;  
  16. import android.content.DialogInterface;  
  17. import android.content.Intent;  
  18. import android.content.IntentFilter;  
  19. import android.content.pm.PackageInfo;  
  20. import android.content.pm.PackageManager;  
  21. import android.net.Uri;  
  22. import android.os.Bundle;  
  23. import android.util.Log;  
  24. import android.view.View;  
  25. import android.view.View.OnClickListener;  
  26. import android.widget.ImageView;  
  27. import android.widget.Toast;  
  28.   
  29. public class MainActivity extends Activity {  
  30.     private ImageView baoxian_zhushou;  
  31.     ArrayList<String> packagNameList;  
  32.     private MyReceiver receiver;  
  33.   
  34.     public void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.main);  
  37.   
  38.         initpackagNameList();  
  39.   
  40.         // 监听系统新安装程序的广播  
  41.         receiver = new MyReceiver();  
  42.         IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);// 注册广播机制  
  43.         filter.addDataScheme("package"); // 必须添加这项,否则拦截不到广播  
  44.         registerReceiver(receiver, filter);  
  45.   
  46.         baoxian_zhushou = (ImageView) findViewById(R.id.baoxian_zhushou);  
  47.                  
  48.                  // 主页面小模块的图标  
  49.         baoxian_zhushou.setOnClickListener(new OnClickListener() {  
  50.             public void onClick(View v) {  
  51.   
  52.                 // 检查是否已经安装  
  53.                 Log.d("time", "clicked start " + System.currentTimeMillis()  
  54.                         + "");  
  55.                 boolean installed = detectApk("cn.oncomm.activity");  
  56.   
  57.                 if (installed) {// 已经安装直接起动  
  58.                     Log.d("time", "getPackageManager start "  
  59.                             + System.currentTimeMillis() + "");  
  60.   
  61.                     Intent intent = new Intent();  
  62.                     // 组件名称,第一个参数是包名,也是主配置文件Manifest里设置好的包名 第二个是类名,要带上包名  
  63.                     intent.setComponent(new ComponentName("cn.oncomm.activity",  
  64.                             "cn.oncomm.activity.MailActivity"));  
  65.                     intent.setAction(Intent.ACTION_VIEW);  
  66.   
  67.                     Log.d("time", "setAction start "  
  68.                             + System.currentTimeMillis() + "");  
  69.                     startActivity(intent);  
  70.   
  71.                 } else {// 未安装先安装  
  72.                     //  
  73.                     // get the cacheDir.  
  74.                     File fileDir = getFilesDir();  
  75.                     final String cachePath = fileDir.getAbsolutePath()  
  76.                             + "/pingAnAccident3.0.apk";  
  77.                     retrieveApkFromAssets(MainActivity.this,  
  78.                             "pingAnAccident3.0.apk", cachePath);  
  79.                     showInstallConfirmDialog(MainActivity.this, cachePath);  
  80.                 }  
  81.             }  
  82.         });  
  83.     }  
  84.   
  85.     // 捆绑安装  
  86.     public boolean retrieveApkFromAssets(Context context, String fileName,  
  87.             String path) {  
  88.         boolean bRet = false;  
  89.   
  90.         try {  
  91.             File file = new File(path);  
  92.             if (file.exists()) {  
  93.                 return true;  
  94.             } else {  
  95.                 file.createNewFile();  
  96.                 InputStream is = context.getAssets().open(fileName);  
  97.                 FileOutputStream fos = new FileOutputStream(file);  
  98.   
  99.                 byte[] temp = new byte[1024];  
  100.                 int i = 0;  
  101.                 while ((i = is.read(temp)) != -1) {  
  102.                     fos.write(temp, 0, i);  
  103.                 }  
  104.                 fos.flush();  
  105.                 fos.close();  
  106.                 is.close();  
  107.   
  108.                 bRet = true;  
  109.             }  
  110.   
  111.         } catch (IOException e) {  
  112.             Toast.makeText(context, e.getMessage(), 2000).show();  
  113.             Builder builder = new Builder(context);  
  114.             builder.setMessage(e.getMessage());  
  115.             builder.show();  
  116.             e.printStackTrace();  
  117.         }  
  118.   
  119.         return bRet;  
  120.     }  
  121.   
  122.     /**  
  123.      *提示用户安装程序  
  124.      */  
  125.     public void showInstallConfirmDialog(final Context context,  
  126.             final String filePath) {  
  127.         AlertDialog.Builder tDialog = new AlertDialog.Builder(context);  
  128.         tDialog.setIcon(R.drawable.info);  
  129.         tDialog.setTitle("未安装该程序");  
  130.         tDialog.setMessage("请安装该程序");  
  131.   
  132.         tDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  133.   
  134.             public void onClick(DialogInterface dialog, int which) {  
  135.   
  136.                 // 修改apk权限  
  137.                 try {  
  138.                     String command = "chmod " + "777" + " " + filePath;  
  139.                     Runtime runtime = Runtime.getRuntime();  
  140.                     runtime.exec(command);  
  141.                 } catch (IOException e) {  
  142.                     e.printStackTrace();  
  143.                 }  
  144.                 // install the apk.  
  145.                 Intent intent = new Intent(Intent.ACTION_VIEW);  
  146.                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  147.                 intent.setDataAndType(Uri.parse("file://" + filePath),  
  148.                         "application/vnd.android.package-archive");  
  149.                 context.startActivity(intent);  
  150.   
  151.             }  
  152.         });  
  153.   
  154.         tDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
  155.   
  156.             public void onClick(DialogInterface dialog, int which) {  
  157.             }  
  158.         });  
  159.   
  160.         tDialog.show();  
  161.     }  
  162.   
  163.     /**  
  164.      * 检测是否已经安装  
  165.      *   
  166.      * @param packageName  
  167.      * @return true已安装 false未安装  
  168.      */  
  169.     private boolean detectApk(String packageName) {  
  170.         return packagNameList.contains(packageName.toLowerCase());  
  171.   
  172.     }  
  173.   
  174.     private void initpackagNameList() {  
  175.         // 初始化小模块列表  
  176.         packagNameList = new ArrayList<String>();  
  177.         PackageManager manager = this.getPackageManager();  
  178.         List<PackageInfo> pkgList = manager.getInstalledPackages(0);  
  179.         for (int i = 0; i < pkgList.size(); i++) {  
  180.             PackageInfo pI = pkgList.get(i);  
  181.             packagNameList.add(pI.packageName.toLowerCase());  
  182.         }  
  183.   
  184.     }  
  185.   
  186.     /**  
  187.      *   
  188.      * 设置广播监听  
  189.      *   
  190.      */  
  191.     private class MyReceiver extends BroadcastReceiver {  
  192.         public void onReceive(Context context, Intent intent) {  
  193.             if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {  
  194.   
  195.                 String packName = intent.getDataString().substring(8);  
  196.   
  197.                 Log.e(intent.getDataString() + "====", packName);  
  198.                 // package:cn.oncomm.activity cn.oncomm.activity  
  199.                 // packName为所安装的程序的包名  
  200.                 packagNameList.add(packName.toLowerCase());  
  201.   
  202.                 // 删除file目录下的所有以安装的apk文件  
  203.                 File file = getFilesDir();  
  204.                 File[] files = file.listFiles();  
  205.                 for (File f : files) {  
  206.                     if (f.getName().endsWith(".apk")) {  
  207.                         f.delete();  
  208.                     }  
  209.                 }  
  210.   
  211.             }  
  212.         }  
  213.     }  
  214. }  

主页面的
[html] view plain
  1. main.xml文件:  

[html] view plain
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/layContain" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:orientation="horizontal">  
  5.     <!-- android:background="#FFC0CB"-->  
  6.   
  7.   
  8.     <LinearLayout android:id="@+id/layFirst"  
  9.         android:layout_width="400px" android:layout_height="fill_parent"  
  10.         android:orientation="vertical" android:layout_marginBottom="50dp">  
  11.   
  12.         <LinearLayout android:layout_width="fill_parent"  
  13.             android:layout_height="fill_parent" android:orientation="vertical">  
  14.   
  15.             <LinearLayout android:layout_width="fill_parent"  
  16.                 android:layout_height="wrap_content" android:orientation="horizontal"  
  17.                 android:layout_marginTop="30dp">  
  18.    
  19.                 <LinearLayout android:layout_width="wrap_content"  
  20.                     android:layout_height="wrap_content" android:layout_weight="1"  
  21.                     android:gravity="center_horizontal" android:orientation="vertical">  
  22.   
  23.                     <ImageView android:id="@+id/baoxian_zhushou"  
  24.                         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  25.                         android:src="@drawable/icon" />  
  26.                     <TextView android:layout_width="wrap_content"  
  27.                         android:layout_height="wrap_content" android:text="平安助手"  
  28.                           />  
  29.   
  30.                 </LinearLayout>  
  31.                   
  32.                                   
  33.             </LinearLayout>  
  34.         </LinearLayout>  
  35.     </LinearLayout>  
  36. </LinearLayout>  

0 0
原创粉丝点击