标签:插件开发
dynamic-load插件式开发可以访问没有安装的APK中的类,也就是APK无需安装就可以被打开。具体原理有相关文档参考 http://blog.csdn.net/singwhatiwanna/article/details/22597587
其中有一些缺陷,插件中无法调用系统相册或者相机等,只要是用到intent调用系统acitivity都是行不通的。
dynamic-load有三种开发模式,第一种是主程序与插件没有交互完全独立开发的,第二种是插件可以调用主程序提供的接口(因为插件中不支持.so,service等,需要在主程序中注册,再提供接口给插件中使用),第三种没有去研究,文档中不推荐,也就没怎么看。具体可以看文档介绍。
接下来看demo demo用的是第一种开发模式 demo下载地址
http://download.csdn.net/detail/u012303938/8724767
用法 ,demo中的Camera、ScaleAnimationDemo2、uploadTemp为插件,update为主程序。首先将 Camera.apk,ScaleAnimationDemo2.apk,uploadTemp.apk放入手机SDK 中的DynamicLoadHost文件中,如没有就在内置SDK中创建DynamicLoadHost文件,DynamicLoadHost文件名可以随便改的,必须和主程序中的
String apkPaths=Environment.getExternalStorageDirectory()+"/DynamicLoadHost";DynamicLoadHost一致就行。
如果想在手机上运行安装后的插件,将插件中的plugslibs名字换成libs,就可以安装并正常运行,否则会报错。
在dynamic-load开发中,主程序,插件都要用到dl-lib.jar包,主程序dl-lib只需放在libs中,插件中必须将libs改名,将dl-lib放在改名中的libs中,demo中改名为pulgslibs,并且右键build path——configure buid path——Libraries中点击Add Jars找到自己插件中的pulgslibs里的dl-lib确定,运行出来的apk就可以放在SDK中的DynamicLoadHost文件下,通过主程序运行就可以了。
看demo 主程序
package com.example.update; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import com.example.update.R.drawable; import com.ryg.dynamicload.internal.DLIntent; import com.ryg.dynamicload.internal.DLPluginManager; import com.ryg.dynamicload.internal.DLPluginPackage; import com.ryg.utils.DLUtils; import android.app.Activity; import android.content.pm.PackageInfo; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity{ private ArrayList<Map<String, Object>> list=new ArrayList<Map<String,Object>>(); private GridView gridView1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView1=(GridView) findViewById(R.id.gridView1); //加载在SDK中的所有APK插件 loadData(); } private void loadData() { // TODO Auto-generated method stub String apkPaths=Environment.getExternalStorageDirectory()+"/DynamicLoadHost"; File file=new File(apkPaths); if(!file.exists()){ file.mkdirs(); } //所有apk插件 File[] apkFiles=file.listFiles(); if(apkFiles==null||apkFiles.length==0){ Toast.makeText(this, "插件不存在或路径不对", 1).show(); return; } for(File apkFile:apkFiles){ Map<String, Object> map=new HashMap<String, Object>(); map.put("apkPath", apkFile.getAbsolutePath()); PackageInfo packageInfo=DLUtils.getPackageInfo(this, map.get("apkPath").toString()); map.put("packageInfo", packageInfo); if(packageInfo.activities!=null&&packageInfo.activities.length>0){ map.put("activitysName", packageInfo.activities[0].name); } if(packageInfo.services!=null&&packageInfo.services.length>0){ map.put("servicesName", packageInfo.services[0].name); } //app名称 CharSequence appName=DLUtils.getAppLabel(this,(String)map.get("apkPath")); map.put("appName","app名称: "+appName); //app包名 CharSequence packageName=packageInfo.applicationInfo.packageName; map.put("packageName","app包名: "+packageName); //app图标 Drawable img=DLUtils.getAppIcon(this, (String)map.get("apkPath")); map.put("appImg", img); list.add(map); DLPluginManager.getInstance(this).loadApk(map.get("apkPath").toString()); } SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.apk_item, new String[]{"appName","packageName","appImg"}, new int[]{R.id.textView2,R.id.textView3,R.id.imageView1}); gridView1.setAdapter(adapter); adapter.notifyDataSetChanged(); gridView1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub DLPluginManager dlPluginManager=DLPluginManager.getInstance(MainActivity.this); dlPluginManager.startPluginActivity(MainActivity.this, new DLIntent(((PackageInfo)list.get(position).get("packageInfo")).packageName, (String)list.get(position).get("activitysName"))); } }); } }
package com.example.scaleanimationdemo2; import java.io.ByteArrayOutputStream; import com.ryg.dynamicload.DLBasePluginActivity; import com.ryg.dynamicload.internal.DLIntent; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.Toast; public class MainActivity extends DLBasePluginActivity { Bitmap bitmap; ImageButton imageButton1; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); that.setContentView(R.layout.activity_main); bitmap = BitmapFactory.decodeResource(that.getResources(), R.drawable.xiaohei_big); imageButton1=(ImageButton) findViewById(R.id.imageButton1); imageButton1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // DLIntent dlIntent=new DLIntent(getPackageName(), ShowImagePage.class); // intent传递bitmap ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] bitmapByte = baos.toByteArray(); dlIntent.putExtra("bitmap", bitmapByte); startPluginActivity(dlIntent); // Toast.makeText(that, "adsadad", 1).show(); } }); } }
package com.example.scaleanimationdemo2; import java.io.ByteArrayOutputStream; import com.ryg.dynamicload.DLBasePluginActivity; import com.ryg.dynamicload.internal.DLIntent; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.Toast; public class MainActivity extends DLBasePluginActivity { Bitmap bitmap; ImageButton imageButton1; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); that.setContentView(R.layout.activity_main); bitmap = BitmapFactory.decodeResource(that.getResources(), R.drawable.xiaohei_big); imageButton1=(ImageButton) findViewById(R.id.imageButton1); imageButton1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub // DLIntent dlIntent=new DLIntent(getPackageName(), ShowImagePage.class); // intent传递bitmap ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] bitmapByte = baos.toByteArray(); dlIntent.putExtra("bitmap", bitmapByte); startPluginActivity(dlIntent); // Toast.makeText(that, "adsadad", 1).show(); } }); } }ScaleAnimationDemo2中我用了自定义主题 发现没有用,插件是不支持自定义主题,不过支持系统主题
标签:插件开发
原文地址:http://blog.csdn.net/u012303938/article/details/45892553