如何获取消息的发送和送达通知

问题描述 投票:0回答:2

嗨,我正在开发一个完全基于短信的应用程序。我可以成功地将消息发送到所需的号码,但我的问题是,即使我使用代码通过 Intentfilter 和挂起的意图获取通知,我也没有收到发送和传递的通知。 发送短信和接收通知的代码是在扩展广播接收器的类 (SmsHandling.java) 内完成的。我的程序代码在这里..

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());
    }

}

我是否需要做更多类似intentfilter的事情,而不是在清单文件中声明接收器我的清单文件如下所示:

<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>

而且我对意图过滤器没有正确的了解,任何人都可以解释为什么它在清单文件中使用。 也帮我解决上面的问题

android
2个回答
0
投票

事实证明,你无法使用 getResultCode() 来查明短信是否真的已发送。你必须解析pdu。 Android 可以解析它,但你必须解释状态码。根据是 GSM 还是 CDMA,“已发送”状态代码将为 0 或 2。

更多详情请参阅: Android 短信发送失败通知:误报


0
投票

更改此行

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

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

请参阅此处 sendTextMessage 的参考

© www.soinside.com 2019 - 2024. All rights reserved.