美文网首页
广播及广播接收器

广播及广播接收器

作者: 四喜汤圆 | 来源:发表于2018-06-03 15:15 被阅读4次
    image.png

    静态注册广播接收器

    <receiver android:name=".MainActivity$MyGPSReceiver"
                android:exported="true"
                android:enabled="true">
         <intent-filter>
               <action android:name="android.location.PROVIDERS_CHANGED"/>
          </intent-filter>
     </receiver>
    

    动态注册广播接收器

    • onResume()中注册【注册完成后会首先收到一条广播
    • onPause()中取消注册
    package com.example.wifidemo;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.wifi.WifiManager;
    import android.os.Bundle;
    import android.support.v4.content.LocalBroadcastManager;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    
    public class MainActivity extends AppCompatActivity {
        private static final String TAG = "MainActivity";
        private MyReceiver mMyReceiver;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            // 取消注册广播接收器
            unregisterMyReceiver();
    
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            // 注册广播接收器
            registerMyReceiver();
        }
    
        private void registerMyReceiver() {
            mMyReceiver = new MyReceiver();
            IntentFilter filter = new IntentFilter();
            // 接收wifi状态改变的广播
            filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
            Log.e(TAG, "在onResume()方法中注册接收器");
            // 注册
            registerReceiver(mMyReceiver, filter);
        }
    
        private void unregisterMyReceiver() {
            if (mMyReceiver != null) {
                unregisterReceiver(mMyReceiver);
                mMyReceiver = null;
            }
    
        }
    
        class MyReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.e(TAG, "收到广播");
                String action = intent.getAction();
                if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
                    int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
                    Log.e("H3c", "wifiState" + wifiState);
                    switch (wifiState) {
                        case WifiManager.WIFI_STATE_DISABLED:
                        case WifiManager.WIFI_STATE_DISABLING:
                        case WifiManager.WIFI_STATE_UNKNOWN:
                            break;
                        case WifiManager.WIFI_STATE_ENABLING:
                        case WifiManager.WIFI_STATE_ENABLED:
                            break;
                        //
                    }
                }
            }
        }
    
    }
    
    

    发送本地广播,注册、取消注册本地广播接收器

    package com.example.wifidemo;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.support.v4.content.LocalBroadcastManager;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    import butterknife.BindView;
    import butterknife.ButterKnife;
    import butterknife.OnClick;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG = "MainActivity";
        private static final String MY_BROADCAST_ACTION = "my_broadcast";
        @BindView(R.id.btn_localbroadcast)
        Button btn_localbroadcast;
        private MyReceiver mMyReceiver;
        private LocalBroadcastManager mLocalBroadcastManager;
        private Context mContext;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            mContext = this;
        }
    
        @OnClick(R.id.btn_localbroadcast)
        public void sendLocalBoradcast(){
            Toast.makeText(this, "btn被点击", Toast.LENGTH_SHORT).show();
            Log.e(TAG,"btn被点击");
            // 发送本地广播
            mLocalBroadcastManager = LocalBroadcastManager.getInstance(mContext);
            Intent intent = new Intent();
            intent.setAction(MY_BROADCAST_ACTION);
            mLocalBroadcastManager.sendBroadcast(intent);
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            // 取消注册广播接收器
            unregisterMyReceiver();
    
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            // 注册广播接收器
            registerMyReceiver();
        }
    
        private void registerMyReceiver() {
            Log.e(TAG, "在onResume()方法中注册接收器");
            mMyReceiver = new MyReceiver();
            IntentFilter filter = new IntentFilter();
            // 接收wifi状态改变的广播
            filter.addAction(MY_BROADCAST_ACTION);
            // 注册
            mLocalBroadcastManager = LocalBroadcastManager.getInstance(mContext);
            mLocalBroadcastManager.registerReceiver(mMyReceiver, filter);
        }
    
        private void unregisterMyReceiver() {
            if (mMyReceiver != null) {
                mLocalBroadcastManager = LocalBroadcastManager.getInstance(mContext);
                mLocalBroadcastManager.unregisterReceiver(mMyReceiver);
                mMyReceiver = null;
            }
    
        }
    
        class MyReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (MY_BROADCAST_ACTION.equals(action)) {
                    Log.e(TAG, "接收到了广播");
                }
    
    
            }
        }
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:广播及广播接收器

          本文链接:https://www.haomeiwen.com/subject/qskjsftx.html