android 广播,manifest.xml注册,代码编写

本文介绍了一种在安卓应用中实现文件下载的方法,并通过发送广播通知接收器下载完成的状态。提供了两种实现方式:一是使用广播接收器监听下载完成事件;二是启动服务进行文件下载。文中还包含了实现这两种方案的具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.种

private void downloadBr(File file) {
  // 广播出去,由广播接收器来处理下载完成的文件
  Intent sendIntent = new Intent("com.test.downloadComplete");
  // 把下载好的文件的保存地址加进Intent
  sendIntent.putExtra("downloadFile", file.getPath());
  sendBroadcast(sendIntent);
 }

 

 

 

 <receiver android:name="com.yuxin.mhealth.ui.dbmanager.DownLoadBR" >
            <intent-filter>
                <action android:name="com.test.downloadComplete" >
                </action>
            </intent-filter>
        </receiver>

 

2种

public  static  void start(Context context,String fileUrl,String name){
  Intent downloadIntent = new Intent(context, DownloadFileService.class);
        Bundle bundle = new Bundle();
        bundle.putString("url", fileUrl);
        bundle.putString("fileName", name);
        downloadIntent.putExtras(bundle);
        context.startService(downloadIntent);
 }

    <service android:name="com.yuxin.mhealth.ui.dbmanager.DownloadFileService" >
        </service>

转载于:https://www.cnblogs.com/melons/p/5791945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值