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?