Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Start Intent after download using DownloadManager
    primarykey
    data
    text
    <p>What I have is multiple buttons. Using if-else statements, I download a file to that corresponding button. Now, I also define what class to open via intent in the if-else statement. 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. So, you may be confused. So I'll explain through my code...</p> <p>So, here I'm setting it all up:</p> <pre><code> 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)); </code></pre> <p>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:</p> <pre><code>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); } </code></pre> <p>Ok. So all works good. Now, my showDownload:</p> <pre><code>public void showDownload(View view) { Intent i = new Intent(); i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS); startActivity(i); </code></pre> <p>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:</p> <pre><code> try { Intent openNewIntent = new Intent(Bmod.this, Class.forName(className) ); startActivity( openNewIntent ); } catch (ClassNotFoundException e) { e.printStackTrace(); } } </code></pre> <p>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!</p> <p>Edit - Here's an updated code:</p> <pre><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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload