After finding an instance on the internet, you constantly expand the instance, but this installation is suitable for portal website development and some simple external APK (this APK generally only has one activity ), because the context of this APK is transmitted from the primary APK, many resources must use the primary APK, unless you define some resources in the secondary APK (for example, the layout file is either defined in the primary APK, there are instances below, or you can use the Java write control to load in the secondary APK, for example, to load a resource file, either define a direct call in the main APK, or write Chinese and English in the class according to the current configuration language ), after the context is passed, other activities cannot be called.
The Code is as follows:
The main APK project is as follows:
Package com. Android. Reflect. Main;
Import java. Lang. Reflect. constructor;
Import java. Lang. Reflect. method;
Import Android. App. activity;
Import Android. content. PM. packageinfo;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Dalvik. system. dexclassloader;
Public class mainactivity extends activity {
Private Static final string tag = "mainactivity ";
Private class mloadclass = NULL;
Private object mloadistance = NULL;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button BTN = (button) findviewbyid (R. Id. BTN );
BTN. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Bundle parambundle = new bundle ();
Parambundle. putboolean ("key_start_from_other_activity", true );
// String dexpath = "/sdcard/testb.apk ";
String dexpath = "/sdcard/testb.apk ";
String dexoutputpath = "/sdcard /";
Loadapk (parambundle, dexpath, dexoutputpath );
}
});
}
Public void loadapk (bundle parambundle, string dexpath, string dexoutputpath ){
Classloader localclassloader = classloader. getsystemclassloader ();
Dexclassloader localdexclassloader = new dexclassloader (dexpath,
Dexoutputpath, null, localclassloader );
Try {
Packageinfo plocalobject = getpackagemanager ()
. Getpackagearchiveinfo (dexpath, 1 );
If (plocalobject. Activities! = NULL)
& (Plocalobject. Activities. length> 0 )){
String activityname = plocalobject. Activities [0]. Name;
Log. D (TAG, "activityname =" + activityname );
Mloadclass = localdexclassloader. loadclass (activityname );
Constructor localconstructor = mloadclass
. Getconstructor (new class [] {});
Mloadistance = localconstructor. newinstance (new object [] {});
Log. D (TAG, "instance =" + mloadistance );
Method localmethodsetactivity = mloadclass. getdeclaredmethod (
"Setactivity", new class [] {activity. Class });
Localmethodsetactivity. setaccessible (true );
Localmethodsetactivity. Invoke (mloadistance, new object [] {This });
Method methodoncreate = mloadclass. getdeclaredmethod (
"Oncreate", new class [] {bundle. Class });
Methodoncreate. setaccessible (true );
Methodoncreate. Invoke (mloadistance, new object [] {parambundle });
}
Return;
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
/* Public applicationinfo getapplicationinfo (URI packageuri ){
Final string archivefilepath = packageuri. getpath ();
Packageparser = new packageparser (archivefilepath );
File sourcefile = new file (archivefilepath );
Displaymetrics metrics = new displaymetrics ();
Metrics. settodefaults ();
Packageparser. Package PKG = packageparser. parsepackage (sourcefile, archivefilepath, metrics, 0 );
If (PKG = NULL ){
Return NULL;
}
Return PKG. applicationinfo;
}*/
@ Override
Protected void onstart (){
Super. onstart ();
Try {
Method method = mloadclass. getdeclaredmethod (
"Onstart ");
Method. setaccessible (true );
Method. Invoke (mloadistance );
} Catch (exception e ){
E. printstacktrace ();
}
}
@ Override
Protected void onresume (){
Super. onresume ();
Try {
Method method = mloadclass. getdeclaredmethod (
"Onresume ");
Method. setaccessible (true );
Method. Invoke (mloadistance );
} Catch (exception e ){
E. printstacktrace ();
}
}
@ Override
Protected void onpause (){
Super. onpause ();
Try {
Method method = mloadclass. getdeclaredmethod (
"Onpause ");
Method. setaccessible (true );
Method. Invoke (mloadistance );
} Catch (exception e ){
E. printstacktrace ();
}
}
@ Override
Protected void onstop (){
Super. onstop ();
Try {
Method method = mloadclass. getdeclaredmethod (
"Onstop ");
Method. setaccessible (true );
Method. Invoke (mloadistance );
} Catch (exception e ){
E. printstacktrace ();
}
}
@ Override
Protected void ondestroy (){
Super. ondestroy ();
Try {
Method method = mloadclass. getdeclaredmethod (
"Ondestroy ");
Method. setaccessible (true );
Method. Invoke (mloadistance );
} Catch (exception e ){
E. printstacktrace ();
}
}
}
22222222222222222222222222222
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Android. Reflect. Main"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Uses-SDK Android: minsdkversion = "7"/>
<Uses-Permission Android: Name = "android. Permission. write_external_storage"> </uses-Permission>
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = "com. Android. Reflect. Main. mainactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
</Application>
</Manifest>
333333333333
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button Android: text = "Start Activity B"
Android: layout_width = "wrap_content"
Android: Id = "@ + ID/BTN"
Android: layout_height = "wrap_content">
</Button>
<Button Android: text = "Start Activity B"
Android: layout_width = "wrap_content"
Android: Id = "@ + ID/btna"
Android: layout_height = "wrap_content">
</Button>
<Button Android: text = "Start Activity B"
Android: layout_width = "wrap_content"
Android: Id = "@ + ID/btnb"
Android: layout_height = "wrap_content">
</Button>
<Button Android: text = "Start Activity B"
Android: layout_width = "wrap_content"
Android: Id = "@ + ID/btnc"
Android: layout_height = "wrap_content">
</Button>
</Linearlayout>
444444444
Mainlayout. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical"
Android: Id = "@ + ID/main"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "cccccccccccccc"
Android: Background = "@ drawable/icon"
/>
<Button Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "cccccccccccccc"
Android: Background = "@ drawable/icon"
Android: Id = "@ + ID/btnclick"
/>
</Linearlayout>
The code for the next APK is as follows:
Package com. Android. Reflect;
Import Android. App. activity;
Import Android. Graphics. color;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. surfaceview;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. linearlayout;
Import Android. widget. linearlayout. layoutparams;
Import Android. widget. textview;
Import Android. widget. Toast;
Public class testbactivity extends activity {
Private Static final string tag = "testbactivity ";
Private activity otheractivity;
Private surfaceview msurfaceview;
Private linearlayout rootlayout;
Private textview mtextview;
Private view mmainview;
Private void initview (){
Linearlayout. layoutparams paramlayoutparams = new layoutparams (layoutparams. fill_parent, layoutparams. fill_parent );
Rootlayout = new linearlayout (this. otheractivity );
Rootlayout. setlayoutparams (paramlayoutparams );
Mtextview = new textview (this. otheractivity );
Mtextview. setlayoutparams (New layoutparams (layoutparams. wrap_content, layoutparams. wrap_content ));
Mtextview. settextcolor (color. Green );
Mtextview. settextsize (30 );
Mtextview. settext ("cccccccccc ");
Mtextview. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v) {// getresource is mainactivity Resoures
// Right toast. maketext (otheractivity, otheractivity. getresources (). getstring (R. String. app_name), Toast. length_long). Show ();
// Error toast. maketext (otheractivity, testbactivity. This. getresources (). getstring (R. String. app_name), Toast. length_long). Show ();
/* Intent = new intent ();
Intent. setaction ("AA ");
Startactivity (intent );*/
}
});
Rootlayout. addview (mtextview );
Int layoutid = otheractivity. getresources (). getidentifier ("mainlayout", "layout", "com. Android. Reflect. Main ");
Mmainview = otheractivity. getlayoutinflater (). Inflate (layoutid, null );
Int btnclick = otheractivity. getresources (). getidentifier ("btnclick", "ID", "com. Android. Reflect. Main ");
Button mbtn = (button) mmainview. findviewbyid (btnclick );
Mbtn. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
Toast. maketext (otheractivity, otheractivity. getresources (). getstring (R. String. app_name), Toast. length_long). Show ();
}
});
Msurfaceview = new tbsurfaceview (this. otheractivity );
}
@ Override
Public void oncreate (bundle savedinstancestate ){
Boolean B = false;
If (savedinstancestate! = NULL ){
B = savedinstancestate. getboolean ("key_start_from_other_activity", false );
If (B ){
Initview ();
// This. otheractivity. setcontentview (msurfaceview );
// This. otheractivity. setcontentview (rootlayout );
This. otheractivity. setcontentview (mmainview );
}
}
Log. I (TAG, "= oncreate = B =" + B );
If (! B ){
Super. oncreate (savedinstancestate );
// Setcontentview (R. layout. Main );
Setcontentview (New tbsurfaceview (this ));
}
}
Public void setactivity (activity paramactivity ){
Log. D (TAG, "setactivity..." + paramactivity );
This. otheractivity = paramactivity;
}
}
2222222222222222222222
Package com. Android. Reflect;
Import Android. content. context;
Import Android. Graphics. Canvas;
Import Android. Graphics. color;
Import Android. Graphics. paint;
Import Android. View. surfaceholder;
Import Android. View. surfaceholder. Callback;
Import Android. View. surfaceview;
Public class tbsurfaceview extends surfaceview implements callback, runnable {
Private surfaceholder SFH;
Private thread th;
Private canvas;
Private paint;
Public tbsurfaceview (context ){
Super (context );
Th = new thread (this );
SFH = This. getholder ();
SFH. addcallback (this );
Paint = new paint ();
Paint. setantialias (true );
Paint. setcolor (color. Red );
This. setkeepscreenon (true );
}
Public void surfacecreated (surfaceholder holder ){
Th. Start ();
}
Private void draw (){
Try {
Canvas = SFH. lockcanvas ();
If (canvas! = NULL ){
Canvas. drawcolor (color. White );
Canvas. drawtext ("time:" + system. currenttimemillis (), 100,
100, paint );
}
} Catch (exception ex ){
Ex. printstacktrace ();
} Finally {
If (canvas! = NULL ){
SFH. unlockcanvasandpost (canvas );
}
}
}
Public void run (){
While (true ){
Draw ();
Try {
Thread. Sleep (100 );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
Public void surfacechanged (surfaceholder holder, int format, int width,
Int height ){
}
Public void surfacedestroyed (surfaceholder holder ){
}
}
333333333333
Package com. Android. Reflect;
Import Android. App. activity;
Import Android. OS. Bundle;
Public class secondactivity extends activity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
// Todo auto-generated method stub
Super. oncreate (savedinstancestate );
}
@ Override
Protected void onstart (){
// Todo auto-generated method stub
Super. onstart ();
}
@ Override
Protected void onrestart (){
// Todo auto-generated method stub
Super. onrestart ();
}
}
4444444444444
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Android. Reflect"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Uses-SDK Android: minsdkversion = "7"/>
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". testbactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
<Activity Android: Name = ". secondactivity">
<Intent-filter>
<Action Android: Name = "AA"/>
</Intent-filter>
</Activity>
</Application>
</Manifest>
5555555
Clayout. XML
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical"
Android: Id = "@ + ID/main"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "cccccccccccccc"
Android: Background = "@ drawable/icon"
/>
<Button Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "cccccccccccccc"
Android: Background = "@ drawable/icon"/>
</Linearlayout>
66666666666
Main. XML
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
</Linearlayout>