Android, how to use DexClassLoader to dynamically replace an Activity or Service
Posted
by RickNotFred
on Stack Overflow
See other posts from Stack Overflow
or by RickNotFred
Published on 2010-03-25T22:04:58Z
Indexed on
2010/03/28
21:33 UTC
Read the original article
Hit count: 298
I am trying to do something similar to this stackoverflow posting. What I want to do is to read the definition of an activity or service from the SD card. To avoid manifest permission issues, I create a shell version of this activity in the .apk, but try to replace it with an activity of the same name residing on the SD card at run time. Unfortunately, I am able to load the activity class definition from the SD card using DexClassLoader, but the original class definition is the one that is executed. Is there a way to specify that the new class definition replaces the old one, or any suggestions on avoiding the manifest permission issues without actually providing the needed activity in the package? The code sample:
ClassLoader cl = new DexClassLoader("/sdcard/mypath/My.apk",
getFilesDir().getAbsolutePath(),
null,
MainActivity.class.getClassLoader());
try {
Class<?> c = cl.loadClass("com.android.my.path.to.a.loaded.activity");
Intent i = new Intent(getBaseContext(), c);
startActivity(i);
}
catch (Exception e) {
Intead of launching the com.android.my.path.to.a.loaded.activity
specified in /sdcard/mypath/My.apk
, it launches the activity statically loaded into the project.
© Stack Overflow or respective owner