DexClassLoader 和 DownloadManager API

发布于 2024-12-04 08:05:19 字数 2442 浏览 0 评论 0原文

我试图运行我的代码部分,其中我通过 DexClassLoader 从我的 sdcard 上的外部 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);


    }


};

}

我在 m.invoke(this,(Object[]) 行中收到错误 java.lang.reflect.InvocatTargetException null ); 在我的 main.我真的不知道,这会出现什么错误。我已经用简单的方法尝试过,并且连接到我的“外部”方法是可以的。但我无法启动下载。这可能很棘手,但我现在确实被困住了。有人有什么想法吗?

I was trying to run my part of a code, where I get method startDownload from external APK on my sdcard via DexClassLoader. In onCreate method of my activity I am trying to invoke this activity, that means I want to invoke download. Source code of 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();
        }

Source code of 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);


    }


};

}

I am getting error java.lang.reflect.InvocationTargetException in line m.invoke(this,(Object[]) null ); in my main. I really don't know, what mistake can be there. I have tried it with simple method, and connection to my "external" method is OK. But I am not able to launch download. This could be a tricky, but really I am stuck with it right now. Does anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文