【发布时间】:2011-11-02 19:32:54
【问题描述】:
使用以下代码,我可以从我的广播接收器 abortBroadcast() 并将短信和地址捆绑到我的活动中,我想要做的是从我的活动中中止广播(), 所以我的活动检查地址是否与我存储的地址之一相同然后中止广播(),但我找不到这样做的方法,是否可以以任何方式从我的活动中中止广播()或任何检查方式活动的详细信息然后允许广播接收器中止?
这是我的代码:
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
// a notification message
String messages = "";
if (extras != null) {
Object[] smsExtra = (Object[]) extras.get("pdus");
for (int i = 0; i < smsExtra.length; ++i) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
String body = sms.getMessageBody().toString();// sf
String address = sms.getOriginatingAddress();
messages += "SMS from " + address + " :\n";
messages += body + "\n";
}
Intent data = new Intent(context, Details.class);
data.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
for (int i = 0; i < smsExtra.length; ++i) {
// get sms message
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress().toString();
// bundle
Bundle bundle1 = new Bundle();
bundle1.putString("title", body);
bundle1.putString("title2", address);
data.putExtras(bundle1);
context.startActivity(data);
abortBroadcast();
}
}
}
}
在我的活动中:
Bundle bundle1 = this.getIntent().getExtras();
String title = bundle1.getString("title");
String title2 = bundle1.getString("title2");
((EditText) findViewById(R.id.ettext)).setText(title);
((EditText) findViewById(R.id.etnumber)).setText(title2);
mSms =(TextView)findViewById(R.id.tvsms);
String sms = mSms.getText().toString();
if (sms.equals(title2))
abortBroadcast();????
【问题讨论】: