代码中注册广播

此方法不用在manifests中注册广播

代码如下:

        broadcastManager = LocalBroadcastManager.getInstance(getActivity());
        IntentFilter intentFilterupDateCourse = new IntentFilter();
        intentFilterupDateCourse.addAction("android.intent.action.upDateCourseShow");
        upDateCourse = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
               showWeek = intent.getIntExtra("month", weekNumber);
                showCorse(showWeek);
            }
        };
        broadcastManager.registerReceiver(upDateCourse, intentFilterupDateCourse);

注册完成后切记要在onDestroy()方法中反注册广播

  broadcastManager.unregisterReceiver(upDateCourse);

你可能感兴趣的:(代码中注册广播)