I am trying to send data to my Broadcast
class using
In my MainActivity
String stop = "0";
Intent intent = new Intent(getApplicationContext(), SMSReceiver.class);
intent.putExtra("label",stop);
sendBroadcast(intent);
In my Broadcast
class
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.