简体   繁体   English

从MainActivity向广播接收器发送数据

[英]Send data to Broadcast Receiver from MainActivity

I am trying to send data to my Broadcast class using 我正在尝试使用以下方式将数据发送到我的Broadcast

In my MainActivity 在我的MainActivity

String stop = "0";
Intent intent = new Intent(getApplicationContext(), SMSReceiver.class);
intent.putExtra("label",stop);
sendBroadcast(intent);

In my Broadcast class 在我的Broadcast课上

public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    Object[] messages = (Object[]) bundle.get("pdus");
    SmsMessage[] sms = new SmsMessage[messages.length];
    final MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.son1);

    // Creation de messages
    for (int i=0; i < messages.length; i++) {
        sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
    }
    for (SmsMessage msg : sms) {

        // Vérifie
        if (TextUtils.equals(msg.getOriginatingAddress(), ewitnumber)) {
            //Toast.makeText(context, "" + msg.getMessageBody(), Toast.LENGTH_SHORT).show();
            Intent i = new Intent(context, DisplayActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);

            String cp = bundle.getString("label");
            Log.d("SMS", ""+cp);

            //music du message
            AudioManager mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);

            Runnable stopPlayerTask = new Runnable(){
                @Override
                public void run() {
                    mPlayer.pause();
                }
            };

            if(mPlayer.isPlaying()){
                mPlayer.pause();
            }else{
            mPlayer.seekTo(startime);
            mPlayer.start();
            mPlayer.setLooping(true);
            }
            Handler handler = new Handler();
            handler.postDelayed(stopPlayerTask, endtime);

            //Efface le message automatiquement
            abortBroadcast();
        }
    }

}

My application crashes. 我的应用程序崩溃。

Do like this 像这样

String cp = bundle.getStringExtra("label");

and Intent also should be like this 和意图也应该是这样的

Intent intent = new Intent(Yourtivity.this, SMSReceiver.class);

Make sure that you're receiver is registered in your manifest, it looks like this. 确保您的接收者已在清单中注册,看起来像这样。

<receiver android:name="your.package.SMSReceiver"> 
     <intent-filter> 
          <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter> 
     <intent-filter> 
          <action android:name="SMSReceiver.action.label" /> 
     </intent-filter> 
</receiver>

When sending broadcast usually I am using intent filter. 通常在发送广播时,我使用意图过滤器。

String stop = "0";
Intent intent = new Intent("SMSReceiver.action.label");
intent.putExtra("label",stop);
sendBroadcast(intent);

Since your receiver is normally for sms PDU or sms data would normally null and app will crash since your are sending a normal broadcast not a SMS one. 由于您的接收器通常用于短信PDU或短信数据通常会为空,并且应用会崩溃,因为您发送的是普通广播而不是短信。 So use the intent filter to get the label data. 因此,请使用意图过滤器来获取标签数据。

public void onReceive(Context context, Intent intent) {

    //get the action of the intent
    String action = intent.getAction();

    if(action.equals("SMSReceiver.action.label")){

         String stop = intent.getStringExtra("label");

         //do something

    } else {

        Bundle bundle = intent.getExtras();
        Object[] messages = (Object[]) bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];
        final MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.son1);

        .
        .
        .
        .
        .
    }
}

I hope this helps. 我希望这有帮助。

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

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