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:net.momodalo.app.vimtouch.VimTouch.java

private void downloadFullRuntime() {

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override// w w w . j av a2s  .  c  om
        public void onReceive(Context context, Intent intent) {
            context.unregisterReceiver(this);
            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(mEnqueue);
                Cursor c = mDM.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));
                        Intent newintent = new Intent(getApplicationContext(), InstallProgress.class);
                        newintent.setData(Uri.parse(uriString));
                        startActivity(newintent);
                    }
                }
            }
        }
    };

    registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

    mDM = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    Request request = new Request(Uri.parse("https://github.com/downloads/momodalo/vimtouch/vim.vrz"));
    mEnqueue = mDM.enqueue(request);
}

From source file:com.ywesee.amiko.MainActivity.java

/**
 * Overrides onCreate method/*from  w  w w. j ava 2  s .c  om*/
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        AsyncLoadDBTask loadDBTask = new AsyncLoadDBTask(this);
        loadDBTask.execute();
    } catch (Exception e) {
        Log.e(TAG, "AsyncLoadDBTask exception caught!");
    }

    // Load CSS from asset folder
    if (Utilities.isTablet(this))
        mCSS_str = Utilities.loadFromAssetsFolder(this, "amiko_stylesheet.css", "UTF-8");
    else
        mCSS_str = Utilities.loadFromAssetsFolder(this, "amiko_stylesheet_phone.css", "UTF-8");

    // Flag for enabling the Action Bar on top
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    // Enable overlay mode for action bar (no good, search results disappear behind it...)
    // getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

    // Create action bar
    int mode = ActionBar.NAVIGATION_MODE_TABS;
    if (savedInstanceState != null) {
        mode = savedInstanceState.getInt("mode", ActionBar.NAVIGATION_MODE_TABS);
    }

    // Sets tab bar items
    addTabNavigation();

    // Reset action name
    Log.d(TAG, "OnCreate -> " + mActionName);
    mActionName = getString(R.string.tab_name_1);

    /*
    'getFilesDir' returns a java.io.File object representing the root directory
    of the INTERNAL storage four the application from the current context.
    */
    mFavoriteData = new DataStore(this.getFilesDir().toString());
    // Load hashset containing registration numbers from persistent data store
    mFavoriteMedsSet = new HashSet<String>();
    mFavoriteMedsSet = mFavoriteData.load();

    // Initialize preferences
    SharedPreferences settings = getSharedPreferences(AMIKO_PREFS_FILE, 0);

    long timeMillisSince1970 = 0;
    if (Constants.appLanguage().equals("de")) {
        timeMillisSince1970 = settings.getLong(PREF_DB_UPDATE_DATE_DE, 0);
        if (timeMillisSince1970 == 0) {
            SharedPreferences.Editor editor = settings.edit();
            editor.putLong(PREF_DB_UPDATE_DATE_DE, System.currentTimeMillis());
            // Commit the edits!
            editor.commit();
        }
    } else if (Constants.appLanguage().equals("fr")) {
        timeMillisSince1970 = settings.getLong(PREF_DB_UPDATE_DATE_FR, 0);
        if (timeMillisSince1970 == 0) {
            SharedPreferences.Editor editor = settings.edit();
            editor.putLong(PREF_DB_UPDATE_DATE_DE, System.currentTimeMillis());
            // Commit the edits!
            editor.commit();
        }
    }

    checkTimeSinceLastUpdate();

    // Init toast object
    mToastObject = new CustomToast(this);

    // Initialize download manager
    mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

    mBroadcastReceiver = 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);
                if (downloadId == mDatabaseId || downloadId == mReportId || downloadId == mInteractionsId)
                    mDownloadedFileCount++;
                // Before proceeding make sure all files have been downloaded before proceeding
                if (mDownloadedFileCount == 3) {
                    Query query = new Query();
                    query.setFilterById(downloadId);
                    Cursor c = mDownloadManager.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        // Check if download was successful
                        if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                            try {
                                // Update database
                                AsyncUpdateDBTask updateDBTask = new AsyncUpdateDBTask(MainActivity.this);
                                updateDBTask.execute();
                            } catch (Exception e) {
                                Log.e(TAG, "AsyncUpdateDBTask: exception caught!");
                            }
                            // Toast
                            mToastObject.show("Databases downloaded successfully. Installing...",
                                    Toast.LENGTH_SHORT);
                            if (mProgressBar.isShowing())
                                mProgressBar.dismiss();
                            mUpdateInProgress = false;
                            // Store time stamp
                            SharedPreferences settings = getSharedPreferences(AMIKO_PREFS_FILE, 0);
                            SharedPreferences.Editor editor = settings.edit();
                            editor.putLong(PREF_DB_UPDATE_DATE_DE, System.currentTimeMillis());
                            // Commit the edits!
                            editor.commit();
                        } else {
                            mToastObject.show("Error while downloading database...", Toast.LENGTH_SHORT);
                        }
                    }
                    c.close();
                }
            }
        }
    };
    registerReceiver(mBroadcastReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}