我所拥有的是多个按钮。使用if-else语句,我将文件下载到相应的按钮。现在,我还定义了在if-else语句中通过意图打开哪个类。我需要拥有它才能开始下载文件,然后开始新的活动。我曾经用AsyncTask做这件事,然后在onPostExecute中启动新的意图,但我决定最好使用DownloadManager。所以,你可能会感到困惑。所以,我会通过我的代码,以便解释...Android - 使用DownloadManager下载后的开始意向
,在这里我设置了这一切:
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
确定。现在,在我的if-else我声明等于输出文件的URL下载,以及设置字符串等于一个类,另一个字符串:
if (andy != null){
className = "com.cydeon.plasmamodz.Softkeys";
fileName = "batterymod.zip";
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request req = new Request(
Uri.parse("https://dl.dropbox.com/s/gfukrwqy4xqrnj9/Android.zip"));
req.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
fileName);
enqueue = dm.enqueue(req);
}
确定。所以一切正常。现在,我的秀下载:
public void showDownload(View view) {
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
好。现在,它下载。所以,现在这是下载,我需要开始一个新的活动。而且,我研究并尝试了一些东西,但没有任何工作。如你所见,我已经在字符串中设置了一个类。我有这样的代码,我在onPostExecute使用,所以我知道它工作正常:
try {
Intent openNewIntent = new Intent(Bmod.this, Class.forName(className));
startActivity(openNewIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
所以,我会重复我想要什么。我想下载一个文件,然后在执行下载之后,开始一个新的活动。任何帮助是极大的赞赏。谢谢!
编辑 - 下面是一个更新的代码:
public void showDownload(View view) {
Context context = getApplicationContext();
CharSequence text = "Download complete";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
try {
Intent openNewIntent = new Intent(Bmod.this, Class.forName(className));
startActivity(openNewIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
好的。我不希望它在开始课程之前等待下载完成。我希望它下载,而其他应用程序正在运行。 – user2182912 2013-03-28 02:29:26
好了,那么不要做showDownload()就开始你的活动。下载完成后,DownloadManager将在后台运行,并且您的接收器将被调用。您不必显示下载以供下载。如果您启动了DownloadManager.ACTION_VIEW_DOWNLOADS活动,那么该活动将控制。 – 2013-03-28 02:32:45
好的。我会尝试的。如果有效,我会接受你的答案。 :) 谢谢! – user2182912 2013-03-28 02:34:25