SMS receiver didn't work
Asked Answered
B

1

0

i am new with android SMS and I am trying to study it. So i am trying to make an android app that always works on the background, and when I receive sms with the content of "hello"(somebody sending "hello" to my phone by SMS) a music will play.

I searched all over the internet, and I built a project that worked on the AVD, but when I tested it on my phone it didn't work:

import info.kfsoft.android.R.raw;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.TextView;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{

    MediaPlayer ourSong;
    //static final int uniqueID=12345;
    TextView display;

    public void onReceive(Context context, Intent intent)
    {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        ourSong = MediaPlayer.create(context, R.raw.music2);


        Bundle myBundle = intent.getExtras();
        SmsMessage [] messages = null;
        String strMessage = "";

        if (myBundle != null)
        {
            Object [] pdus = (Object[]) myBundle.get("pdus");
            messages = new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++)
            {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
          //      strMessage += "SMS From the king him self: " + messages[i].getOriginatingAddress();
      //          strMessage += " : ";
               strMessage += messages[i].getMessageBody();
         //       strMessage += "\n";


            }
    //max volume
            AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                am.setStreamVolume(AudioManager.STREAM_MUSIC,am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);

            Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();

     //       Notification n=new    Notification(android.R.drawable.)
     //       nm.notify(uniqueID,n);

            ourSong.start();
        }
    }
    public void a(){

    }

}

THX.

Beacon answered 17/9, 2014 at 19:42 Comment(0)
I
2

Maybe it's your intent priority too low and system-installed SMSApp receives a message before your application. Try adding priority="999"

<receiver android:name="OnAlarmReceiver">
    <intent-filter android:priority="999">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    </intent-filter>
</receiver>

Another trick is not register SMS_RECEIVED in a AndroidManifest.xml file but programmatically in an application code. Here is an example where I actually register multiple events to a same broadcastreceiver.

broadcastReceiver = new EventReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY-1);
registerReceiver(broadcastReceiver, filter);
Important answered 17/9, 2014 at 20:31 Comment(1)
@asdasd asd: Please accept this answer (green checkmark), minor issue it's just how SO wants us users to behave :)Important

© 2022 - 2024 — McMap. All rights reserved.