为什么我的BroadcastReceiver会不断收到相同的广播?

时间:2014-06-10 00:20:42

标签: android android-broadcast

我的Application班级每天都会创建一个警报并接收一次系统广播。在onReceive()中,它发送由我的MainActivity类接收的应用程序广播。

问题是每当发生方向更改时,都会不断调用MainActivity类中的onReceive()。我理解为什么在方向更改时调用onResume(),但我不明白为什么onReceive()也会被调用。

我认为因为Application班只发出一次本地广播,我的 MainActivity只接收一次广播。

有人知道为什么我的MainActivity类中的onReceive()会被不断调用吗?

以下是onCreate()课程中的Application

@Override
public void onCreate()
{
    super.onCreate();

    // register a receiver in the Application class to receive a broadcast
    // at the start of each day
    IntentFilter intentFilter = new IntentFilter(START_OF_DAY_ACTION);
    startOfDayReceiver = new BroadcastReceiver() {

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

            Toast.makeText(TaskReminderApp.this, 
                           "Application: startofday broadcast received", 
                           Toast.LENGTH_LONG).show();        

            // send a broadcast to MainActivity
            Intent i = new Intent();
            i.setAction(TEST_ACTION); 

            context.sendBroadcast(i);     
        }
    };

    this.registerReceiver(startOfDayReceiver, intentFilter);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 19);  // for testing purposes
    calendar.set(Calendar.MINUTE, 51);       // for testing purposes
    calendar.set(Calendar.SECOND, 0);        
    calendar.set(Calendar.MILLISECOND, 0);

    Intent intent = new Intent(START_OF_DAY_ACTION);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);    
}

以下是MainActivity中的onResume()onPause()

@Override
protected void onResume()
{
    super.onResume();

    IntentFilter intentFilter = new IntentFilter(TEST_ACTION);
    receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) 
        {
            // This is getting called on every orientation change
            // and every time the activity resumes.
            Toast.makeText(MainActivity.this, 
                          "MainActivity: broadcast received", 
                          Toast.LENGTH_LONG).show();
        }
    };

    this.registerReceiver(receiver, intentFilter);    
} 

@Override
protected void onPause() 
{
    super.onPause();

    // I thought this might be the problem, but it makes no
    // difference if I comment it out.
    this.unregisterReceiver(receiver); 
}

2 个答案:

答案 0 :(得分:0)

因为您需要在onCreate()中创建Receiver。否则它将一次又一次地创建..

注册很好,与取消注册相同。

答案 1 :(得分:0)

我通过发送本地应用程序广播而不是系统广播解决了这个问题。

在我的课程Application中,我发送了这样的本地广播:

Intent i = new Intent(TEST_ACTION);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);

然后在我的MainActivity类中,我定义了BroadcastReceiver,如下所示:

private BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Toast.makeText(MainActivity.this, 
                       "MainActivity: broadcast received", 
                        Toast.LENGTH_LONG).show();
    }
};

我在MainActivity onCreate()

中注册了接收器
LocalBroadcastManager.getInstance(this).registerReceiver(receiver,
        new IntentFilter(TEST_ACTION));

然后我在MainActivity onDestroy()中取消注册接收器:

LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);

现在效果很好,我只收到一个广播。