Broadcastreceiver和SMSReceiver

时间:2014-01-20 10:08:57

标签: android sms broadcastreceiver

当我在班级SMSReceiver中收到一条消息时,我想在显示活动中播放声音。问题是播放接收声音的每条消息。但是在我收到2条消息的情况下,同时混合了声音。

以下是我的代码,

public class SMSReceiver extends BroadcastReceiver {
static String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

@Override
public void onReceive(Context context, Intent intent) {

    if(intent.getAction().equals(SMSReceiver.SMS_RECEIVED)){
        Bundle bundle = intent.getExtras();
        Object[] messages = (Object[]) bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];
        for (int i=0; i < messages.length; i++) {
            sms[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
        }
        String sms2 = new String();
        for (SmsMessage msg : sms) {
            if (TextUtils.equals(msg.getOriginatingAddress(), number)) {
                Intent i = new Intent(context, DisplayActivity.class);
                sms2 = msg.getMessageBody();
                i.putExtra("SMS",sms2);
                context.startActivity(i);
                abortBroadcast();
            }
        }
    }
}
}


public class DisplayActivity extends Activity {

MessageBDD messagebdd = new MessageBDD(this);
Message message = new Message();

int delay = 0;
boolean isFirstTime = true;
int period = 60000;
static String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private IntentFilter mIntentFilter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display);


    final MediaPlayer mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.son1);

     AudioManager mAudioManager = (AudioManager)getApplication().getSystemService(Context.AUDIO_SERVICE);


        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);

        Timer timer = new Timer();
          timer.scheduleAtFixedRate(new TimerTask() {
          public void run() {
              if (isFirstTime) {
               isFirstTime = false;
               mPlayer.start();
              }
              else{
                  mPlayer.seekTo(0);
                  mPlayer.start();
              }
          }
          }, delay, period);

    /*************Sauvegarde des messages*****************************/
    Bundle extra = getIntent().getExtras();
    String sms = extra.getString("SMS");
    messagebdd.Open();
    message.setContenu(sms);
    messagebdd.insertMessage(message);
    messagebdd.Close();


    /******************Alerte de stop sonnerie**********************/
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Message");
    alertDialog.setMessage("Stopper la notification?");

    alertDialog.setButton("STOP", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        mPlayer.release();
        finish();
    }
    });
    alertDialog.show();
}
}



public class MyService extends Service {
private static final String number = "15555215556";
int delay = 0;
boolean isFirstTime = true;
int period = 60000;
final MediaPlayer mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.son1);

private final BroadcastReceiver receiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
             AudioManager mAudioManager = (AudioManager)getApplication().getSystemService(Context.AUDIO_SERVICE);
             mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);

                        mPlayer.start();

          }
       }
    };

    @Override
       public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(receiver, filter);
       }

     public void onStart(Intent intent, int startId) {      
           super.onStart(intent, startId);

       }

    public void onDestroy() {
           super.onDestroy();
           unregisterReceiver(receiver);
       }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
  }

1 个答案:

答案 0 :(得分:1)

让所有MediaPlayer在另一个单例类中工作,并调用播放音乐的方法播放。并检查mediaPlayer.isPlaying()是否为true然后不播放音乐。 我希望它会有所帮助。