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保存了标志的状态),但它不起作用。