下载管理器广播接收器不工作

时间:2017-11-14 10:46:04

标签: android broadcastreceiver android-download-manager downloading

我一直关注这个question但是没有成功,基本上,我想在下载某些内容时收到通知,或者从任何来源放入“下载”文件夹,例如下载,用电脑把它放在那里。问题是,我的接收器没有收到任何回复。这是我的代码:

public class DownloadReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("TAG", "SOMETHING DOWNLOADED");
    Toast.makeText(context, "SOMETHING DOWNLOADED", Toast.LENGTH_SHORT).show();
    long receivedID = intent.getLongExtra(
            DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
    DownloadManager mgr = (DownloadManager)
            context.getSystemService(Context.DOWNLOAD_SERVICE);

    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(receivedID);
    Cursor cur = mgr.query(query);
    int index = cur.getColumnIndex(DownloadManager.COLUMN_STATUS);
    if(cur.moveToFirst()) {
        if(cur.getInt(index) == DownloadManager.STATUS_SUCCESSFUL){
            // do something
            Log.d("TAG", "SOMETHING DOWNLOADED");
            Toast.makeText(context, "SOMETHING DOWNLOADED", Toast.LENGTH_SHORT).show();
        }
    }
    cur.close();
}

这是我在我的应用程序文件中注册的方式:

final IntentFilter downloadFilter = new IntentFilter();
downloadFilter.addAction("android.intent.action.DOWNLOAD_COMPLETE");
registerReceiver(new DownloadReceiver(),downloadFilter);

这是我的Android清单:

<receiver android:name="com.tproductions.Openit.provider.DownloadReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" android:enabled="true" />
        </intent-filter>
    </receiver>

0 个答案:

没有答案