1

コードの一部を実行しようとしていたところ、Sdcardの外部APKからDexClassLoaderを介してメソッドstartDownloadを取得しました。アクティビティのonCreateメソッドで、このアクティビティを呼び出そうとしています。つまり、ダウンロードを呼び出したいということです。メインのソースコード(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 [])null);でエラーjava.lang.reflect.InvocationTargetExceptionが発生します。私のメインで。私は本当に知りません、どんな間違いがそこにある可能性があります。簡単な方法で試してみましたが、「外部」の方法との接続はOKです。しかし、ダウンロードを開始できません。これはトリッキーかもしれませんが、実際に私は今それで立ち往生しています。誰かアイデアはありますか?

4

0 に答える 0