`

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>

分享到:
评论
1 楼 Sunnyfans 2013-04-20  
哥们,谢谢分享!

相关推荐

    Android系统中的.apk文件和dex文件

    APK是Android Package的缩写,即Android安装包。APK是类似Symbian Sis或Sisx的文件格式,PC上的安装文件的后缀名为“.exe”。通过将APK文件直接传到Android模拟器或Android手机中执行即可安装。

    android批量打包生成apk

    当你要将多个应用发布到多个推广渠道的时候,你会针对每一个渠道为android应用添加相应的渠道号,这样工作量会很大。这时你会想:如果有某种工具能针对不同的渠道批量生成apk就好了。这个时候就要求你去写个打包工具...

    APk 编辑器 Android

    APK编辑器是八神智能天下编程组成员罗茂才出品的一个可以编辑apk文件的软件。它包括对apk文件的解包、打包、签名、汉化、应用共存制作、文字翻译等等操作!而本软件也是手机端首个,Android汉化软件,让你在没有电脑...

    apk编辑器电脑版

    如果要开发的话需要SDK开发工具等等我们不太熟悉的东西,但是我们可以通过一个APK manager软件,先解压缩apk文件然后对其进行编辑,然后再打包,最后签名。然后就可以安装自己修改过的apk文件了。 1.普通apk文件...

    Android 连网下载APK文件并自动安装.rar

    Android 连网下载APK文件并自动安装的例子,这个例子在智能手机上已经不是个新鲜功能了,那么具体是如何实现的呢?本源码会告诉你答案,而且代码中的注释也是非常多的,对于学会此技巧很有帮助。下面是代码片段:  ...

    解决Android单个dex文件不能超过65535个方法的类库

    Android 5.0(API leve 21)和更高的系统使用runtime是ART ,原生支持从应用的apk文件加载多个DEX文件。ART在安装应用时预编译应用程序,会扫描多个classes(..N).dex文件编译成一个.oat的文件。更多Android5.0 ...

    Android Studio打包APK文件具体实现步骤解析

    那么今天我们就来讲讲如何通过Android Studio打包APK文件,相信有很多用户还不是非常了解,下面通过这篇文章给大家介绍一下。 Android Studio软件版本:2.3.0.8 官方版编程开发立即查看 前面一直使用的是out文件夹...

    Android 360apk 美团apk打包工具.zip

    软件介绍: 360apk打包工具MCPTool使用方法1. 将apk文件放到MCPTool目录下2. 将apk文件拖拽到MCPTool.bat上3. 执行完成后会在当前目录下生成各个...命令执行完毕后会生成一个文件夹,里面存放的就是多渠道打包的apk

    Android文件管理器源码

    Android文件管理器(增加了文件夹复制移动,下载资源暂停删除等,以及复制过程中的可视化进程)是一个基于Android开发的应用,包含常用文件操作以及文件下载功能,文件操作包括打开文件夹和打开各类的文件(apk、avi...

    Android APK+Dex文件反编译及回编译工具v2.0.2

    新增,反编译jar文件时,自动识别是否含多个dex文件,并使用ShakaApktool反编译 *.新增,反编译JAR文件后,回编译时自动合并并转移到反编译目录内 右键[APK文件]、[RSA文件]、[original]目录、[META-INF]目录 可...

    android 多个工程 打包

    一个简单的Demo,告诉大家,如何将相互关联的多个APK打包为一个APK文件!运行FeatherApp即可!

    Android完美解决listview中多个edittext显示混乱问题

    Android完美解决listview中多个edittext显示混乱问题,内含源码及apk文件

    android实现语音朗读 支持中文朗读 TTS 讯飞语音库

    我采用的是讯飞语音库,手机版本2.2-4.0需要安装2个讯飞语音库APK文件,版本4.0以上只需要安装一个APK。APK安装成功后,打开手机设置(我的手机SDK是4.0.4版本)-&gt;语言和输入法-&gt;文字转语音(TTS)输出-&gt;...

    安卓APK导出的dex转jar文件

    首先将apk文件后缀改为zip并解压,得到其中的classes.dex,它就是java文件编译再通过dx工具打包而成的,将classes.dex复制到dex2jar.bat所在目录dex2jar 文件夹。 在命令行下定位到dex2jar.bat所在目录,运行 dex2...

    Android Studio 一个工程打包多个不同包名的APK实例详解

    公司最近有个特别的需求,同一套代码,稍做修改(如包名不一样,图标不一样,应用名不一样等),编译出几... // 设置applicationId(这里很重要,两个相同applicationId的apk不同同时安装在同一台Android手机中) app

    Android TCP Socket通信实例Demo源码Apk下载

    本Demo主要实现了安卓(Android)TCP 客户端(Client)和服务器(Server)Demo的Socket通讯。以及对接硬件的项目数据在十六进制&&byte&&int的转换处理。 要注意BufferedReader的readLine()方法的阻塞问题: 读取socket输入...

    Android Studio 打包生成APK文件方法

    那么今天我们就来讲讲如何通过Android Studio打包APK文件,相信有很多用户还不是非常了解,下面通过这篇文章给大家介绍一下。 前面一直使用的是out文件夹里面的那个apk文件(debug版本),最近在使用在线升级的时候...

    Android APK xml 批量解密工具

    android开发中有时会想研究借鉴一下设计思路和UI风格,但解压apk包后 layout文件夹下xml文件一般都是加密的。在网上找到了一个工具,可以完美地解密xml文件,但这个工具的使用方法是:调cmd 然后...

    Android多渠道打包工具,支持V1和V2签名的apk

    该jar包可直接用命令行运行,命令为:java -jar writechanneltool.jar -sourceApk test.apk -channelFile channel.txt,test.apk为待签名apk,channel.txt为渠道信息文件,每个渠道占一行,支持#开头的注释,该工具...

    APK 编辑器 APK Editor Studio 1.4.0 中文多语.zip

    此外,您可以篡改多个 Android 清单属性,而无需分析其内部结构。 选择感兴趣的 APK 第一步是选择需要处理的包装。它可以位于计算机或 Android 设备上。 您只需要通过专用USB电缆将其连接到PC,即可立即获得有关它...

Global site tag (gtag.js) - Google Analytics