views:

76

answers:

2

Hi all, guys.

This is my target:

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:

  1. I look for a method which silently install the application and then runs it (is it possible?);
  2. With your help, I understand how to initialize a second "application" inside my own one (with its own R and all stuff);
  3. I write my master program to fetch data from the web and construct pages dinamically.

So, I definitely need your help! Thanks in advice!

+1  A: 

I look for a method which silently install the application and then runs it (is it possible?);

No, it is not possible. That would be a security issue.

With your help, I understand how to initialize a second "application" inside my own one (with its own R and all stuff);

I suspect that will be in the difficult-to-impossible range.

I write my master program to fetch data from the web and construct pages dinamically.

There are certainly things that fit this description that should work. Personally, I'd recommend you just use HTML5, maybe hosted by a WebView, since HTML is a tried-and-true solution for dynamically generating UIs (as this very Web page illustrates).

CommonsWare
Like I was suspecting... I'll let you know!
Enrico Carlesso
A: 

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.

Walter Mundt