我正在尝试在 IntentService 的 onHandleIntent() 方法中注册 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/