Android广播接受和一些错误解决
在应用程序之间进行通信的一个机制。当然程序内部之间也可以进行通信。就要看你自己要接受什么样子
的广播了!要接受什么样子的广播,你就要加载什么样子的广播哦!
比如短信接收。我在做的时候可能在OnReceive()中的代码没有被调用,可能出现的问题如下:
1,可能在你xml文件中可能没有加权限,或者你加的权限不是再<application>标签之外。还有注意是不
是你写的权限标签中有没有拼错。 实在还有一种变态的可能: 把权限删除,再重新写一遍
2,可能在你注册接收器的时候的那个Action可能报错。
3,当你想显示短信内容在界面时,可能也报错,这时候尝试删除布局文件,再重新写布局文件(这种情况我也看到遇到过)
注册有两种方法:
1,在XML文件注册广播接受器
2,通过代码来注册广播接受器
我这里通过第二种
做短信广播接受的步骤:
1,写一个类来继承BroadcastReceiver,重写OnReceive()方法
2,在OnStart()方法中注册和实例化我们写的广播接收器类
3,记住,在OnDestory()中要取消广播接收器注册。
MainActivity。activity
package com.zql;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
MyBorder receiver=null;
public static TextView textView1 =null;
public static TextView textView2 =null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1=(TextView) this.findViewById(R.id.textView1);
textView2=(TextView) this.findViewById(R.id.textView2);
}
@Override
protected void onDestroy() {
// 取消广播接收器
this.unregisterReceiver(receiver);
super.onDestroy();
}
@Override
protected void onStart() {
//注册广播接收器和实例化广播接收器
receiver=new MyBorder();
IntentFilter filter=new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
this.registerReceiver(receiver, filter);
super.onStart();
}
}
MyBorder类
package com.zql;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class MyBorder extends BroadcastReceiver {
public static String telNumber=null;
public static String content=null;
@Override
public void onReceive(Context context, Intent intent) {
//重写OnReceive方法
//1,判断接受的广播是不是短信广播
System.out.println("sfsdffhfh");
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
//2,如果接收到这个短信的广播之后,就来接受这个广播,这个广播上信息一般都是存放在
//intent对象的,intent就想一个邮递员一样
Bundle bundle = intent.getExtras();
//3,判断Bundle的是不是为空
if(bundle!=null){
//4,得到Bundle里的信息,“pdus”是Android自带的关键字,因为可能是包含多个信息,
//返回的是数组
Object[] SMSData=(Object[]) bundle.get("pdus");
//把Object对象转换为SmsMessage对象,也就是解析从Bundle中得到的数据,因为
//从Bundle得到的数据并不是直接得到的是SmsMessage对象
SmsMessage[] smsMessage=new SmsMessage[SMSData.length];
for(int i=0;i<smsMessage.length;i++){
//最终得到了我们想要的那个短信对象,可以包含哪个号码发的信息,信息的内容等
smsMessage[i] = SmsMessage.createFromPdu((byte[]) SMSData[i]);
}
//通过smsMessage的相应方法得到我们想要的信息,比如说:手机号码,信息内容
//因为这里我用模拟器发送短信,因此每次只是发送一条短信。所以取第一个数据
String telNumber=smsMessage[0].getDisplayOriginatingAddress();
String content=smsMessage[0].getDisplayMessageBody();
MainActivity.textView1.setText(telNumber);
MainActivity.textView2.setText(content);
}
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
浙公网安备 33010602011771号