[英]Android - Start Intent after download using DownloadManager
What I have is multiple buttons. 我有多个按钮。 Using if-else statements, I download a file to that corresponding button.
使用if-else语句,我将文件下载到相应的按钮。 Now, I also define what class to open via intent in the if-else statement.
现在,我还要在if-else语句中定义通过意图打开的类。 I need to have it so that it will begin downloading the file and then start a new activity.
我需要它,以便它将开始下载文件,然后开始新的活动。 I used to do this with an AsyncTask, and start the new intent in the onPostExecute, but I decided it's better to use DownloadManager.
我曾经使用AsyncTask来执行此操作,并在onPostExecute中启动新的意图,但我认为最好使用DownloadManager。 So, you may be confused.
因此,您可能会感到困惑。 So I'll explain through my code...
因此,我将通过我的代码进行解释...
So, here I'm setting it all up: 因此,在这里我将其全部设置:
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));
Ok. 好。 Now, in my if-else I declare the url to download, as well as setting a string equal to a class, and another string equal to the output file:
现在,在我的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);
}
Ok. 好。 So all works good.
所以一切正常。 Now, my showDownload:
现在,我的节目下载:
public void showDownload(View view) {
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
Good. 好。 Now, it downloads.
现在,它下载。 So, now that's it's downloading, I need to start a new activity.
所以,现在就是下载了,我需要开始一个新的活动。 And, I've researched and tried some stuff, but nothing works.
而且,我已经研究并尝试了一些方法,但是没有任何效果。 As you saw, I already set a class inside the string.
如您所见,我已经在字符串中设置了一个类。 I have this code which I used in the onPostExecute, so I know it works fine:
我有在onPostExecute中使用的这段代码,因此我知道它可以正常工作:
try {
Intent openNewIntent = new Intent(Bmod.this, Class.forName(className) );
startActivity( openNewIntent );
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
So, I'll repeat what I want. 所以,我会重复我想要的。 I want to download a file, and then, after executing the download, start a new activity.
我想下载一个文件,然后在执行下载后开始一个新的活动。 Any help is greatly appreciated.
任何帮助是极大的赞赏。 Thanks!
谢谢!
Edit - Here's an updated code: 编辑-这是更新的代码:
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();
}
}
将startActivity()调用放入广播接收器的onReceive()中,以便在通知接收器下载完成时开始活动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.