如何在android中检测传入的消息并在android中播放音乐?

时间:2014-02-26 15:27:08

标签: android sms broadcastreceiver

它会打印语句,但它不播放音乐。我在下面发布了我的代码。

    if(intent.getAction().toString().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED") ) //.equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED"))

    {
        notificationfunction();
        System.out.println("message receive inside power receiver");
    }
}

private void notificationfunction() 
{
    /*  // TODO Auto-generated method stub
                        if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
                        {
                             mMediaPlayer.reset();
                             //mMediaPlayer.stop();

                        }
     */
    switch (np)
    {
    case R.id.nradioButton1:
        mMediaPlayer = MediaPlayer.create(context, R.raw.notification1);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setLooping(true);

        if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
        {
            mMediaPlayer.reset();
        }
        mMediaPlayer.start();
        break;

    case R.id.nradioButton2:
        mMediaPlayer = MediaPlayer.create(context, R.raw.notification2);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setLooping(true);
        if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
        {
            mMediaPlayer.reset();
        }
        mMediaPlayer.start();
        break;

    case R.id.nradioButton3:
        mMediaPlayer = MediaPlayer.create(context, R.raw.notification3);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setLooping(true);
        if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
        {
            mMediaPlayer.reset();
        }
        mMediaPlayer.start();
        break;

    case  R.id.nradioButton4:
        mMediaPlayer = MediaPlayer.create(context, R.raw.notification4);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setLooping(true);

        if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
        {
            mMediaPlayer.reset();
        }
        mMediaPlayer.start();
        break;

    case R.id.nradioButton5:
        mMediaPlayer = MediaPlayer.create(context, R.raw.notification5);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setLooping(true);

        if ( mMediaPlayer.isPlaying() )  // Checking if already playing any song
        {
            mMediaPlayer.reset();
        }
        mMediaPlayer.start();

        break;
    }
}

我不知道我哪里错了。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

你还没有给你的开关一个变量来检查,

     notificationfunction(View np){
     switch(np.getId()){
        //cases
        }
     }

如果您不打算查看已检查的单选按钮的视图,则应使用if和else,如果条件与radiobuttons的状态相反,而不是视图ID上的开关:

    if(RadioButton1.ischecked()){
        //do something
    }else if (RadioButton2.isChecked(){
        //do something else
    }//etc.