android 动态加载框架 快速使用
来源:互联网 发布:单片机最小系统是什么 编辑:程序博客网 时间:2024/06/04 20:06
其实现在的有很多动态加载框架 360的有一个 还有其他的 我一开始选择的360的 但是他的so 文件兼容性貌似不是很好 我就用的另外一个了
下面是git 的连接
https://github.com/limpoxe/Android-Plugin-Framework
然后 贴一下我的代码 作用就是 打开SD卡 然后选择apk 安装后 再打开
上下效果图
package tongyin.plub.www.mplugintest;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.os.Bundle;import android.os.RemoteException;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;import com.plugin.content.PluginDescriptor;import com.plugin.core.manager.PluginManagerHelper;import com.plugin.util.LogUtil;import com.plugin.util.ResourceUtil;import java.io.File;import java.util.Collection;import java.util.Iterator;public class MainActivity extends Activity { private ViewGroup mList; private Button btSelect; String apkPath; private TextView textView; private Context context; boolean isInstalled = false; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 0 && resultCode == RESULT_OK) { apkPath = data.getStringExtra("apk_path"); textView.setText(apkPath); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this; setContentView(R.layout.activity_main); mList = (ViewGroup) findViewById(R.id.list_1);// listAll(); textView = (TextView) findViewById(R.id.textView); //安装 findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { install(apkPath); } }); //打开 findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { open(apkPath); } }); btSelect = (Button) findViewById(R.id.bt_select); btSelect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); intent.setClass(context, FileExplorerActivity.class); startActivityForResult(intent, 0); } }); } private void open(String apkUrl) { PackageManager pm = context.getPackageManager(); File apk = new File(apkUrl); if (apk != null && apk.exists() && apk.getPath().toLowerCase().endsWith(".apk")) { final PackageInfo info = pm.getPackageArchiveInfo(apk.getPath(), 0); ApkItem apkItem = new ApkItem(context, info, apk.getPath()); //判断本地是否安装插件了 boolean pluginPackage = false; final PackageInfo packageInfo = apkItem.packageInfo; pluginPackage = getPackageName(info.packageName); if (pluginPackage) {//已安装 启动// Intent intent = pm.getLaunchIntentForPackage(info.packageName);// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// context.startActivity(intent); Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(info.packageName); if (launchIntent == null) { //Utils.showToastShort(context, "意图为空"); } else { context.startActivity(launchIntent); } } else { //未安装 安装 } } } private boolean getPackageName(String packageName) { Collection<PluginDescriptor> plugins = PluginManagerHelper.getPlugins(); Iterator<PluginDescriptor> itr = plugins.iterator(); while (itr.hasNext()) { final PluginDescriptor pluginDescriptor = itr.next(); String name = pluginDescriptor.getPackageName(); if (name.equals(packageName)) { return true; } } return false; } private void install(String apk) { if (!isInstalled) { isInstalled = true; try { if (apk.endsWith(".apk")) { PluginManagerHelper.installPlugin(apk); } } catch (Exception e) { e.printStackTrace(); } } else { Toast.makeText(MainActivity.this, "点1次就可以啦!", Toast.LENGTH_LONG).show(); } } private void listAll() { ViewGroup root = mList; root.removeAllViews(); // 列出所有已经安装的插件 Collection<PluginDescriptor> plugins = PluginManagerHelper.getPlugins(); Iterator<PluginDescriptor> itr = plugins.iterator(); while (itr.hasNext()) { final PluginDescriptor pluginDescriptor = itr.next(); Button button = new Button(this); button.setPadding(10, 25, 10, 25); LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutParam.topMargin = 25; layoutParam.bottomMargin = 25; layoutParam.gravity = Gravity.LEFT; root.addView(button, layoutParam); LogUtil.d("插件id:", pluginDescriptor.getPackageName()); button.setText("打开插件:" + ResourceUtil.getLabel(pluginDescriptor) + ", V" + pluginDescriptor.getVersion()); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent launchIntent = getPackageManager().getLaunchIntentForPackage(pluginDescriptor.getPackageName()); if (launchIntent == null) { Toast.makeText(MainActivity.this, "插件" + pluginDescriptor.getPackageName() + "没有配置Launcher", Toast.LENGTH_SHORT).show(); //没有找到Launcher,打开插件详情// Intent intent = new Intent(MainActivity.this, DetailActivity.class);// intent.putExtra("plugin_id", pluginDescriptor.getPackageName());// startActivity(intent); startActivity(launchIntent); } else { //打开插件的Launcher界面 if (!pluginDescriptor.isStandalone()) { //测试向非独立插件传宿主中定义的VO对象// launchIntent.putExtra("paramVO", new SharePOJO("宿主传过来的测试VO")); } startActivity(launchIntent); } //也可以直接构造Intent,指定打开插件中的某个Activity //Intent intent = new Intent("test.abc"); //startActivity(intent); } }); } }}
详细代码 在这里 项目地址 http://download.csdn.net/detail/liudao7994/9628710
0 0
- android 动态加载框架 快速使用
- android动态加载框架使用
- Android动态加载(五)--动态加载框架
- Android 使用动态加载框架DL进行插件化开发
- Android--使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- Android 使用动态加载框架DL进行插件化开发
- 快学Big Data
- Reducing the Dimensionality of Data with Neural Networks:神经网络用于降维
- Java注解总结
- LibRIL 运行机制
- enum 枚举的作用
- android 动态加载框架 快速使用
- Hadoop的容错性
- python 操作MySQL数据库
- C++学习笔记4--表达式
- CI框架教程1——优化验证码机制(或验证码辅助函数)
- CRC的校验原理及硬件、软件算法实现
- OpenCV 基本函数和类型总结
- MVC实现伪静态
- sqlmap tamper 细读