Android中BroadcastReceiver广播
BroadCastReceiver 简介
广播接收者( BroadcastReceiver )用于接收广播 Intent ,广播 Intent 的发送是通过调用 Context.sendBroadcast() 、 Context.sendOrderedBroadcast() 来实现的。通常一个广播 Intent 可以被订阅了此 Intent 的多个广播接收者所接收。
广播是一种广泛运用的在应用程序之间传输信息的机制 。而 BroadcastReceiver 是对发送出来的广播进行过滤接收并响应的一类组件;
来自普通应用程序,如一个应用程序通知其他应用程序某些数据已经下载完毕。
BroadcastReceiver 自身并不实现图形用户界面,但是当它收到某个通知后, BroadcastReceiver 可以启动 Activity 作为响应,或者通过 NotificationMananger 提醒用户,或者启动 Service 等等。
生命周期
编写类继承BroadcastReceiver,复写onReceiver()方法
package com.example.receive;
import android.content.BroadcastReceiver;
import
android.content.Context;
import android.content.Intent;
import
android.util.Log;
public class MyReceivceReg extends BroadcastReceiver {
private static final String TAG = "MyReceivce";
public
MyReceivceReg(){
Log.i(TAG,"MyReceivceReg");
}
@Override
public
void onReceive(Context context, Intent intent)
{
Log.i(TAG,"onReceiveReg");
}
}
<receiver android:name="com.example.receive.MyReceivce"> <intent-filter > <action android:name="android.provider.Telephony.SMS_RECEIVED"/> </intent-filter> </receiver>
要接收某些action,需要在AndroidManifest.xml里面添加相应的permission。例如接收SMS:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
……
</manifest>
界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".AndroidBroadcastServiceActivity" > <Button android:id="@+id/btnStartBroad" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动Broadservice" > </Button> <Button android:id="@+id/btnRegisterBroad" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册RegisterBroad" > </Button> <Button android:id="@+id/btnUnRegisterBroad" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消RegisterBroad" > </Button> </LinearLayout>
构建Intent,发送
package com.example.androidbroadcastservice; import com.example.receive.MyReceivceReg; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class AndroidBroadcastServiceActivity extends Activity { protected static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";//广播类型。其实是对应Manifest.xml中<action android:name="android.provider.Telephony.SMS_RECEIVED"/> private Button btnStartBroad,btnRegisterBroad,btnUnRegisterBroad; private MyReceivceReg receivece; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_android_broadcast_service); btnStartBroad = (Button) this.findViewById(R.id.btnStartBroad); btnStartBroad.setOnClickListener(onclick); btnRegisterBroad = (Button) this.findViewById(R.id.btnRegisterBroad); btnRegisterBroad.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { receivece=new MyReceivceReg(); IntentFilter filter=new IntentFilter(); filter.addAction(ACTION); registerReceiver(receivece, filter); } }); btnUnRegisterBroad = (Button) this.findViewById(R.id.btnUnRegisterBroad); btnUnRegisterBroad.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //MyReceivceReg receivece=new MyReceivceReg(); //IntentFilter filter=new IntentFilter(ACTION); unregisterReceiver(receivece); } }); } private OnClickListener onclick = new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setAction(ACTION); sendBroadcast(intent); } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_android_broadcast_service, menu); return true; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。