本文主要是介绍基础篇1.8 BroadcastReceiver,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基础篇1.8 BroadcastReceiver
一、Android广播接收器简介
1、 用于接收sendBroadcast()传输过来的intent对象。
2、 注册一个BroadcastReceiver有两种方式。通过Context.registerReceiver()动态注册一个实例。或者在AndroidManifest.xml中通过<receiver>标签静态化一个实现。
3、 如图:
二、注册BroadcastReceiver
1、 在AndroidManifest.xml中进行注册。
(1) 在AndroidManifest.xml中注册的receiver在任何时候都会被执行,即使应用程序关闭。
(2) 应用举例:
AndroidManifest.xml:
<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.solidwang.broadcast"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="9"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.solidwang.broadcast.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=".TestReceiver1">
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED"/>
</intent-filter>
</receiver>
<receiver android:name=".TestReceiver2">
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED"/>
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity:
public classMainActivity extends Activity {
private Button myButton;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(newBroadcastListener());
}
class BroadcastListener implementsOnClickListener {
@Override
public void onClick(View v) {
Intent intent = newIntent();
intent.setAction(Intent.ACTION_DATE_CHANGED);
MainActivity.this.sendBroadcast(intent);
}
}
}
TestReceiver1:
public classTestReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intentintent) {
System.out.println("onReceive.............1");
}
}
TestReceiver2:
public classTestReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intentintent) {
System.out.println("onReceive.............2");
}
}
运行结果:
点击“发送广播”后,调用receiver1和receiver2中的onReceive方法。
2、 在应用程序的代码中进行注册。
(1) 如果一个BroadcastReceiver用于更新UI,那么通常会使用这种方法进行注册,在Activity启动的时候进行注册,在Activity不见时,取消注册。
(2) 应用举例:
MainActivity:
public class MainActivity extends Activity {
private Button registerBtn;
private Button unregisterBtn;
private SMSReceiver smsReceiver;
private staticfinalString SMS_ACTION= "android.provider.Telephony.SMS_RECEIVED";
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerBtn = (Button)findViewById(R.id.registerBtn);
registerBtn.setOnClickListener(new RegisterListener());
unregisterBtn = (Button) findViewById(R.id.unregisterBtn);
unregisterBtn.setOnClickListener(new UnRegisterListener());
}
//注册receiver
class RegisterListener implements OnClickListener {
@Override
public voidonClick(View v) {
smsReceiver = newSMSReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(SMS_ACTION);
MainActivity.this.registerReceiver(smsReceiver, filter);
}
}
//取消注册receiver
class UnRegisterListener implements OnClickListener {
@Override
public voidonClick(View v) {
MainActivity.this.unregisterReceiver(smsReceiver);
}
}
}
SMSReceiver:
public classSMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intentintent) {
System.out.println("smsreceiver");
}
}
AndroidManifest.xml:
<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.solidwang.broadcast_02"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.solidwang.broadcast_02.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>
</application>
</manifest>
运行结果:
这篇关于基础篇1.8 BroadcastReceiver的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!