18

我有一个广播接收器,它在注册时被触发(随后用 onPause / onResume 重新触发),这肯定是错误的行为吗?我在这里错过了什么吗?

class FooActivity extends Activity {    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        broadcastReceiver = new FooBroadcastReceiver();
        intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(connectivityReceiver, intentFilter);         
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(connectivityReceiver);
    }


class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {       
        if (connectivityAction(intent)) {
            Log.d("footag", "onReceive");
        }
    }

    private boolean connectivityAction(Intent intent) {
        return ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction());
    }

}
4

4 回答 4

17

广播似乎在CONNECTIVITY_ACTION某些设备上很粘(即使文档暗示它不是)。这意味着当您注册接收器时,它将立即onReceive()使用最近发送的广播进行调用。由于您正在注册和注销onPause()onResume()您将收到很多电话onReceive()。您可能希望以不同的方式执行此操作。

于 2013-05-07T21:25:27.180 回答
11

感谢您的回答!我的解决方案是所有这些的结果,通过切换到清单声明的接收器并启用/禁用 onResume/onPause 中的接收器组件停止接收器的粘性。通过启用/禁用该组件,我可以确信我是二等公民。

@Override
protected void onResume() {
    super.onResume();
    setReceiverState(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
}

@Override
protected void onPause() {
    super.onPause();
    setReceiverState(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
}


private void setReceiverState(int receiverState) {
    ComponentName receiver = new ComponentName(this, FooBroadcastReceiver.class);
    PackageManager pm = this.getPackageManager();
    pm.setComponentEnabledSetting(receiver, receiverState,       
    PackageManager.DONT_KILL_APP);
}

AndroidManifest.xml

<receiver android:name="com.example.receiver.FooBroadcastReceiver">
  <intent-filter>
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
  </intent-filter>
</receiver>
于 2013-05-07T22:11:45.453 回答
2

这是另一个对我有用的解决方案:

class FooActivity extends Activity {    

    private Intent mReceiverRegisteringIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        broadcastReceiver = new FooBroadcastReceiver();
        intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mReceiverRegisteringIntent = registerReceiver(connectivityReceiver, intentFilter);         
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(connectivityReceiver);
    }


    private class MyBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {       
            if (connectivityAction(intent)) {
                Log.d("footag", "onReceive");
                //if mReceiverRegisteringIntent is not null, then this is the sticky
                //broadcast received when registering the receiver for the first time
                if (mReceiverRegisteringIntent != null) {
                    //set it to null for future broadcasts
                    mReceiverRegisteringIntent = null;
                    return; //do nothing
                }

                //logic for future broadcasts
                ...
            }
        }

        private boolean connectivityAction(Intent intent) {
            return ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction());
        }
    }

}

我假设这MyBroadcastReceiver是一个内部类,FooActivity因此它可以访问mReceiverRegisteringIntent

于 2017-05-11T11:31:44.437 回答
-2

删除以下两行它们不会在 onPause 和 onResume 中触发!

registerReceiver(connectivityReceiver, intentFilter);
unregisterReceiver(connectivityReceiver);
于 2013-05-07T20:23:58.553 回答