1

This is my first effort in making an Android app. It is a simple SMS listener that then displays SMS as a Toast.

public class SmsReciever extends BroadcastReceiver {

String msgBody = null;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    if(intent.getAction().equals("android.provider.Telephony.SMS_RECIEVED")){
        Bundle bundle = intent.getExtras();
        SmsMessage[] msg = null;
        String msg_from;
        if(bundle != null)
        {
            try{
                Object[] pdus = (Object[]) bundle.get("pdus");
            msg = new SmsMessage[pdus.length];
            for(int i=0;i<msg.length;i++)
            {
                msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                msg_from = msg[i].getOriginatingAddress();
                msgBody += msg[i].getMessageBody();
            }
            }catch(Exception e)
            {
                Log.d("Exception caught",e.getMessage());
            }
            Toast.makeText(context, msgBody, Toast.LENGTH_SHORT).show();
        }

    }
}}

I am getting the following error:

java.lang.RuntimeException: Unable to instantiate receiver com.example.droid.SmsReceiver: java.lang.ClassNotFoundException: com.example.droid.SmsReceiver

I assure you the SmsReciever.java is in droid/src. I have spent whole night researching on this problem. Please help.

EDIT: This is the manifest xml file that u asked for.

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS" /> 

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" android:permission="android.permission.RECEIVE_SMS">
    <activity
        android:name=".JarvisActivity"
        android:label="@string/title_activity_jarvis" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SecureMessagesActivity" android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".SmsReceiver" android:exported="false" >
        <intent-filter android:priority="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
</receiver>
</application>
</manifest>

EDIT: I did a bit of a research and found out that in my manifest file i have an activity tag which points to SecureMessagesActivity. But in my whole project i don't have an activity by that name. So is this the problem?

EDIT: Okay, So i deleted the SecureMessageActivity part from my manifest file and i guess it removed the error. Now i'm getting this Permisssion denial message:

Permission Denial: receiving Intent { act=android.provider.Telephony.SMS_RECEIVED  flg=0x10 (has extras) } to net.learn2develop.SMSMessaging requires android.permission.RECEIVE_SMS due to sender com.android.phone (uid 1001)

Also net.learn2develop.SMSMessaging activity was used by me in an attempt to copy another code for the same purpose but now i have deleted the whole project and cleaned my own project many times. Still this activity keeps coming again and again. edit Chuck it i'll be starting all over again soon... Thanks for all the input.

BenMorel
  • 34,448
  • 50
  • 182
  • 322

3 Answers3

1

Do you have your receiver in your Adnroid manifest? If yes, please provide AndroidManifest.xml too. You may also want to check these related questions: Android - SMS Broadcast receiver, Android SMS receiver not working, BroadcastReceiver + SMS_RECEIVED.

EDIT:

Try to add android:exported="true" to your BroadcastReceiver. This receiver works for me:

<receiver android:name=".SmsBroadcastReceiver"
          android:enabled="true"
          android:exported="true">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

You have now android:exported="false" that basically tells Android that your BroadcastReceiver cannot receive messages from sources outside your application. More about this tag here: http://developer.android.com/guide/topics/manifest/receiver-element.html.

Community
  • 1
  • 1
Nikolai Samteladze
  • 7,699
  • 6
  • 44
  • 70
0

try to change your receiver like this in manifest :

<receiver android:name="com.example.droid.SmsReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
</receiver>
Naresh J
  • 2,087
  • 5
  • 26
  • 39
  • Still getting the same error.. :( ... Can there be a problem with my Eclipse software or the SDK i installed... I really getting helpless here. –  Sep 29 '12 at 06:28
  • i also implemented such thing. whatever you provided code is perfect. try to clean your project through eclipse. and then check. – Naresh J Sep 29 '12 at 10:23
0

I have idea which can help you: lets try this code:

<receiver android:name="com.example.droid.SmsReceiver" 
        android:exported="true" 
        android:enabled="true">
        <intent-filter android:priority="2147483647">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            <action android:name="android.provider.Telephony.GSM_SMS_RECEIVED" />
            <action android:name="android.provider.Telephony.SMS_RECEIVED2" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter android:priority="2147483647">
            <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
            <data android:scheme="sms" />
            <data android:host="localhost" />
        </intent-filter>
</receiver>

Different types of devices can use its own action name which is not a standard.

Michał Rowicki
  • 1,372
  • 1
  • 16
  • 28