简体   繁体   English

广播意图/接收者

[英]broadcast Intent / receiver

In my Application I have a Broadcast receiver (registered in manifest) class from which I want to send an Intent to MainActivity. 在我的应用程序中,我有一个广播接收器(在清单中注册)类,我想从中将Intent发送给MainActivity。 Therefore I have another broadcast receiver (dynamically registered) in MainActivity and an Intent Filter. 因此,我在MainActivity中有另一个广播接收器(动态注册)和一个意图过滤器。 But I don't receive the Intent in Main Activity. 但是我没有收到主要活动的意图。 This is the code: 这是代码:

public class SmsReceiver extends BroadcastReceiver {

    public static final String SMS_BUNDLE = "pdus";

    public SmsReceiver(){

    }
        String TAG = SmsReceiver.class.getSimpleName();
        @Override
        public void onReceive(Context context, Intent intent){
            Bundle bundle = intent.getExtras();

            if (bundle != null) {
                Object[] sms = (Object[]) bundle.get(SMS_BUNDLE);
                String str = "";

                for (int i=0; i < sms.length; i++) {
            SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
                    String smsbody = smsMessage.getMessageBody().toString();
                    str += smsbody;
                }
                Intent bcIntent = new Intent();
                bcIntent.setAction("SMS_RECEIVED_ACTION");
                bcIntent.addCategory(Intent.CATEGORY_DEFAULT);
                bcIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
                bcIntent.putExtra("message", str);
                context.sendBroadcast(bcIntent);
            }
        }
    }

and in the MainActivity: 并在MainActivity中:

public class MainActivity extends AppCompatActivity
{
public boolean receivedSMS;
public String displaySMS;

  private BroadcastReceiver iReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    String qqq = intent.getExtras().getString("message");
    info2(intent.getExtras().getString("message"));
    evalMsg(qqq);
  }
};

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    graphInit();
    timerInit();
    smnInit();

  }

  @Override
  protected void onResume(){

  super.onResume();
  IntentFilter iFilter = new IntentFilter();
    iFilter.addAction("SMS_RECEIVED_ACTION");

    registerReceiver(iReceiver, iFilter);

  }

  @Override
  protected void onPause(){
    unregisterReceiver(iReceiver);
    super.onPause();
  }

Does anyone know why the broadcast receiver in the main activity doesnt receive the intent? 有谁知道为什么主活动中的广播接收器没有收到意图? There should be a text displayed in the TextView but it is never shown. 应该在TextView中显示一个文本,但永远不会显示。 thanks for any idea 谢谢你的任何想法

When receiving the intent via SmsReceiver you can't know if the Activity is up and in what state. 通过SmsReceiver接收意图时,您不知道活动是否启动以及处于什么状态。 What you should consider is using startActivity(android.content.Intent) from within your SmsReceiver.onReceive() . 您应该考虑在SmsReceiver.onReceive()使用startActivity(android.content.Intent) SmsReceiver.onReceive() This way you'll have the same intent object available within Activity.onCreate (just call the getIntent() method) 这样,您将在Activity.onCreate使用相同的intent对象(只需调用getIntent()方法)

But if you want to stick to your original design of double broadcasts, then here is your answer . 但是,如果您想坚持原始设计的双重广播,那么这就是您的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM