[android开发入门]初识BroadcastReceiver

来源:互联网 发布:ftp需要开放的端口 编辑:程序博客网 时间:2024/06/13 16:57

        作者:sundroid

       个人站点:sundroid.cn    邮箱: hfutsnjc@163.com   微博:http://weibo.com/Sundroid

    

   Android系统引入了一种信息传播机制-BroadcastReceiver,这是一种广泛应用于程序之间传输信息的方式,在系统运行过程中会产生很多的系统通知,比如地域变换、电量不足,BroadcastReceiver就相当于收音机,而许许多多的广播台就是消息的发送者,它们通过特定的频率(Action相同),来接受和发送内容。

   在Android系统中发送和接收工作分别由sendBroadcast(Intent intent)方法和注册的BroadcastReceiver类来完成,并且只有发送sendBroadcast(Intent intent)和在manifests中
BroadcastReceiver注册的action一致,才可以正确接收信息。


MainActivity

package cn.sundroid.broadcast;import android.app.Activity;import android.content.Intent;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {    private Button send;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        send = (Button) findViewById(R.id.send);        send.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                //设置广播内容                intent.putExtra("CONTENT","test broadcast");                //设置广播的action                intent.setAction("cn.sundroid.broadcast");                //发送广播                sendBroadcast(intent);            }        });    }}

Receiver

package cn.sundroid.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class Receiver extends BroadcastReceiver {    /**     *     * @param context     * @param intent     */    @Override    public void onReceive(Context context, Intent intent) {        Log.i("RECEIVER",intent.getStringExtra("CONTENT"));    }}


OtherReceiver

package cn.sundroid.broadcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class OtherReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // TODO: This method is called when the BroadcastReceiver is receiving        Log.i("OTHERRECEIVER", intent.getStringExtra("CONTENT"));    }}


mainfest

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.sundroid.broadcast" >    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver            android:name=".Receiver"            android:enabled="true"            android:exported="true" >            <intent-filter>                <action android:name="cn.sundroid.broadcast" />            </intent-filter>        </receiver>        <receiver            android:name=".OtherReceiver"            android:enabled="true"            android:exported="true" >            <intent-filter>                <action android:name="cn.sundroid.receiver" />            </intent-filter>        </receiver>    </application></manifest>



我们可以看出结果中打印的Log为"RECEIVER"+test broadcast,这里的调用的BroadcastReceiver为Receiver,而不是OtherReceiver,就是因为OtherReceiver在manifests里面注册的anction和sendBroadcast(Intent intent)里面设置的Action不一致的结果,那么如果Receiver和OtherReceiver在manifests里都设置相同的action,结果会如何呢?


  这里就不必再做解释了吧!

后台服务还可以查看初识Service

0 0
原创粉丝点击