【问题标题】:abortBroadcast() from activity活动中止广播()
【发布时间】: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();????

【问题讨论】:

    标签: android broadcastreceiver


    【解决方案1】:

    除非我误解,否则我认为这篇文章与您的问题足够相似。您正在尝试在您的 broadcastReceiver 和您的活动之间传递数据,对吗?这篇文章很相似,我同意第一个回复 - 你应该将你的 broadcastReceiver 设置为活动的子类

    android communicate between activity and broadcast receiver

    希望它有帮助,最好的!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多