BroadcastReceiver,广播接收者,用于异步接收广播。
下面就以接收短信广播为例:
import android.app.Activity;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class BroadcastReceiverTestActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout mainLayout;
Button button1;
Button button2;
TextView tv;
MyBroadcastReceiver mybr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ui_init();
setContentView(mainLayout);
}
/**
* ui初始化
*/
void ui_init(){
mainLayout=new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
button1=new Button(this);
button1.setText("on");
button1.setPadding(10, 10, 10, 10);
button1.setOnClickListener(new RegisterReceiverListener());
button2=new Button(this);
button2.setText("off");
button2.setPadding(10, 10, 10, 10);
button2.setOnClickListener(new UnRegisterReceiverListener());
tv=new TextView(this);
tv.setBackgroundColor(Color.WHITE);
tv.setTextColor(Color.BLACK);
tv.setTextSize(20);
tv.setText("hi\n\n");
mainLayout.addView(button1);
mainLayout.addView(button2);
mainLayout.addView(tv);
}
class RegisterReceiverListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mybr=new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
//filter可以添加多个动作
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
//对接收器进行注册到系统中
BroadcastReceiverTestActivity.this.registerReceiver(mybr, filter);
}
}
class UnRegisterReceiverListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
BroadcastReceiverTestActivity.this.unregisterReceiver(mybr);
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("收到短信");
}
}
————————————————————————————————————————————————————————————————————
———————————分割线————————————————————分割线———————————————分割线—————————————
————————————————————————————————————————————————————————————————————
注意事项与进阶
1、记得添加权限:<uses-permission android:name="android.permission.RECEIVE_SMS"/>
2、IntentFilter可以添加多个动作(ACTION),当收到广播时,为了区分具体是哪种广播,可以用intent.getAction()函数来判断。
3、本例使用RECEIVE_SMS动作为参考,我们可以通过如下方法获取短信内容。这算是短信窃听吧,呵呵(Android就是这么奔放)。
// 接受Intent对象当中的数据
Bundle bundle = intent.getExtras();
// 在Bundle对象当中有一个属性名为pdus,这个属性的值是一个Object数组
Object[] myOBJpdus = (Object[]) bundle.get("pdus");
// 创建一个SmsMessage类型的数组
SmsMessage[] messages = new SmsMessage[myOBJpdus.length];
for (int i = 0; i < myOBJpdus.length; i++) {
// 使用Object数组当中的对象创建SmsMessage对象
messages[i] = SmsMessage.createFromPdu((byte[]) myOBJpdus[i]);
// 调用SmsMessage对象的getDisppalyMessageBody()方法,就可以得到消息的内容
System.out.println(messages[i].getDisplayMessageBody());
}