java - IntentService 中的广播接收器

标签 java android android-broadcastreceiver android-intentservice

我正在尝试在 IntentServiceonHandleIntent() 方法中注册 DownloadManager.ACTION_DOWNLOAD_COMPLETE 接收器,并在 onDestroy() 中注销接收器IntentService 的方法。但我认为它没有被注册,因为一旦下载完成,接收器的 onReceive() 方法就不会被触发。 谁能帮我解决这个问题?

最佳答案

创建服务类,

public class ConnectionBroadReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager cm = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        IConnectionCallback callback = (IConnectionCallback) context;
        callback.finishDownload();
}

在 Activity 中,

    ConnectionBroadReceiver broadReceiver = new ConnectionBroadReceiver();
    registerReceiver(broadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

创建接口(interface),并在 Activity 中实现并定义下载后要执行的功能

    public interface IConnectionCallback {
     void finishDownload();

  }

最后在 list 中注册服务,

    <receiver android:name=".ConnectionBroadReceiver">

关于java - IntentService 中的广播接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38246522/

相关文章:

java - 你能做一个可编辑的标签吗?

java - 使用数组时出现 NullPointerException

android - 如何停止服务本身?

android BroadcastReceiver 调用了两次

java - 无法从 NetWorkInfo Android 获取状态

android - 正确跟踪 Play 商店中的安装引荐

java - Spring boot + jpa,通过表单提交进行多对多关联

java - jvm如何运行程序

java - 在一个字段上使用最大条件连接表

android - Activity 无法解析或不是字段