Android 在用广播接收机

Android 在用广播接收机,android,broadcastreceiver,android-broadcast,Android,Broadcastreceiver,Android Broadcast,有人能告诉我一种创建应用程序内广播接收器的方法吗?我已经创建了一个广播接收器,它可以为一条消息祝酒。它甚至在应用程序处于后台状态时也能工作,我希望它仅在应用程序处于前台时才能工作 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onC

有人能告诉我一种创建应用程序内广播接收器的方法吗?我已经创建了一个广播接收器,它可以为一条消息祝酒。它甚至在应用程序处于后台状态时也能工作,我希望它仅在应用程序处于前台时才能工作

  public class MainActivity extends Activity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                this.registerReceiver(this.mConnReceiver, new IntentFilter(
                        ConnectivityManager.CONNECTIVITY_ACTION));
            }

            private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {

                public void onReceive(Context context, Intent intent) {
                    boolean noConnectivity = intent.getBooleanExtra(
                            ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                    String reason = intent
                            .getStringExtra(ConnectivityManager.EXTRA_REASON);
                    boolean isFailover = intent.getBooleanExtra(
                            ConnectivityManager.EXTRA_IS_FAILOVER, false);

                    NetworkInfo currentNetworkInfo = (NetworkInfo) intent
                            .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                    NetworkInfo otherNetworkInfo = (NetworkInfo) intent
                            .getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

                    if (currentNetworkInfo.isConnected()) {
                        System.out.println("Connected");
                        Toast.makeText(getApplicationContext(), "Connected",
                                Toast.LENGTH_LONG).show();
                    } else {
                        System.out.println("Not Connected");
                        Toast.makeText(getApplicationContext(), "Not Connected",
                                Toast.LENGTH_LONG).show();
                    }
                }
            };

        }

So here is my code which is checking network state and generating a BroadcastReceiver. I haven't added anything in manifest.

请参阅Context.registerReceiver()和Context.unregisterReceiver()

有很多方法可以做到这一点。我能想到的前两个是这样的:

  • 如果您的
    在清单中声明为
    ,则您可以从应用程序中启用和禁用它,以便仅在应用程序位于前台时启用它。为此,首先通过在
    的清单条目中添加
    android:enabled=“false”
    将接收器设置为禁用。 现在,当您的应用程序运行时,在
    onResume()
    中,您要启用接收器。使用PackageManager.setComponentEnabledSetting()执行此操作。当您的活动转到后台时,可以在
    onPause()
    中再次禁用接收器

  • 动态注册和注销接收器。为此,您不需要在清单中声明接收者。在应用程序中,使用适当的intent筛选器在
    BroadcastReceiver
    onResume()
    调用
    registerReceiver()
    中创建一个实例。当应用程序进入后台时,在
    onPause()
    中调用
    unregisterReceiver()
    将其删除。当应用程序位于前台时,接收器将只接收对
    onReceive()
    的呼叫

  •   public class MainActivity extends Activity {
    
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    this.registerReceiver(this.mConnReceiver, new IntentFilter(
                            ConnectivityManager.CONNECTIVITY_ACTION));
                }
    
                private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
    
                    public void onReceive(Context context, Intent intent) {
                        boolean noConnectivity = intent.getBooleanExtra(
                                ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                        String reason = intent
                                .getStringExtra(ConnectivityManager.EXTRA_REASON);
                        boolean isFailover = intent.getBooleanExtra(
                                ConnectivityManager.EXTRA_IS_FAILOVER, false);
    
                        NetworkInfo currentNetworkInfo = (NetworkInfo) intent
                                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                        NetworkInfo otherNetworkInfo = (NetworkInfo) intent
                                .getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
    
                        if (currentNetworkInfo.isConnected()) {
                            System.out.println("Connected");
                            Toast.makeText(getApplicationContext(), "Connected",
                                    Toast.LENGTH_LONG).show();
                        } else {
                            System.out.println("Not Connected");
                            Toast.makeText(getApplicationContext(), "Not Connected",
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                };
    
            }
    
    So here is my code which is checking network state and generating a BroadcastReceiver. I haven't added anything in manifest.
    

    在哪里以及如何使用它?在你的活动中,当活动进入fg时注册,当活动进入BG时取消注册如果你使用registerReceiver,你不必在声明中声明它OK…你能像David那样解释一下吗…因为我是android新手,第一次使用BR。是的,这很好。只需将
    registerReceiver()
    调用从
    onCreate()
    移动到
    onResume()
    ,并将
    unregisterReceiver()
    调用添加到
    onPause()
    。但现在,只要我恢复活动,它就会“连接”起来,而我的要求是检测网络变化,然后在活动恢复时不进行吐司。是的,这里的问题是,您正在收听的广播意图是一种“粘性”意图。这意味着Android框架保持最新状态,每当广播接收器注册时,它立即以最新状态触发。要解决此问题,可以在
    onReceive()
    中调用
    isInitialStickyBroadcast()
    。如果返回
    true
    ,则可以忽略对
    onReceive()
    的调用,因为这只是通知您当前状态。