alexkn android第一行代码-7.广播

0.Android 中的广播主要可以分为两种类型,标准广播和有序广播。标准广播(Normal broadcasts)是一种完全异步执行的广播,在广播发出之后,所有的 广播接收器几乎都会在同一时刻接收到这条广播消息,

有序广播(Ordered broadcasts)则是一种同步执行的广播,在广播发出之后,同一时刻 只会有一个广播接收器能够收到这条广播消息。

1.监听系统广播(电量/wifi/时区变化...)

如果需要监听系统广播,需要两步:

(1)重写BroadcastReceiver中的onReceive方法即可,实现自己的接收器;

    public class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"network change",Toast.LENGTH_SHORT).show();
        }
    }

(2)定义一个意图过滤器,过滤出广播

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver,intentFilter);
    }

(3)在AndroidMainifest.xml中注册 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.alex.learn.broadcasttest">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

 

2.发送普通广播

(1)实现一个广播接收器MyBroadcastReceiver,定义在接收到广播时做什么事情

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
    }
}

(2)实现一个广播发送器,定义广播的发送

public class MainActivity extends AppCompatActivity {

    private IntentFilter intentFilter;
    private NetWorkChangeReceiver netWorkChangeReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
                sendBroadcast(intent);
            }
        });
    }

现在com.example.broadcasttest.MY_BROADCAST被绑定在意图中,因此需要通过在AndroidMainifest.xml中注册一个receiver即可

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.alex.learn.broadcasttest">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        ...
        <receiver android:name=".MyBroadcastReceiver">
            <intent-filter>
                <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
            </intent-filter>
        </receiver>
    </application>

 

 

 

posted @ 2016-06-10 09:42  李雷雷alexkn  阅读(460)  评论(0编辑  收藏  举报