SMS Receiver + Toast = FAIL :(

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

SMS Receiver + Toast = FAIL :(

Postby electrophoretic » Wed Aug 04, 2010 8:12 pm

Hi Guys...

I am trying to get this code to work on my Dell Streak but when I receive an SMS I still dont get a Toast notification... I have added the 'receive' tag in the manifest.xml file... I am a complete noob at this and need a little help getting started :)

Code: Select all
package net.learn2develop.SMSMessaging;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();       
        SmsMessage[] msgs = null;
        String str = "";           
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];           
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);               
                str += "SMS from " + msgs[i].getOriginatingAddress();                     
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";       
            }
            //---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }                         
    }
}


thanks

G
electrophoretic
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Aug 04, 2010 8:07 pm

Top

Re: SMS Receiver + Toast = FAIL :(

Postby techkanika » Fri Aug 13, 2010 6:38 am

Hi electrophoretic,

Try this code..
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void onReceive(final Context context, Intent intent)
  3.         {              
  4.                 if (intent.getAction().equals(ACTION))
  5.                 {
  6.                         sb = new StringBuilder();
  7.                     bundle = intent.getExtras();
  8.                     if (bundle != null)
  9.                     {
  10.                                 pdusObj = (Object[]) bundle.get("pdus");
  11.                     messages = new SmsMessage[pdusObj.length];
  12.                     for (int i = 0; i<pdusObj.length; i++)
  13.                     {
  14.                         messages[i] = SmsMessage.createFromPdu ((byte[])pdusObj[i]);
  15.                                 for (SmsMessage currentMessage : messages)
  16.                                 {
  17.                                           ----- Do whatever you want for each SMS -----
  18.                                          ....................Toast Notification...................
  19.                                 }
  20.                            }
  21.                   }
  22.        }
  23. }
  24.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Thanks
Kanika Rakheja
User avatar
techkanika
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Fri May 28, 2010 7:49 am

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 7 guests