public abstract class BroadcastReceiver;
Base class for code that will receive intents sent by sendBroadcast().
If you don‘t need to send broadcasts across applications, consider using this class with LocalBroadcastManager
instead of the more general facilities described below. This will give you a much more efficient implementation (no cross-process communication needed) and allow you to avoid thinking about any security issues related to other applications being able to receive or send your broadcasts.
You can either dynamically register an instance of this class with Context.registerReceiver()
or statically publish an implementation through the <receiver>
tag in your AndroidManifest.xml
.
注册广播,有2中方法:
1. 在Manifest.xml文件中注册,需要一个android:name设置,所以我们继承BroadcastReceiver的类需要单独作为一个类文件出现,而在java代码中注册,我们可以使用一个内部类就可以了。
2. 在Mainfest.xml文件中注册过的BroadcastReceiver,即使我们对应的程序已经关闭,但是只要我们的系统(比如说手机,平板)开启,这该BroadcastReceiver依然处于活动状态,依然可以接受到广播信息。
如果我们需要实时监测某个广播信息,则可以采用在Mainfest.xml中进行注册,但如果我们用广播信息来更新activity的UI,则应该采用在java代码中进行注册。
DEMO1:
首先我们创建一个继承自BroadcastReceiver的接收者:
package com.example.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class TestReciver extends BroadcastReceiver { public TestReciver() { System.out.println("TestReceiver"); } @Override public void onReceive(Context context, Intent intent) { System.out.println("TestReciver --> OnReceiver"); } }
在配置文件中添加对应的节点:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcastdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/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="com.example.broadcast.TestReciver"> <intent-filter> <action android:name="android.intent.action.EDIT"/> </intent-filter> </receiver> </application> </manifest>
在MainActivity.java中对广播的具体操作:
package com.example.broadcastdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener{ private Button btnStart = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStart = (Button)findViewById(R.id.btnStart); btnStart.setOnClickListener(this); } @Override public void onClick(View view) { //采用intent发送数据 Intent intent = new Intent(); //只发送给action为ACTION_EDIT的对象 intent.setAction(Intent.ACTION_EDIT); //通过广播方式发送该intent,只有特定action的接收者才能收到该广播信息 MainActivity.this.sendBroadcast(intent); } }
点击BUTTON按钮,测试结果:
注意,只有匹配的Intent才会被执行,并不是所有的Receive都能接收到哦!