一个使用DownloadManager下载文件的小例子

public class ApkClientActivity extends Activity {  
    static final String TAG = "ApkClientActivity";  
    Context mContext;  
    DownloadManager manager ;  
    DownloadCompleteReceiver receiver;  
    Button downBtn ;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        mContext = this;  
        //获取下载服务  
        manager =(DownloadManager)getSystemService(DOWNLOAD_SERVICE);  
        receiver = new DownloadCompleteReceiver();  
        downBtn = (Button)findViewById(R.id.downBtn);  
        downBtn.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                //创建下载请求  
                DownloadManager.Request down=new DownloadManager.Request (Uri.parse("http://192.168.0.66:8080/qqinput.apk"));  
                //设置允许使用的网络类型,这里是移动网络和wifi都可以  
                down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI);  
                //禁止发出通知,既后台下载  
                down.setShowRunningNotification(false);  
                //不显示下载界面  
                down.setVisibleInDownloadsUi(false);  
                //设置下载后文件存放的位置  
                down.setDestinationInExternalFilesDir(mContext, null, "qqinput.apk");  
                //将下载请求放入队列  
                manager.enqueue(down);  
            }  
        });  
    }  
    //接受下载完成后的intent  
    class DownloadCompleteReceiver extends BroadcastReceiver {  
        @Override  
        public void onReceive(Context context, Intent intent) {  
            if(intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){  
                long downId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);  
                Log.v(TAG," download complete! id : "+downId);  
                Toast.makeText(context, intent.getAction()+"id : "+downId, Toast.LENGTH_SHORT).show();  
            }  
        }  
    }  
      
    @Override  
    protected void onResume() {  
        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));  
        super.onResume();  
    }  
      
    @Override  
    protected void onDestroy() {  
        if(receiver != null)unregisterReceiver(receiver);  
        super.onDestroy();  
    }  
}

 

其中在设置 down.setShowRunningNotification(false);时,需要添加相应的权限:

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

否则,会报错!

 

AndroidManifest.xml文件内容如下:

 

 

 

<strong><uses-sdk android:minSdkVersion="9" /></strong>  
  
    <application  
        android:icon="@drawable/ic_launcher"  
        android:label="@string/app_name" >  
        <activity  
            android:label="@string/app_name"  
            android:name=".ApkClientActivity" >  
            <intent-filter >  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  
    <uses-permission android:name="android.permission.INTERNET" />  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

转载于:https://my.oschina.net/u/861587/blog/147943

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值