0

I have an activity in my app which plays a buzzer on receiving a specific keyword as a message but my code doesn't work at all. According to Logcat, it receives the message.

public class SmsReceiver extends BroadcastReceiver {
    int k = 0;
    public static final String SMS_EXTRA_NAME = "pdus";
    MediaPlayer mPlay = new MediaPlayer();

    public void onReceive(Context context, Intent intent) {
        // Get the SMS map from Intent
        Bundle extras = intent.getExtras();
        String body = "";
        SharedPreferences myPrefs = context.getSharedPreferences("test", Context.MODE_PRIVATE);
        String password = myPrefs.getString("password", "a");
        String find = "find " + password;
        mPlay.create(context, R.raw.music);
        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
        mPlay.setAudioStreamType(AudioManager.STREAM_MUSIC);

        if (extras != null) {
            // Get received SMS array
            Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);

            for (int i = 0; i < smsExtra.length; ++i) {
                SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);

                body = sms.getMessageBody().toString();
                if (body.equalsIgnoreCase(find)) {
                    this.abortBroadcast();
                    int dur = mPlay.getDuration();
                    for (int j = 0;; j += dur) {
                        if (k == 1)
                            break;
                        mPlay.start();
                        try {
                            Thread.sleep(dur);
                        } catch (InterruptedException e) {
                        }
                    }
                }

            }
        }
    }

    @Override
    public void onKeyDown() {
        k = 1;
    }

    public void onDestroy() {
        mPlay.stop();
        mPlay.release();
    }
}
ashu
  • 1,756
  • 4
  • 22
  • 41

2 Answers2

1

Have you set

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

?

Then here is what I've made

public class SmsReceiver extends BroadcastReceiver {

    private Context cont;

    @Override
    public void onReceive(Context context, Intent intent) {
        cont = context;
        Bundle extras = intent.getExtras();

        if (extras != null) {
            Object[] smsExtra = (Object[]) extras.get("pdus");
            SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[0]);
            String phoneNumber = sms.getOriginatingAddress();
            String body = sms.getMessageBody().toString();
            if (body.equalsIgnoreCase("something")) {
                // do your stuff
            }
        }
    }
}

If this doesn't answer your question, please tell us what is not working.

Romain Pellerin
  • 2,470
  • 3
  • 27
  • 36
  • Thanks for the answer. It worked perfect. My code under if statement is still the same and I am unable to break for loop for playing media object. Either because i am not able to override onKeyDown() or I am using wrong approach.. Pls help me.! – ashu Aug 17 '13 at 02:05
  • What are you trying to do? Do you want to play loop the music and be able to stop it with "onKeyDown()" ? – Romain Pellerin Aug 17 '13 at 02:16
  • yes! the audio is playing correctly but my for loop doesn't break. Reason can be any of the above two. – ashu Aug 17 '13 at 02:22
  • Well, you want to play loop until user presses a key ? – Romain Pellerin Aug 17 '13 at 02:23
  • yes! I want the loop until user presses key. You can see my approach of doing this.from the code in.question. – ashu Aug 17 '13 at 02:25
1

Another improvement would be to do this for your loop:

mp = MediaPlayer.create(context, R.raw.music);
mp.setOnCompletionListener(new OnCompletionListener() {
   @Override
   public void onCompletion(MediaPlayer mp) {
      mp.start();
   }
});   
mp.start();
Romain Pellerin
  • 2,470
  • 3
  • 27
  • 36