短信发送失败问题

时间:2014-08-06 14:24:43

标签: android sms android-2.3-gingerbread

我只想澄清一些事情。

我正在编写一个简单的代码,在看到它将使用auto_sms回复回复时,标准会侦听传入设备的短信。我在运行Android 4.0 +的设备上尝试过它们似乎没有任何问题,但我一直在运行Android 2.3.4和2.3.6的两个设备上进行测试,两者都在代码时给我以下错误正在尝试发送auto_sms回复。

我认为这取决于导致问题的设备上的Android版本,是否有使代码适用于旧版本的Android?

07-17 03:29:43.710: D/SMSDispatcher(246): mResultReceiver : success = true
07-17 03:29:44.370: D/SMSDispatcher(246): SMS send failed

以下是我用于 Location_SMS_Request类的代码。

package com.example.mm_parentapp_sms_commands;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

public class Location_SMS_Request_1 extends Activity {

    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    PendingIntent sentPI, deliveredPI;
    BroadcastReceiver smsSentReceiver, smsDeliveredReceiver;
    ContentResolver contentResolver;
    ContentObserver smsContentObserver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
        deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
        contentResolver = getContentResolver();
    }

    @Override
    public void onResume() {
        super.onResume();

        //---create the BroadcastReceiver when the SMS is sent---
        smsSentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio Off", Toast.LENGTH_LONG).show();
                    break;
                }
            }
        };

        smsDeliveredReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS Delivered", Toast.LENGTH_LONG).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
                default:
                    break;
                }
            }
        };

        //---register the two BroadcastReceivers---
        registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));

        //---SEND A SMS MESSAGE---
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage("Response Number Here", null, "Locate", sentPI, deliveredPI);

        smsContentObserver = new ContentObserver(new Handler()) {
            @Override
            public void onChange(boolean selfChange) {
                Uri smsURI = Uri.parse("content://sms/sent");
                Cursor c = getContentResolver().query(smsURI, new String[]{"address", "date", "body", "type"}, null, null, null);

                String[] columns = new String[] {
                        "address", "date", "body", "type"
                };

                //---go to the first rown; which is the most recently 
                // sent message---
                c.moveToNext();

                //---get the various properties of the SMS message---
                String recipient = c.getString(c.getColumnIndex(columns[0]));
                String date = c.getString(c.getColumnIndex(columns[1]));
                String message = c.getString(c.getColumnIndex(columns[2]));
                String type = c.getString(c.getColumnIndex(columns[3]));

                //---print out the details of the message---
                Log.d("DetectOutgoingSMS", recipient + ", " + date + ", " +
                        message + ", " + type);
            }

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

        contentResolver.registerContentObserver(Uri.parse("content://sms"), true, smsContentObserver);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        contentResolver.unregisterContentObserver(smsContentObserver);
        //---unregister the two BroadcastReceivers---
        unregisterReceiver(smsSentReceiver);
        unregisterReceiver(smsDeliveredReceiver);

        finish();
    }
}

0 个答案:

没有答案