DexClassLoader和DownloadManager API

时间:2011-09-12 17:41:20

标签: android reflection download-manager invocationtargetexception

我试图运行我的代码部分,我通过DexClassLoader在我的SD卡上从外部APK获取方法startDownload。在我的活动的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);


    }


};

}

我在main中遇到错误 java.lang.reflect.InvocationTargetException m.invoke(this,(Object [])null); 。我真的不知道,那里会有什么错误。我用简单的方法尝试过它,并且连接到我的“外部”方法就可以了。但我无法启动下载。这可能是一个棘手的问题,但我现在真的很困惑。有没有人有任何想法?

0 个答案:

没有答案