使用以下代码我可以从我的broadcastreciver abortBroadcast()并将短信和地址捆绑到我的活动,我想要做的是从我的活动abortBroadcast(), 所以我的活动检查地址是否与我存储的地址之一相同,然后是abortBroadcast(),但是我找不到一种方法来执行此操作,是否可以通过我的活动或任何方式检查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();????
答案 0 :(得分:0)
我认为这篇文章与你的问题相似,除非我误解。您正试图在broadcastReceiver和您的活动之间传递数据,对吗?这篇文章很相似,我同意第一个回复 - 你应该将你的broadcastReceiver设置为活动的子类
android communicate between activity and broadcast receiver
希望它有所帮助,最好!