DexClassLoader и DownloadManager API - PullRequest
       21

DexClassLoader и DownloadManager API

1 голос
/ 12 сентября 2011

Я пытался запустить свою часть кода, где я получаю метод startDownload от внешнего APK на моей SD-карте через DexClassLoader. В методе onCreate моей активности я пытаюсь вызвать это действие, то есть я хочу вызвать загрузку. Исходный код main (DownloadManagerActivity):

         try {

            DexClassLoader cl = new DexClassLoader("/sdcard/GingerbreadDownload.apk", 
                    getFilesDir().getAbsolutePath(),
                    null,  
                    DownloadManagerActivity.class.getClassLoader());


            Class<?> n = cl.loadClass("com.gingerbread.download.Download23");
            Method m=n.getDeclaredMethod("startDownload", null);
            m.invoke(this,(Object[]) null );   

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Исходный код Download23.java:

public class Download23 {


public static void startDownload() {
    DownloadManager mgr=null;
    long lastDownload;
    String downloadedAPKName="myApp";
    Uri uri=Uri.parse("http://someuri.com/test.apk");

    Environment
        .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
        .mkdirs();

    lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
                                .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                                                                                DownloadManager.Request.NETWORK_MOBILE)
                                .setAllowedOverRoaming(false)
                                .setTitle("Title")
                                .setDescription("Info")
                                .setShowRunningNotification(true)
                                .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, downloadedAPKName));
}


BroadcastReceiver onComplete=new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {    

        File file = new File(Environment.getExternalStorageDirectory()+"/Download/", "myApp");

        Intent intenta = new Intent(Intent.ACTION_VIEW);
        intenta.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        context.startActivity(intenta);


    }


};

}

Я получаю сообщение об ошибке java.lang.reflect.InvocationTargetException в строке m.invoke (this, (Object []) null); в моем main. Я действительно не знаю, какая ошибка может быть там. Я попробовал это с простым методом, и соединение с моим «внешним» методом в порядке. Но я не могу запустить загрузку. Это может быть сложно, но на самом деле я застрял с этим прямо сейчас. У кого-нибудь есть идеи?

...