CLICK HERE TO EARN MONEY FREE

Sunday, September 30, 2012

Download Manager in Android programming

Download Manager in Android programming


This example shows how you can create download manager activity.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it DownloadManager.
2.) Add following permission into your manifest file:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
3.) Write following into main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button android:text="Start Download" android:id="@+id/button1"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:onClick="onClick"></Button>
        <Button android:text="View Downloads" android:id="@+id/button2"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:onClick="showDownload"></Button>
        <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"
                android:src="@drawable/ic_launcher" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
4.) Run for output.
Steps:
1.) Create a project named DownloadManager and set the information as stated in the image.
Build Target: Android 4.0
Application Name: DownloadManager
Package Name: com. example. DownloadManager
Activity Name: DownloadManagerActivity
Min SDK Version: 9

2.) Open DownloadManagerActivity.java file and write following code there:
package com.example.downloadmanager; import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class DownloadManagerActivity extends Activity {
        private long enqueue;
        private DownloadManager dm;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                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)) {
                                                        ImageView view = (ImageView) findViewById(R.id.imageView1);
                                                        String uriString = c
                                                                        .getString(c
                                                                                        .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                                                        view.setImageURI(Uri.parse(uriString));
                                                }
                                        }
                                }
                        }
                };
                registerReceiver(receiver, new IntentFilter(
                                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        }
        public void onClick(View view) {
                dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                Request request = new Request(
                                Uri.parse("https://encrypted-tbn3.google.com/images?q=tbn:ANd9GcTIQQgqjfdNDRYQ3btnNJwRB07htjMKgeW2FyZrDCkxM7lfikmtHg"));
                enqueue = dm.enqueue(request);
        }
        public void showDownload(View view) {
                Intent i = new Intent();
                i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
                startActivity(i);
        }
}
3.) Compile and build the project.
Output



0 comments:

Post a Comment

 

TEKONOLOGY Copyright © 2011 -- Template created by TEKONOLOGY -- Powered by Blogger