Пример #1
0
 private void updateLanguageListWithDownloadManagerStatus(OCRLanguageAdapter adapter) {
   if (adapter != null) {
     // find languages that are currently beeing downloaded
     Query query = new Query();
     query.setFilterByStatus(
         DownloadManager.STATUS_RUNNING
             | DownloadManager.STATUS_PENDING
             | DownloadManager.STATUS_PAUSED);
     final DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
     Cursor c = dm.query(query);
     int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_TITLE);
     while (c.moveToNext()) {
       final String title = c.getString(columnIndex);
       adapter.setDownloading(title, true);
     }
     adapter.notifyDataSetChanged();
     c.close();
   }
 }
  @Override
  public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
      Query query = new Query();
      query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
      Cursor c = dm.query(query);
      if (c.moveToFirst()) {
        int titleColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_TITLE);
        String title = context.getString(R.string.menu_view) + " " + c.getString(titleColumnIndex);
        int pdfPathColumnIndex = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
        String pdfPath = c.getString(pdfPathColumnIndex);
        Intent intentPdf = new Intent(Intent.ACTION_VIEW);
        intentPdf.setDataAndType(Uri.parse(pdfPath), "application/pdf");
        PendingIntent contentIntent =
            PendingIntent.getActivity(context, 0, intentPdf, PendingIntent.FLAG_CANCEL_CURRENT);

        SharedPreferences settings =
            context.getSharedPreferences("IssueDownloadNotification", Context.MODE_PRIVATE);
        if (settings.getBoolean(title, false)) return;
        settings.edit().putBoolean(title, true).commit();

        Notification noti =
            new NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setContentText(context.getString(R.string.download_completed_text))
                .setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.new_issue)
                .getNotification();

        NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        noti.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(ID_NOTIFICATION++, noti);
      }
      c.close();
    }
  }