Click here to Skip to main content
14,450,268 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,I am new to android and i am using android 2.1 for some sms_receive thing: when an sms received,it won't work... Nothing happens when sms received, help!
Code for androidmanifest.xml:



<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    package="com.example.smsreceiver"
    android:versionCode="1"
    android:versionName="1.0">
    
    <uses-sdk android:minsdkversion="7" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <application android:label="@string/app_name">
        <activity>
            android:name=".SmsReceiver"
            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="com.example.smsreceiver.SmsReceiver" android:enabled="true">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>


and this is may class
package ir.avr.SmsReceiver;

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 {

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";



    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(SMS_RECEIVED)) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                if (pdus.length == 0) {
                    return;
                }
                SmsMessage[] messages = new SmsMessage[pdus.length];
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < pdus.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    sb.append(messages[i].getMessageBody());
                }
                String sender = messages[0].getOriginatingAddress();
                String message = sb.toString();
                Toast.makeText(context, "SMS RECEIVED", Toast.LENGTH_SHORT).show();

            }
        }
    }
}
thanks alot
Posted
Updated 27-Aug-13 2:05am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100