Android广播接收器未呼叫

Android广播接收器未呼叫,android,Android,我使用android下载管理器和广播接收器开发了一个android应用程序,下载完成后,我将成功响应传递给BroadcastReceiver类,但没有调用。我做错了什么 public class MobuleDownloadManager { private boolean isReaname = false; private ProgressDialog mProgressDialog; private Activity activity; private Do

我使用android下载管理器和广播接收器开发了一个android应用程序,下载完成后,我将成功响应传递给BroadcastReceiver类,但没有调用。我做错了什么

public class MobuleDownloadManager {
    private boolean isReaname = false;
    private ProgressDialog mProgressDialog;
    private Activity activity;
    private DownloadFile mDownloadClas;
    private String TAG = "MobuleDownloadManager", URL, mFileName, mDisplayName_;
    private AlertDialog alert;

    private DownloadManager downloadManager;
    private long enqueue, id;
    SharedPreferences preferenceManager;
    final String strPref_Download_ID = "PREF_DOWNLOAD_ID";
    public static BroadcastReceiver downloadReceiver = null;
    final Timer myTimer = new Timer();
    Cursor cursorMain, cursorTimer;
    private SharedPreferences.Editor PrefEdit;
    int bytes_total;

    public MobuleDownloadManager(final Activity activity_, String mFilePath_,
                                 final String mFileName_, final String mDisplayName) {
        super();

        this.URL = mFilePath_;
          fileDownloadManager(URL);

        FileInputStream in = null;
        try {
            in = activity.openFileInput(mFileName);
            // file is alredy exist so no need too download again just open file
            myTimer.cancel();
            OnHttpResponseDownloadFile callBack = (OnHttpResponseDownloadFile) activity;
            callBack.onSuccessDownloadFile(mFileName, mDisplayName_);
            fileDownloadManager(URL);
        } catch (FileNotFoundException e1) {
            try {
                fileDownloadManager(URL);
        } 

    }
    private void fileDownloadManager(String url) {
        preferenceManager = PreferenceManager.getDefaultSharedPreferences(activity);

        downloadManager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadUri = Uri.parse(url);
        DownloadManager.Request request = new DownloadManager.Request(downloadUri);
        id = downloadManager.enqueue(request);

        mProgressDialog = new ProgressDialog(activity);
        mProgressDialog.setTitle("Downloading");
        mProgressDialog.setMessage("Downloading, Please Wait!");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
                "Cancel", new DialogInterface.OnClickListener() {
                    // Set a click listener for progress dialog cancel
                    // button
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            myTimer.cancel();
                            downloadManager.remove(id);
                            mProgressDialog.dismiss();
                        } catch (Exception e) {
                            Log.e(TAG, e.toString());
                        }

                    }
                });

        mProgressDialog.show();
        PrefEdit = preferenceManager.edit();
        PrefEdit.putLong(strPref_Download_ID, id);
        PrefEdit.commit();

        myTimer.schedule(new TimerTask() {

            public void run() {
                try {
                    DownloadManager.Query q;
                    q = new DownloadManager.Query();
                    q.setFilterById(preferenceManager.getLong(strPref_Download_ID, 0));
                    cursorTimer = downloadManager.query(q);

                    if (cursorTimer.moveToFirst() && cursorTimer != null) {
                        Log.e("bytes_total", "cursorTimer");
                        int bytes_downloaded = cursorTimer.getInt(cursorTimer.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                        bytes_total = cursorTimer.getInt(cursorTimer.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                        final int dl_progress = (int) ((double) bytes_downloaded / (double) bytes_total * 100f);

                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mProgressDialog.setProgress(dl_progress);
                            }
                        });

                        if (bytes_total == bytes_downloaded) {
                            Log.e(TAG, "run block 1");
                            myTimer.cancel();
                            mProgressDialog.dismiss();
                            IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
                            activity.registerReceiver(downloadReceiver, intentFilter);
                            Log.e(TAG, "run block 2");
                        }
                    } else {
                                           }

                } catch (Exception e) {
                    Log.e(TAG, "Exception:" + e.getMessage());
                } finally {
                    Log.e(TAG, "finally");
                    cursorTimer.close();
                }
            }

        }, 0, 1000);

        try {
            Log.e(TAG, "BroadcastReceiver ");
            downloadReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    Log.e(TAG, "BroadcastReceiver onReceive");
                    DownloadManager.Query query = new DownloadManager.Query();
                    query.setFilterById(preferenceManager.getLong(strPref_Download_ID, 0));
                    cursorMain = downloadManager.query(query);
                    Log.e(TAG, "BroadcastReceiver " + cursorMain.getColumnIndex(DownloadManager.COLUMN_STATUS));
                    if (cursorMain.moveToFirst()) {
                        int columnIndex = cursorMain.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        int status = cursorMain.getInt(columnIndex);

                        if (status == DownloadManager.STATUS_SUCCESSFUL) {
                            ParcelFileDescriptor file;
                            try {
                                file = downloadManager.openDownloadedFile(preferenceManager.getLong(strPref_Download_ID, 0));
                                InputStream fileStream = new FileInputStream(file.getFileDescriptor());
                                FileOutputStream newOS = null;

                                if (isReaname) {
                                    newOS = activity.openFileOutput("encrypted$" + mFileName, Context.MODE_PRIVATE);
                                } else {
                                    newOS = activity.openFileOutput(mFileName, Context.MODE_PRIVATE);
                                }
                                byte[] buffer = new byte[1024];
                                int length;
                                while ((length = fileStream.read(buffer)) > 0) {
                                    newOS.write(buffer, 0, length);
                                }

                                newOS.flush();
                                fileStream.close();
                                newOS.close();
                                downloadManager.remove(id);
                                activity.unregisterReceiver(downloadReceiver);
                                PrefEdit.clear();
                                OnHttpResponseDownloadFile callBack = (OnHttpResponseDownloadFile) activity;
                                callBack.onSuccessDownloadFile(mFileName, mDisplayName_);

                            } catch (Exception e) {

                            }
                        } else {
                            downloadManager.remove(id);
                            activity.unregisterReceiver(downloadReceiver);
                        }
                    }
                }
            };
        } catch (Exception e) {

        } finally {
        }
    }
}
声明: public static downloadReceiver downloadReceiver=新的downloadReceiver()

现在这是我的接受者课程

public static class DownloadReciver extends BroadcastReceiver {
        public DownloadReciver() {
            super();
        }
        @Override
        public void onReceive(Context context, Intent intent) {
        }
    }
我是这样打电话的:

if (bytes_total == bytes_downloaded) {
    IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    activity.registerReceiver(downloadReceiver, intentFilter);
    }
<receiver android:name="com.mobule.lms.connection.MobuleDownloadManager$DownloadReciver" >
            <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
        </receiver> 
这是我的清单:

if (bytes_total == bytes_downloaded) {
    IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    activity.registerReceiver(downloadReceiver, intentFilter);
    }
<receiver android:name="com.mobule.lms.connection.MobuleDownloadManager$DownloadReciver" >
            <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
        </intent-filter>
        </receiver> 

我遇到了异常:
java.lang.RuntimeException:无法启动receiver com.mobule.lms.connection.MobuleDownloadManager$DownloadReceiver:java.lang.NullPointerException

是否在清单中声明BroadcastReceiver?

在清单中声明receiver,检查是否授予Android 6.0棉花糖权限,以及是否提供使用权限。 对于内部类,我们也可以在清单中声明:
receiver android:name属性应该类似于.path.to.class.MyClass$MyInnerClass

您应该按照以下方式注册广播接收器

<receiver android:name=".path.to.your.downloadReciever" >
    <intent-filter>
       <action android:name="your filter" />
    </intent-filter>
</receiver>


在使用广播接收器之前,您必须先注册它,很抱歉,无法让您…?不。但我被声明在类中而不是活动中。那么我如何声明呢?在应用程序标记声明中。好吧,你动态地创建了广播接收器,现在我看到了。您的代码中有问题。您能建议如何更改吗@米洛瓦尼确实喜欢,但现在我无法启动receiver Exception is DownloadReceiver是MobuleDownloadManager的内部类吗?您应该将android:name的值更改为com.mobule.lms.connection.downloadReceiver,如果我这样做的话。但是我得到了java.lang.RuntimeException:接收广播意图时出错只需查看“原因”行上的logcat,并检查您的代码在哪里出错