天天看點

android中實作多個apk檔案。

有時一個大項目下面會有很多個小子產品,如果小子產品之間沒有聯系,這時可以将每個小子產品作為單獨的項目,生成apk。

這時就涉及到怎麼将多個apk放到一個項目中。

首先,将小子產品生成的apk放到項目的assets檔案夾中

android中實作多個apk檔案。

java代碼:

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. <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. 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. installed = detectApk("cn.oncomm.activity");  
56.   
57.                 if (installed) {// 已經安裝直接起動  
58.                     Log.d("time", "getPackageManager start "  
59.                             + System.currentTimeMillis() + "");  
60.   
61. 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. fileDir = getFilesDir();  
75. 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. bRet = false;  
89.   
90.         try {  
91. file = new File(path);  
92.             if (file.exists()) {  
93.                 return true;  
94.             } else {  
95.                 file.createNewFile();  
96. is = context.getAssets().open(fileName);  
97. fos = new FileOutputStream(file);  
98.   
99. temp = new byte[1024];  
100. i = 0;  
101. 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 = 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. 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. command = "chmod " + "777" + " " + filePath;  
139. runtime = Runtime.getRuntime();  
140.                     runtime.exec(command);  
141.                 } catch (IOException e) {  
142.                     e.printStackTrace();  
143.                 }  
144.                 // install the apk.  
145. 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. manager = this.getPackageManager();  
178. <PackageInfo> pkgList = manager.getInstalledPackages(0);  
179. i = 0; i < pkgList.size(); i++) {  
180. 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. 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 = getFilesDir();  
204. 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檔案:  
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>  
37.