1

Hi I am developing an app which is fully based on sms. I can send the successfully to required number, but my problem is that I am not getting the send and delivered notification even if I am using the code to get both notification through intentfilter and pending intent. The code for sending sms and recieving notification is done inside a class (SmsHandling.java) which extends broadcast reciever. My program code is here..

public class SmsHandling extends BroadcastReceiver{
     IntentFilter sendFilter,deliveredFilter;
     Context mcontext;
     String result="got it";
    public SmsHandling(Mobile mobile) {
        // TODO Auto-generated constructor stub
        mcontext=mobile;
    }
    String sendSms(String num,String messege){
        Log.d("inside method", "getting");
        String SENT="SMS_SENT";
        String DELIVERED="SMS_DELIVERED";

        sendFilter=new IntentFilter("SMS_SEND");
        deliveredFilter=new IntentFilter("SMS_DELIVERED");
        PendingIntent sendPI=PendingIntent.getBroadcast(mcontext, 0, new Intent(SENT), 0);
        PendingIntent deliveredPI=PendingIntent.getBroadcast(mcontext, 0, new Intent(DELIVERED), 0);
        mcontext.registerReceiver(sendingSms, sendFilter);
        mcontext.registerReceiver(deliveredSms, deliveredFilter);

        Log.d("inside method 2", "getting 2");
        SmsManager sms=SmsManager.getDefault();
        sms.sendTextMessage(num, null, messege, null, null);
        Log.d("inside method 3", "getting 3");
        return result;

    }
    BroadcastReceiver sendingSms=new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(context, "Sms Send", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(context, "Generic Errors", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(context, "No Service", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(context, "Null pdu", Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(context, "Error Radio", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
            }
        }
    };
    BroadcastReceiver deliveredSms=new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(context, "Sms Delivered", Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(context, "Sms Failed", Toast.LENGTH_SHORT).show();
                break;

            default:
                break;
            }
        }
    };
    public void unregisterreciever(){
        mcontext.unregisterReceiver(sendingSms);
        mcontext.unregisterReceiver(deliveredSms);
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        System.out.print("Recieved : "+intent.getAction());
    }

}

Is i need to do anything more like intentfilter rather than declaring receiver in manifest file My manifest file is shown below :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rrm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.BROADCAST_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.rrm.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Mobile"
            android:label="@string/app_name"></activity>
        <activity
            android:name=".Dth"
            android:label="@string/app_name"></activity>
        <activity
            android:name=".Datacard"
            android:label="@string/app_name"></activity>
        <activity
            android:name=".Check"
            android:label="@string/app_name"></activity>
        <activity
            android:name=".Register"
            android:label="@string/app_name"></activity>
        <receiver
            android:name=".SmsHandling"></receiver>
    </application>

</manifest>

and also I am not getting correct idea about Intent filter can anybody explain why it is used in manifest file.. And also help me to solve the above problem

2 Answers2

0

Change this line

sms.sendTextMessage(num, null, messege, null, null);

to

sms.sendTextMessage(num, null, messege, sendPI, deliverdPI);

see the reference for sendTextMessage here

maiatoday
  • 795
  • 2
  • 7
  • 15
0

It turns out that you cannot use getResultCode() to find out if the SMS has really been delivered. You have to parse the pdu. Android can parse it, but you have to interpret the status code. Depending on whether it is GSM or CDMA, the "delivered ok" status code will be 0 or 2.

For more details see: Android SMS delivery notification on failure: false positive

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127