当前位置: 代码迷 >> Android >> android中兑现多个apk文件
  详细解决方案

android中兑现多个apk文件

热度:60   发布时间:2016-05-01 11:47:58.0
android中实现多个apk文件

android中实现多个apk文件??

2012-10-25 16:02:35|??分类:?Android|字号?订阅

?
?

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

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

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

?

android中实现多个apk文件 - Figthing - Figthing的博客
?
java代码:

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;

publicclassMainActivityextendsActivity{
privateImageView baoxian_zhushou;
ArrayList<String> packagNameList;
privateMyReceiver receiver;

publicvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

initpackagNameList();

// 监听系统新安装程序的广播
receiver =newMyReceiver();
IntentFilter filter =newIntentFilter(Intent.ACTION_PACKAGE_ADDED);// 注册广播机制
filter.addDataScheme("package");// 必须添加这项,否则拦截不到广播
registerReceiver(receiver, filter);

baoxian_zhushou =(ImageView) findViewById(R.id.baoxian_zhushou);

&nbsp;// 主页面小模块的图标
baoxian_zhushou.setOnClickListener(newOnClickListener(){
publicvoid 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 =newIntent();
// 组件名称,第一个参数是包名,也是主配置文件Manifest里设置好的包名 第二个是类名,要带上包名
intent.setComponent(newComponentName("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();
finalString cachePath = fileDir.getAbsolutePath()
+"/pingAnAccident3.0.apk";
retrieveApkFromAssets(MainActivity.this,
"pingAnAccident3.0.apk", cachePath);
showInstallConfirmDialog(MainActivity.this, cachePath);
}
}
});
}

// 捆绑安装
publicboolean retrieveApkFromAssets(Context context,String fileName,
String path){
boolean bRet =false;

try{
File file =newFile(path);
if(file.exists()){
returntrue;
}else{
file.createNewFile();
InputStreamis= context.getAssets().open(fileName);
FileOutputStream fos =newFileOutputStream(file);

byte[] temp =newbyte[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 =newBuilder(context);
builder.setMessage(e.getMessage());
builder.show();
e.printStackTrace();
}

return bRet;
}

/**
*提示用户安装程序
*/
publicvoid showInstallConfirmDialog(finalContext context,
finalString filePath){
AlertDialog.Builder tDialog =newAlertDialog.Builder(context);
tDialog.setIcon(R.drawable.info);
tDialog.setTitle("未安装该程序");
tDialog.setMessage("请安装该程序");

tDialog.setPositiveButton("确定",newDialogInterface.OnClickListener(){

publicvoid 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 =newIntent(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("取消",newDialogInterface.OnClickListener(){

publicvoid onClick(DialogInterface dialog,int which){
}
});

tDialog.show();
}

/**
* 检测是否已经安装
*
* @param packageName
* @return true已安装 false未安装
*/
privateboolean detectApk(String packageName){
return packagNameList.contains(packageName.toLowerCase());

}

privatevoid initpackagNameList(){
// 初始化小模块列表
packagNameList =newArrayList<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());
}

}

/**
*
* 设置广播监听
*
*/
privateclassMyReceiverextendsBroadcastReceiver{
publicvoid 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();
}
}

}
}
}
}

主页面的
main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns: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"-->


<LinearLayoutandroid:id="@+id/layFirst"
android:layout_width="400px"android:layout_height="fill_parent"
android:orientation="vertical"android:layout_marginBottom="50dp">

<LinearLayoutandroid:layout_width="fill_parent"
android:layout_height="fill_parent"android:orientation="vertical">

<LinearLayoutandroid:layout_width="fill_parent"
android:layout_height="wrap_content"android:orientation="horizontal"
android:layout_marginTop="30dp">

<LinearLayoutandroid:layout_width="wrap_content"
android:layout_height="wrap_content"android:layout_weight="1"
android:gravity="center_horizontal"android:orientation="vertical">

<ImageViewandroid:id="@+id/baoxian_zhushou"
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:src="@drawable/icon"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"android:text="平安助手"
/>

</LinearLayout>


</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>

  相关解决方案