I need an Android application which should be able to fetch data from the web (maybe an .apk or a .jar) and launch “something” from it.
If it’s a “trivial” class there’s no problem at all. This is my Loader
package com.m31.android.urlload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ClassLoader;
import java.net.URL;
import dalvik.system.PathClassLoader;
import dalvik.system.DexClassLoader;
public class Loader extends ClassLoader {
public Loader() throws IOException {
super(Loader.class.getClassLoader());
}
public Class loadClass(String className) throws ClassNotFoundException {
return findClass(className);
}
private String fetch_package(String url) throws IOException {
BufferedInputStream in = new BufferedInputStream(new URL(url).openStream());
FileOutputStream fos = new FileOutputStream("/mnt/sdcard/_plugins/plugin1.jar");
BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte data[] = new byte[1024];
int count;
while((count = in.read(data,0,1024)) > 0) {
bout.write(data,0,count);
}
bout.close();
in.close();
return "/mnt/sdcard/_plugins/plugin1.jar";
}
public Class findMyClass(String className, String url) throws IOException, ClassNotFoundException {
String path = fetch_package(url);
DexClassLoader pcl = new DexClassLoader(path, "/mnt/sdcard/_dex/", null, this);
return pcl.loadClass(className);
}
}
The problem is that the code I want to execute looks very like an application, which should have a “simple” view and some interaction on it.
I’m not able to invoke the “onCreate” method of my downloaded class.
I guess I’ve three streets:
- I look for a method which silently install the application and then runs it (is it possible?);
- With your help, I understand how to initialize a second “application” inside my own one (with its own R and all stuff);
- I write my master program to fetch data from the web and construct pages dynamically.
So, I definitely need your help!
I’m pretty sure you can’t silently install a new app. Also, I’ve been unable to find any way to register a new Activity into an existing application at runtime. I suppose you could write a WrapperActivity that passes all calls to another that you load dynamically, but that would still leave unsolved the problem of being able to load the resource data.
In the end, you will probably need to either write your code to completely avoid using the Android resource system (possible but difficult), or just go with a WebView and HTML with JavaScript (harder to cache and less native-looking, but much simpler to implement) for the dynamic bits.