[android开发入门]初识BroadcastReceiver

        作者: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值