Permissions Required :
MainActivity.class :
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>activity_main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Download" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="show Download" /> </LinearLayout>
MainActivity.class :
package com.example.samppledefaultdownloader; 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.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private long enqueue; private DownloadManager dm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button b = (Button) findViewById(R.id.button1); Button b1 = (Button) findViewById(R.id.button2); b.setOnClickListener(this); b1.setOnClickListener(this); dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 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)) { String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); Log.i("123456", "Download successfull +" + Uri.parse(uriString).getPath()); } } } } }; registerReceiver(receiver, new IntentFilter( DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } @Override public void onClick(View v) { if (v.getId() == R.id.button1) { Request request = new Request(Uri.parse("http://upload.wikimedia.org/wikipedia/commons/b/b8/Charminar_sumeet_photography_3.JPG")); request.setDestinationInExternalPublicDir("/meher-samples/Download-example/", "charminar.png"); // enable this line to download in custom path. request.setTitle("charminar"); enqueue = dm.enqueue(request); } else if (v.getId() == R.id.button2) { Intent i = new Intent(); i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS); startActivity(i); } } }
No comments:
Post a Comment