Read SMS?


Last Updated:

  1. Sergioum86

    Sergioum86 New Member This Topic's Starter

    Joined:
    Sep 14, 2010
    Messages:
    3
    Likes Received:
    0
    Hello, I am pretty new to android and I'm trying to create an application that reads the text of an incoming SMS, however, I've tried many methods found on several tutorials and none of those seem to work.

    Could anybody help me?

    this is what I've tried:

    Code (Text):
    1. package com.smsReader;
    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 SmsReceptor extends BroadcastReceiver {
    11.     @Override
    12.     public void onReceive(Context context, Intent intent){
    13.         Bundle bundle= intent.getExtras();
    14.         SmsMessage[] msgs= null;
    15.         String str="";
    16.         if(bundle != null ){
    17.             Object [] pdus=(Object[]) bundle.get("pdus");
    18.             msgs=new SmsMessage[pdus.length];
    19.             for(int i=0; i< msgs.length;i++){
    20.                 msgs[i]= SmsMessage.createFromPdu((byte[])pdus[i]);
    21.                 str+= msgs[i].getMessageBody();
    22.                
    23.             }
    24.              Toast.makeText(context, str, Toast.LENGTH_LONG).show();
    25.         }
    26.            
    27.     }
    28.  
    29. }
    30.  
    and this is my Manifest

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.       package="com.smsReader"
    4.       android:versionCode="1"
    5.       android:versionName="1.0">
    6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
    7.         <activity android:name=".MessageReader"
    8.                   android:label="@string/app_name">
    9.             <intent-filter>
    10.                 <action android:name="android.intent.action.MAIN" />
    11.                 <category android:name="android.intent.category.LAUNCHER" />
    12.             </intent-filter>
    13.         </activity>
    14.  
    15.     <receiver android:name=".SmsReceptor" android:enabled="true">
    16.     <intent-filter>
    17.     <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    18.     </intent-filter>
    19.     </receiver>
    20. </application>
    21.     <uses-sdk android:minSdkVersion="7" />
    22.  
    23. <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    24. </manifest>
    However, the application never touches that class, Am I missing something on it?
     

    Advertisement
  2. miXer

    miXer Well-Known Member

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    Are you absolutely shure it doesn't fire SmsReceptor class? The code you have provided looks just fine. It should receive sms fine. Have you tried to put a Toast at the very beginning of the onReceive method?
     
  3. raubvogel

    raubvogel Member

    Joined:
    Sep 17, 2010
    Messages:
    14
    Likes Received:
    0
    Dumb question: what is the &quot;.&quot; in front of the receiver name,
    Code (Text):
    1.  
    for? Mine does not have it and it seems to work. Just trying to understand things. :)
     
  4. miXer

    miXer Well-Known Member

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    I think the "." is a replacement for the package name
     
  5. yogisdk

    yogisdk New Member

    Joined:
    Sep 13, 2010
    Messages:
    2
    Likes Received:
    0
    I have the same issues, and going crazy... I have the correct permission in my manifest, but in the log it reads:

    09-20 19:01:50.710: WARN/ActivityManager(51): Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED (has extras) } to xxx.xxx.xxx requires android.permission.RECEIVE_SMS due to sender com.android.phone (uid 1001)

    Can anyone help?
     
  6. miXer

    miXer Well-Known Member

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    Could you please post your manifest.xml file?
     
  7. yogisdk

    yogisdk New Member

    Joined:
    Sep 13, 2010
    Messages:
    2
    Likes Received:
    0
    Finally got it to work, but i had to declare the receiver dynamically in my service. Dont know why is working for you others, I cant get it to work if I declare it in the manifest, perhaps its like the SCREEN_ON intent which also needs to be received dynamically? Will post the manifest file later on when I am back home
     
  8. Sergioum86

    Sergioum86 New Member This Topic's Starter

    Joined:
    Sep 14, 2010
    Messages:
    3
    Likes Received:
    0
    I got it working... I had an error on my Manifest...
     
  9. meshushelly

    meshushelly New Member

    Joined:
    Nov 15, 2010
    Messages:
    1
    Likes Received:
    0
    Hi

    Can you please share what was the error in your manifest ?!

    10x,
    Moshe.
     

Share This Page

Loading...