Example usage for android.app DownloadManager EXTRA_DOWNLOAD_ID

List of usage examples for android.app DownloadManager EXTRA_DOWNLOAD_ID

Introduction

In this page you can find the example usage for android.app DownloadManager EXTRA_DOWNLOAD_ID.

Prototype

String EXTRA_DOWNLOAD_ID

To view the source code for android.app DownloadManager EXTRA_DOWNLOAD_ID.

Click Source Link

Document

Intent extra included with #ACTION_DOWNLOAD_COMPLETE intents, indicating the ID (as a long) of the download that just completed.

Usage

From source file:org.mozilla.focus.broadcastreceiver.DownloadBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    final long downloadReference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
    displaySnackbar(context, downloadReference, downloadManager);
}

From source file:com.renard.ocr.help.OCRLanguageInstallService.java

@Override
protected void onHandleIntent(Intent intent) {
    if (!intent.hasExtra(DownloadManager.EXTRA_DOWNLOAD_ID)) {
        return;/*from   w  w w .j a  v a2 s  . c om*/
    }
    final long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
    if (downloadId != 0) {

        DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        ParcelFileDescriptor file;
        try {
            file = dm.openDownloadedFile(downloadId);
            FileInputStream fin = new FileInputStream(file.getFileDescriptor());
            BufferedInputStream in = new BufferedInputStream(fin);
            FileOutputStream out = openFileOutput("tess-lang.tmp", Context.MODE_PRIVATE);
            GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
            final byte[] buffer = new byte[2048 * 2];
            int n = 0;
            while (-1 != (n = gzIn.read(buffer))) {
                out.write(buffer, 0, n);
            }
            out.close();
            gzIn.close();
            FileInputStream fileIn = openFileInput("tess-lang.tmp");
            TarArchiveInputStream tarIn = new TarArchiveInputStream(fileIn);

            TarArchiveEntry entry = tarIn.getNextTarEntry();

            while (entry != null && !(entry.getName().endsWith(".traineddata")
                    && !entry.getName().endsWith("_old.traineddata"))) {
                entry = tarIn.getNextTarEntry();
            }
            if (entry != null) {
                File tessDir = Util.getTrainingDataDir(this);
                final String langName = entry.getName().substring("tesseract-ocr/tessdata/".length());
                File trainedData = new File(tessDir, langName);
                FileOutputStream fout = new FileOutputStream(trainedData);
                int len;
                while ((len = tarIn.read(buffer)) != -1) {
                    fout.write(buffer, 0, len);
                }
                fout.close();
                String lang = langName.substring(0, langName.length() - ".traineddata".length());
                notifyReceivers(lang);
            }
            tarIn.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            String tessDir = Util.getTessDir(this);
            File targetFile = new File(tessDir, OCRLanguageActivity.DOWNLOADED_TRAINING_DATA);
            if (targetFile.exists()) {
                targetFile.delete();
            }
        }

    }
}

From source file:com.otaupdater.DownloadReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action == null)
        return;//from w ww.  j  a  v a2  s.  c o  m

    if (action.equals(DL_ROM_ACTION)) {
        RomInfo.FACTORY.clearUpdateNotif(context);
        RomInfo.FACTORY.fromIntent(intent).startDownload(context);
    } else if (action.equals(DL_KERNEL_ACTION)) {
        KernelInfo.FACTORY.clearUpdateNotif(context);
        KernelInfo.FACTORY.fromIntent(intent).startDownload(context);
    } else if (action.equals(CLEAR_DL_ACTION)) {
        if (intent.hasExtra(DownloadManager.EXTRA_DOWNLOAD_ID)) {
            DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            dm.remove(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1));
            DownloadBarFragment.notifyActiveFragment();
        }
    } else if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
        DownloadStatus status = DownloadStatus.forDownloadID(context,
                intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1));
        if (status == null)
            return;

        BaseInfo info = status.getInfo();
        if (info == null)
            return;

        int error = status.getStatus() == DownloadManager.STATUS_SUCCESSFUL ? info.checkDownloadedFile()
                : status.getReason();

        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (error == 0) {
            Intent mainIntent = new Intent(context, OTAUpdaterActivity.class);
            mainIntent.setAction(info.getNotifAction());
            mainIntent.putExtra(OTAUpdaterActivity.EXTRA_FLAG_DOWNLOAD_DIALOG, true);
            PendingIntent mainPIntent = PendingIntent.getActivity(context, 0, mainIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Intent flashIntent = new Intent(context, DownloadsActivity.class);
            flashIntent.setAction(info.getFlashAction());
            info.addToIntent(flashIntent);
            PendingIntent flashPIntent = PendingIntent.getActivity(context, 0, flashIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Notification notif = new NotificationCompat.Builder(context)
                    .setTicker(context.getString(info.getDownloadDoneTitle()))
                    .setContentTitle(context.getString(info.getDownloadDoneTitle()))
                    .setSmallIcon(R.drawable.ic_stat_av_download)
                    .setContentText(context.getString(R.string.notif_completed)).setContentIntent(mainPIntent)
                    .addAction(R.drawable.ic_action_system_update, context.getString(R.string.install),
                            flashPIntent)
                    .build();
            nm.notify(info.getFlashNotifID(), notif);
        } else {
            Intent mainIntent = new Intent(context, OTAUpdaterActivity.class);
            mainIntent.setAction(info.getNotifAction());
            info.addToIntent(mainIntent);
            PendingIntent mainPIntent = PendingIntent.getActivity(context, 0, mainIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Intent dlIntent = new Intent(context, DownloadReceiver.class);
            dlIntent.setAction(info.getDownloadAction());
            info.addToIntent(dlIntent);
            PendingIntent dlPIntent = PendingIntent.getBroadcast(context, 1, dlIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Intent clearIntent = new Intent(context, DownloadReceiver.class);
            clearIntent.setAction(CLEAR_DL_ACTION);
            clearIntent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, status.getId());
            PendingIntent clearPIntent = PendingIntent.getBroadcast(context, 2, clearIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Notification notif = new NotificationCompat.Builder(context)
                    .setTicker(context.getString(info.getDownloadFailedTitle()))
                    .setContentTitle(context.getString(info.getDownloadFailedTitle()))
                    .setContentText(status.getErrorString(context)).setSmallIcon(R.drawable.ic_stat_warning)
                    .setContentIntent(mainPIntent).setDeleteIntent(clearPIntent)
                    .addAction(R.drawable.ic_action_refresh, context.getString(R.string.retry), dlPIntent)
                    .build();
            nm.notify(info.getFailedNotifID(), notif);
        }
    } else if (action.equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
        long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
        if (ids.length == 0)
            return;

        DownloadStatus status = DownloadStatus.forDownloadID(context, ids[0]);
        if (status == null)
            return;

        BaseInfo info = status.getInfo();
        if (info == null)
            return;

        Intent i = new Intent(context, OTAUpdaterActivity.class);
        i.setAction(info.getNotifAction());
        i.putExtra(OTAUpdaterActivity.EXTRA_FLAG_DOWNLOAD_DIALOG, true);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

From source file:com.nks.nksmod.otaupdater.DownloadReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action == null)
        return;/*w w w  . j  a v a 2  s  .  co  m*/

    if (action.equals(DL_ROM_ACTION)) {
        RomInfo.FACTORY.clearUpdateNotif(context);
        RomInfo.FACTORY.fromIntent(intent).startDownload(context);
        /* } else if (action.equals(DL_KERNEL_ACTION)) {
            KernelInfo.FACTORY.clearUpdateNotif(context);
            KernelInfo.FACTORY.fromIntent(intent).startDownload(context); */
    } else if (action.equals(CLEAR_DL_ACTION)) {
        if (intent.hasExtra(DownloadManager.EXTRA_DOWNLOAD_ID)) {
            DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
            dm.remove(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1));
            DownloadBarFragment.notifyActiveFragment();
        }
    } else if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
        DownloadStatus status = DownloadStatus.forDownloadID(context,
                intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1));
        if (status == null)
            return;

        BaseInfo info = status.getInfo();
        if (info == null)
            return;

        int error = status.getStatus() == DownloadManager.STATUS_SUCCESSFUL ? info.checkDownloadedFile()
                : status.getReason();

        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (error == 0) {
            Intent mainIntent = new Intent(context, OTAUpdaterActivity.class);
            mainIntent.setAction(info.getNotifAction());
            mainIntent.putExtra(OTAUpdaterActivity.EXTRA_FLAG_DOWNLOAD_DIALOG, true);
            PendingIntent mainPIntent = PendingIntent.getActivity(context, 0, mainIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Intent flashIntent = new Intent(context, DownloadsActivity.class);
            flashIntent.setAction(info.getFlashAction());
            info.addToIntent(flashIntent);
            PendingIntent flashPIntent = PendingIntent.getActivity(context, 0, flashIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Notification notif = new NotificationCompat.Builder(context)
                    .setTicker(context.getString(info.getDownloadDoneTitle()))
                    .setContentTitle(context.getString(info.getDownloadDoneTitle()))
                    .setSmallIcon(R.drawable.ic_stat_av_download)
                    .setContentText(context.getString(R.string.notif_completed)).setContentIntent(mainPIntent)
                    .addAction(R.drawable.ic_action_system_update, context.getString(R.string.install),
                            flashPIntent)
                    .build();
            nm.notify(info.getFlashNotifID(), notif);
        } else {
            Intent mainIntent = new Intent(context, OTAUpdaterActivity.class);
            mainIntent.setAction(info.getNotifAction());
            info.addToIntent(mainIntent);
            PendingIntent mainPIntent = PendingIntent.getActivity(context, 0, mainIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Intent dlIntent = new Intent(context, DownloadReceiver.class);
            dlIntent.setAction(info.getDownloadAction());
            info.addToIntent(dlIntent);
            PendingIntent dlPIntent = PendingIntent.getBroadcast(context, 1, dlIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Intent clearIntent = new Intent(context, DownloadReceiver.class);
            clearIntent.setAction(CLEAR_DL_ACTION);
            clearIntent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, status.getId());
            PendingIntent clearPIntent = PendingIntent.getBroadcast(context, 2, clearIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            Notification notif = new NotificationCompat.Builder(context)
                    .setTicker(context.getString(info.getDownloadFailedTitle()))
                    .setContentTitle(context.getString(info.getDownloadFailedTitle()))
                    .setContentText(status.getErrorString(context)).setSmallIcon(R.drawable.ic_stat_warning)
                    .setContentIntent(mainPIntent).setDeleteIntent(clearPIntent)
                    .addAction(R.drawable.ic_action_refresh, context.getString(R.string.retry), dlPIntent)
                    .build();
            nm.notify(info.getFailedNotifID(), notif);
        }
    } else if (action.equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
        long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
        if (ids.length == 0)
            return;

        DownloadStatus status = DownloadStatus.forDownloadID(context, ids[0]);
        if (status == null)
            return;

        BaseInfo info = status.getInfo();
        if (info == null)
            return;

        Intent i = new Intent(context, OTAUpdaterActivity.class);
        i.setAction(info.getNotifAction());
        i.putExtra(OTAUpdaterActivity.EXTRA_FLAG_DOWNLOAD_DIALOG, true);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

From source file:com.ota.updates.receivers.AppReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Bundle extras = intent.getExtras();/*w w w .j  a v a2  s  . c  o  m*/
    long mRomDownloadID = Preferences.getDownloadID(context);

    if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
        long id = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
        boolean isAddonDownload = false;
        int keyForAddonDownload = 0;

        Set<Integer> set = OtaUpdates.getAddonDownloadKeySet();
        Iterator<Integer> iterator = set.iterator();

        while (iterator.hasNext() && isAddonDownload != true) {
            int nextValue = iterator.next();
            if (id == OtaUpdates.getAddonDownload(nextValue)) {
                isAddonDownload = true;
                keyForAddonDownload = nextValue;
                if (DEBUGGING) {
                    Log.d(TAG, "Checking ID " + nextValue);
                }
            }
        }

        if (isAddonDownload) {
            DownloadManager downloadManager = (DownloadManager) context
                    .getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(id);
            Cursor cursor = downloadManager.query(query);

            // it shouldn't be empty, but just in case
            if (!cursor.moveToFirst()) {
                if (DEBUGGING)
                    Log.e(TAG, "Addon Download Empty row");
                return;
            }

            int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
            if (DownloadManager.STATUS_SUCCESSFUL != cursor.getInt(statusIndex)) {
                if (DEBUGGING)
                    Log.w(TAG, "Download Failed");
                Log.d(TAG, "Removing Addon download with id " + keyForAddonDownload);
                OtaUpdates.removeAddonDownload(keyForAddonDownload);
                AddonActivity.AddonsArrayAdapter.updateProgress(keyForAddonDownload, 0, true);
                AddonActivity.AddonsArrayAdapter.updateButtons(keyForAddonDownload, false);
                return;
            } else {
                if (DEBUGGING)
                    Log.v(TAG, "Download Succeeded");
                Log.d(TAG, "Removing Addon download with id " + keyForAddonDownload);
                OtaUpdates.removeAddonDownload(keyForAddonDownload);
                AddonActivity.AddonsArrayAdapter.updateButtons(keyForAddonDownload, true);
                return;
            }
        } else {
            if (DEBUGGING)
                Log.v(TAG, "Receiving " + mRomDownloadID);

            if (id != mRomDownloadID) {
                if (DEBUGGING)
                    Log.v(TAG, "Ignoring unrelated non-ROM download " + id);
                return;
            }

            DownloadManager downloadManager = (DownloadManager) context
                    .getSystemService(Context.DOWNLOAD_SERVICE);
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(id);
            Cursor cursor = downloadManager.query(query);

            // it shouldn't be empty, but just in case
            if (!cursor.moveToFirst()) {
                if (DEBUGGING)
                    Log.e(TAG, "Rom download Empty row");
                return;
            }

            int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
            if (DownloadManager.STATUS_SUCCESSFUL != cursor.getInt(statusIndex)) {
                if (DEBUGGING)
                    Log.w(TAG, "Download Failed");
                Preferences.setDownloadFinished(context, false);
                if (Utils.isLollipop()) {
                    AvailableActivity.setupMenuToolbar(context); // Reset options menu
                } else {
                    AvailableActivity.invalidateMenu();
                }
                return;
            } else {
                if (DEBUGGING)
                    Log.v(TAG, "Download Succeeded");
                Preferences.setDownloadFinished(context, true);
                AvailableActivity.setupProgress(context);
                if (Utils.isLollipop()) {
                    AvailableActivity.setupMenuToolbar(context); // Reset options menu
                } else {
                    AvailableActivity.invalidateMenu();
                }
                return;
            }
        }
    }

    if (action.equals(DownloadManager.ACTION_NOTIFICATION_CLICKED)) {

        long[] ids = extras.getLongArray(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);

        for (long id : ids) {
            if (id != mRomDownloadID) {
                if (DEBUGGING)
                    Log.v(TAG, "mDownloadID is " + mRomDownloadID + " and ID is " + id);
                return;
            } else {
                Intent i = new Intent(context, AvailableActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }
        }
    }

    if (action.equals(MANIFEST_CHECK_BACKGROUND)) {
        if (DEBUGGING)
            Log.d(TAG, "Receiving background check confirmation");

        boolean updateAvailable = RomUpdate.getUpdateAvailability(context);
        String filename = RomUpdate.getFilename(context);

        if (updateAvailable) {
            Utils.setupNotification(context, filename);
            Utils.scheduleNotification(context, !Preferences.getBackgroundService(context));
        }
    }

    if (action.equals(START_UPDATE_CHECK)) {
        if (DEBUGGING)
            Log.d(TAG, "Update check started");
        new LoadUpdateManifest(context, false).execute();
    }

    if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
        if (DEBUGGING) {
            Log.d(TAG, "Boot received");
        }
        boolean backgroundCheck = Preferences.getBackgroundService(context);
        if (backgroundCheck) {
            if (DEBUGGING)
                Log.d(TAG, "Starting background check alarm");
            Utils.scheduleNotification(context, !Preferences.getBackgroundService(context));
        }
    }

    if (action.equals(IGNORE_RELEASE)) {
        if (DEBUGGING) {
            Log.d(TAG, "Ignore release");
        }
        Preferences.setIgnoredRelease(context, Integer.toString(RomUpdate.getVersionNumber(context)));
        final NotificationManager mNotifyManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setContentTitle(context.getString(R.string.main_release_ignored))
                .setSmallIcon(R.drawable.ic_notif)
                .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(), 0));
        mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());

        Handler h = new Handler();
        long delayInMilliseconds = 1500;
        h.postDelayed(new Runnable() {

            public void run() {
                mNotifyManager.cancel(NOTIFICATION_ID);
            }
        }, delayInMilliseconds);
    }
}

From source file:org.messic.android.download.DownloadManagerService.java

public void onCreate() {
    super.onCreate();
    dm = (DownloadManager) getSystemService(Activity.DOWNLOAD_SERVICE);

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override//w  w w  .  j  a  v  a2 s.co  m
        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);
                saveCover(pendingDownloads.get(downloadId));
                pendingDownloads.remove(downloadId);
                // Uri uri = dm.getUriForDownloadedFile( downloadId );
                // System.out.println( uri );
                // dm.remove( downloadId );
            }
        }
    };
    registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

From source file:ru.appsm.inapphelp.service.AttachmentDownloadReceiver.java

private void downloadCompleted(Context context, Intent intent) {
    StringBuilder text = new StringBuilder();
    //Files are  ready
    String filename = context.getString(R.string.iah_attachment);
    String filepath = null;//from   www.j a  v  a 2s .c  o m
    String mediaType = null;
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
    Query query = new Query();
    query.setFilterById(downloadId);
    Cursor c = dm.query(query);
    if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        filename = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
        filepath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
        mediaType = c.getString(c.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE));
        if (status == DownloadManager.STATUS_SUCCESSFUL) {
            text.append(context.getString(R.string.iah_download_complete));

        } else {
            text.append(context.getString(R.string.iah_error_during_download));
        }
    }

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(context);
    notificationbuilder.setAutoCancel(true);
    notificationbuilder.setContentText(text.toString());
    notificationbuilder.setContentTitle(filename);
    notificationbuilder.setSmallIcon(R.drawable.iah_notification_download_light_img);
    notificationbuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
    notificationbuilder.setContentIntent(getPendingIntent(context));

    notificationManager.notify(filename, NOTIFICATION_ID, notificationbuilder.build());
}

From source file:com.tenmiles.helpstack.service.AttachmentDownloadReceiver.java

private void downloadCompleted(Context context, Intent intent) {
    StringBuilder text = new StringBuilder();
    //Files are  ready
    String filename = context.getString(R.string.hs_attachment);
    String filepath = null;/*from  ww  w  .j  a  v  a 2s  . c  o  m*/
    String mediaType = null;
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
    Query query = new Query();
    query.setFilterById(downloadId);
    Cursor c = dm.query(query);
    if (c.moveToFirst()) {
        int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
        filename = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
        filepath = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
        mediaType = c.getString(c.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE));
        if (status == DownloadManager.STATUS_SUCCESSFUL) {
            text.append(context.getString(R.string.hs_download_complete));

        } else {
            text.append(context.getString(R.string.hs_error_during_download));
        }
    }

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(context);
    notificationbuilder.setAutoCancel(true);
    notificationbuilder.setContentText(text.toString());
    notificationbuilder.setContentTitle(filename);
    notificationbuilder.setSmallIcon(R.drawable.hs_notification_download_img);
    notificationbuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
    notificationbuilder.setContentIntent(getPendingIntent(context));

    notificationManager.notify(filename, NOTIFICATION_ID, notificationbuilder.build());
}

From source file:com.rdrive.updateapk.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn_update = (Button) findViewById(R.id.button_update);
    btn_update.setEnabled(false);/* ww  w .j a v a  2 s.  c o  m*/

    currentVersionValue = (TextView) findViewById(R.id.currentVersionValue);
    lastVersionValue = (TextView) findViewById(R.id.lastVersionValue);

    dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

    new CheckUpdate() {
        @Override
        protected void onPostExecute(Boolean aBoolean) {
            currentVersionValue.setText(this.getCurrentVersion().toString());
            lastVersionValue.setText(this.getLastVersion().toString());
            if (!aBoolean) {
                btn_update.setEnabled(true);
            }
        }
    }.execute(getContext());

    btn_update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT > 22) {
                if (ContextCompat.checkSelfPermission(getActivity(),
                        Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(getActivity(),
                            new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
                            ASK_WRITE_EXTERNAL_STORAGE_FOR_UPDATE);
                    return;
                }
            }
            downloadLastVersion(getContext(), dm);
        }
    });

    BroadcastReceiver attachmentDownloadCompleteReceive = 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);
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(downloadId);
                Cursor cursor = dm.query(query);
                if (cursor.moveToFirst()) {
                    int downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                    String downloadLocalUri = cursor
                            .getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                    if ((downloadStatus == DownloadManager.STATUS_SUCCESSFUL) && downloadLocalUri != null) {
                        openApk(getContext(), downloadLocalUri);
                    }
                }
                cursor.close();
            }
        }
    };

    registerReceiver(attachmentDownloadCompleteReceive,
            new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

From source file:org.amahi.anywhere.util.Downloader.java

private boolean isDownloadCurrent(Intent intent) {
    return downloadId == intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
}