2

I am new to Android development and just trying to create a program that will display a Toast if I receive an SMS...here's the code so far

SMSReceiveActivity.java

package sms.receive.sample;
import android.content.*;
import android.os.Bundle;
import android.telephony.*;
import android.util.Log;
import android.widget.Toast;

public class SMSReceiveActivity extends BroadcastReceiver {

private static final String TAG = "Message recieved";

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

 Bundle pudsBundle = intent.getExtras();
 Object[] pdus = (Object[]) pudsBundle.get("pdus");
 SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);    
 Log.i(TAG,  messages.getMessageBody());
 Toast.makeText(context, "SMS Received : "+messages.getMessageBody(),
 Toast.LENGTH_LONG).show();
}
  }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sms.receive.sample"
android:versionCode="1"
android:versionName="1.0" >    
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS" /> 
<application icon="@drawable/ic_launcher" label="@string/app_name">
<receiver 
     android:name=".SMSReceiveActivity" 
     android:exported="true" 
     android:enabled="true">
     <intent-filter android:priority="999">
         <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter>
</receiver>
</application>

When I send (telnet) an SMS to the AVD, I get the nofication in the drawer but not the Toast. I've tried on my phone too and I get the same.... Any help much appreciated.

user1816183
  • 131
  • 1
  • 7
  • In LogCat I get the following when I telnet an SMS, 11-11 15:41:05.315: E/MediaPlayerService(744): Couldn't open fd for content://settings/system/notification_sound 11-11 15:41:05.336: E/MediaPlayer(761): Unable to to create media player – user1816183 Nov 11 '12 at 15:41
  • Try to add: `android.permission.READ_SMS" ` – Maxim Shoustin Nov 11 '12 at 15:51
  • Found the problem...I didn't have an Activity that the user could launch...once I added that everything works okay http://stackoverflow.com/questions/11865434/android-broadcastreceiver-wont-work?rq=1 – user1816183 Nov 11 '12 at 16:01

0 Answers0