DexClassLoader 和 DownloadManager API
我试图运行我的代码部分,其中我通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论