1. Introducing Channels - a new way to chat with other Android users!
    Dismiss Notice

SMS Receiver rubbishness :p


Last Updated:

  1. electrophoretic

    electrophoretic New Member This Topic's Starter

    Joined:
    Aug 3, 2010
    Messages:
    1
    Likes Received:
    0
    Hi Guys...

    Code (Text):
    1. package net.learn2develop.SMSMessaging;
    2.  
    3. import android.content.BroadcastReceiver;
    4. import android.content.Context;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.telephony.SmsMessage;
    8. import android.widget.Toast;
    9.  
    10. public class SMSReceiver extends BroadcastReceiver
    11. {
    12.     @Override
    13.     public void onReceive(Context context, Intent intent)
    14.     {
    15.         //---get the SMS message passed in---
    16.         Bundle bundle = intent.getExtras();        
    17.         SmsMessage[] msgs = null;
    18.         String str = "";            
    19.         if (bundle != null)
    20.         {
    21.             //---retrieve the SMS message received---
    22.             Object[] pdus = (Object[]) bundle.get("pdus");
    23.             msgs = new SmsMessage[pdus.length];            
    24.             for (int i=0; i<msgs.length; i++){
    25.                 msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
    26.                 str += "SMS from " + msgs[i].getOriginatingAddress();                    
    27.                 str += " :";
    28.                 str += msgs[i].getMessageBody().toString();
    29.                 str += "\n";        
    30.             }
    31.             //---display the new SMS message---
    32.             Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    33.         }                        
    34.     }
    35. }
    why doesnt this work!? I installed it on my Dell Streak, received an SMS and nothing... I have added the Receiver tag in the manifest file as well...

    please help I am going to college in 4 weeks to learn mobile development and am trying to get a heads up on it before I start but this simple app is taking the mick :(

    Thanks,

    Gordy
     

    Advertisement
  2. miXer

    miXer Well-Known Member

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    Hey. I use this code to receive new sms:

    Manifest broadcastreceiver tag:
    Code (Text):
    1. <receiver android:name=".SMSHandlerBR">
    2.             <intent-filter>
    3.                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    4.             </intent-filter>
    5.         </receiver>
    SMSHandlerBR.java:
    Code (Text):
    1. package SMSHandler;
    2.  
    3. import android.content.BroadcastReceiver;
    4. import android.content.Context;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.telephony.SmsMessage;
    8. import android.widget.Toast;
    9.  
    10. public class SMSHandlerBR extends BroadcastReceiver
    11. {
    12.     @Override
    13.     public void onReceive(Context context, Intent intent)
    14.     {
    15.         Bundle bundle = intent.getExtras();
    16.  
    17.         if (bundle != null)
    18.         {
    19.             Object[] pdus = (Object[])bundle.get("pdus");
    20.             SmsMessage[] msgs = new SmsMessage[pdus.length];
    21.  
    22.             for (int i = 0; i < msgs.length; i++)
    23.             {
    24.                 msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
    25.  
    26.                 try
    27.                 {
    28.                     Intent in = new Intent(context, SMSHandlerDialog.class);
    29.                     in.putExtra("number", msgs[i].getOriginatingAddress());
    30.                     in.putExtra("message", msgs[i].getMessageBody().toString());
    31.                     in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    32.                     context.startActivity(in);
    33.                 }
    34.                 catch (Exception ex) { Toast.makeText(context, "Get sms exception: " + ex.getMessage(), Toast.LENGTH_LONG).show(); }
    35.             }
    36.         }
    37.     }
    38. }
    This works well for me
     

Share This Page

Loading...