0

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

我检查了文档,但没有找到解决问题的方法...我怎么知道?

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

我只是想知道我的短信是否成功发送...谢谢你!

4

2 回答 2

2

我认为你应该试试这个内容观察者代码..它密切关注味精的天气内容是否发生了变化..

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;
    }
}
于 2013-01-29T16:58:19.553 回答
2

添加到@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

@舒蒂

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。

正如您在代码中看到的: 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检查您是否正确(区分大小写)并尝试理解这一点,并非每个广播都可以编程注册,反之亦然。

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

于 2013-01-29T17:20:08.197 回答