Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么我的广播接收器持续接收相同的广播?_Android_Android Broadcast - Fatal编程技术网

Android 为什么我的广播接收器持续接收相同的广播?

Android 为什么我的广播接收器持续接收相同的广播?,android,android-broadcast,Android,Android Broadcast,我的应用程序类每天创建一个警报并接收一次系统广播。在onReceive()中,它发送一个由我的MainActivity类接收的应用程序广播 问题在于,每当发生方向更改时,MainActivity类中的onReceive()就会不断被调用。我理解为什么跨方向更改调用onResume(),但我不理解为什么也会调用onReceive() 我假设因为应用程序类只发送一次本地广播,所以我的 MainActivity将只接收一次广播 有人知道为什么在我的MainActivity类中不断调用onReceive

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

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

我假设因为
应用程序
类只发送一次本地广播,所以我的
MainActivity将只接收一次广播

有人知道为什么在我的MainActivity类中不断调用
onReceive()

下面是我的
应用程序
类中的
onCreate()

@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);    
}
以下是main活动中的
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); 
}

因为您需要在onCreate()中创建接收器。否则它将被一次又一次地创建


注册很好,与注销一样。

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

在扩展
应用程序的类中,我发送一个本地广播,如下所示:

Intent i = new Intent(TEST_ACTION);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
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类中,我定义了一个
BroadcastReceiver
,如下所示:

Intent i = new Intent(TEST_ACTION);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
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()
中注册接收器:

然后,我在MainActivity的
onDestroy()
中注销接收器:


它现在运行良好,我只收到一次广播。

我试过了,但每次方向改变,它仍然会收到广播。我甚至设置了一个标志来检查它是否已经创建(并使用onSaveInstanceState保存了标志的状态),但它不起作用。