Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 2.59 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android: Error installing an apk from Downloads folder
  2. import android.app.Activity;
  3. import android.app.DownloadManager;
  4. import android.app.DownloadManager.Query;
  5. import android.app.DownloadManager.Request;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.database.Cursor;
  11. import android.net.Uri;
  12. import android.os.Bundle;
  13. import android.view.View;
  14. import android.widget.ImageView;
  15.  
  16. public class DownloadManagerActivity extends Activity {
  17.     private long enqueue;
  18.     private DownloadManager dm;
  19.  
  20.     /** Called when the activity is first created. */
  21.     @Override
  22.     public void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.main);
  25.  
  26.         BroadcastReceiver receiver = new BroadcastReceiver() {
  27.             @Override
  28.             public void onReceive(Context context, Intent intent) {
  29.                 String action = intent.getAction();
  30.                 if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
  31.                    /* long downloadId = intent.getLongExtra(
  32.                             DownloadManager.EXTRA_DOWNLOAD_ID, 0);*/
  33.                     Query query = new Query();
  34.                     query.setFilterById(enqueue);
  35.                     Cursor c = dm.query(query);
  36.                     if (c.moveToFirst()) {
  37.                         int columnIndex = c
  38.                                 .getColumnIndex(DownloadManager.COLUMN_STATUS);
  39.                         if (DownloadManager.STATUS_SUCCESSFUL == c
  40.                                 .getInt(columnIndex)) {
  41.  
  42.                           /*  ImageView view = (ImageView) findViewById(R.id.imageView1);
  43.                             String uriString = c
  44.                                     .getString(c
  45.                                             .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
  46.                             view.setImageURI(Uri.parse(uriString));*/
  47.                         }
  48.                     }
  49.                 }
  50.             }
  51.         };
  52.  
  53.         registerReceiver(receiver, new IntentFilter(
  54.                 DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  55.     }
  56.  
  57.     public void onClick(View view) {
  58.         dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
  59.         Request request = new Request(
  60.                 Uri.parse("http://xxx.xxx.x.xxx/MyApp.apk"));
  61.  
  62.         enqueue = dm.enqueue(request);
  63.  
  64.     }
  65.  
  66.     public void showDownload(View view) {
  67.         Intent i = new Intent();
  68.         i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
  69.         startActivity(i);
  70.     }
  71. }