我想在 BroadCast receiver 类中获取一个全局数据。
我用的下面的代码,进入BroadCast receiver 类
public class Global extends Application{
String mobileNumber;
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
Receiver 类
public class TextMessageReceiver extends BroadcastReceiver{
Global global;
Context context;
int count;
String number;
public void onReceive(Context context, Intent intent)
{
global=(Global)context;
number = global.getMobileNumber();
Bundle bundle=intent.getExtras();
Object[] messages=(Object[])bundle.get("pdus");
SmsMessage[] sms=new SmsMessage[messages.length];
for(int n=0;n<messages.length;n++){
sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
}
for(SmsMessage msg:sms){
if(msg.getOriginatingAddress().endsWith(number))
{
SMS.updateMessageBox(msg.getOriginatingAddress(),msg.getMessageBody(),context);
/*SMS.updateMessageBox("\nFrom: "+msg.getOriginatingAddress()+"\n"+
"Message: "+msg.getMessageBody()+"\n");*/
/*((SMS) context).delete();*/
abortBroadcast();
}
}
}
我需要从 getter setter 获取电话号码,如何获取?