將數據發送到廣播接收器,我嘗試將數據發送到使用從MainActivity
我Broadcast
類在我MainActivity
String stop = "0";
Intent intent = new Intent(getApplicationContext(), SMSReceiver.class);
intent.putExtra("label",stop);
sendBroadcast(intent);
在我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();
}
}
}
我的應用程序崩潰。
你可以發佈你的onReceive方法嗎?你有沒有在清單中註冊你的廣播接收機? –
我更新了帖子。謝謝 – Merv
順便說一句你爲什麼要發送廣播到SMSReceiver?你的目標是簡單地發送字符串「標籤」到廣播? –