Java android,如何知道短信是否成功发送?

时间:2013-01-29 12:23:29

标签: android

如何知道短信是否成功发送?

我已经检查了文档,但是我找不到解决问题的方法...... 我怎么知道?

//---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent",Toast.LENGTH_SHORT).show();
                        getServerData("http://site.com","id",Id);

                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        System.exit(0);
                        Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        System.exit(0);
                        Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.STATUS_ON_SIM_UNSENT:
                        System.exit(0);
                        Toast.makeText(getBaseContext(), "Message unsent", Toast.LENGTH_SHORT).show();
                        break;

                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        System.exit(0);
                        Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        System.exit(0);
                        Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.STATUS_ON_SIM_SENT :
                        System.exit(0);
                        Toast.makeText(getBaseContext(), "Message sent", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        //Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                       // Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                        break;                      
                }
            }
        }, new IntentFilter(DELIVERED));        





        if(isMobileAvailable(getApplicationContext()) == true){
            Toast.makeText(getApplicationContext(), "Reseau mobile: OK !", Toast.LENGTH_LONG).show();

             ArrayList<String> parts = (ArrayList<String>) splitInChunks(message, 120);
               for (String str : parts) {
                   SmsManager sms = SmsManager.getDefault();
                   sms.sendTextMessage(phoneNumber, null, str, null, null);
              }
               getServerData("http://site.com","id",Id);

        }else
            Toast.makeText(getApplicationContext(), "Reseau mobile: NOK !", Toast.LENGTH_LONG).show();
    }

我想知道我的短信是否成功发送... 打电话给你!

2 个答案:

答案 0 :(得分:2)

我认为你应该尝试这个内容观察代码..它会密切关注msgs的天气内容是否已经改变..

private class MyContentObserver extends ContentObserver {
    ContentValues values = new ContentValues();
    int threadId;

    public MyContentObserver() {
        super(null);
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Uri uriSMSURI = Uri.parse("content://sms");
        Cursor cur = getBaseContext().getContentResolver().query(uriSMSURI, null, null, null, null);
        cur.moveToNext();
        String content = cur.getString(cur.getColumnIndex("body"));
        String contentid = cur.getString(cur.getColumnIndex("_id"));

        Log.v(TAG, "content: " + content+"id:"+contentid);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return false;
    }
}

答案 1 :(得分:1)

添加到@Aexin

Inbox = "content://sms/inbox"
Failed = "content://sms/failed"
Queued = "content://sms/queued"
Sent = "content://sms/sent"
Draft = "content://sms/draft"
Outbox = "content://sms/outbox"
Undelivered = "content://sms/undelivered"
All = "content://sms/all"
Conversations = "content://sms/conversations"

数据库字段

0: _id
1: thread_id
2: address
3: person
4: date
5: protocol
6: read   
7: status
8: type
9: reply_path_present
10: subject
11: body
12: service_center
13: locked

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/provider/Telephony.java

@Shuty

http://developer.android.com/reference/android/telephony/SmsManager.html#sendTextMessage(java.lang.String, java.lang.String, java.lang.String, android.app.PendingIntent, android.app.PendingIntent)

这里显然说如果你必须做回调,你必须提供PendingIntent,这将被广泛播放。

正如您在代码中看到的那样: sms.sendTextMessage(phoneNumber, null, str, null, null);

http://developer.android.com/reference/android/app/PendingIntent.html

尝试什么

Intent intent = new Intent();

pi = PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags);

sms.sendTextMessage(phoneNumber, null, str, pi, null);

sms.sendTextMessage(phoneNumber, null, str, pi, pi);

反正

android.provider.Telephony.SMS_RECEIVED chceck如果你有正确的(区分大小写)并且也试图理解这一点并非每个广播都可以注册Programaticaly,反之亦然。

<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>