无法获得短信发送

时间:2014-10-24 13:23:45

标签: android sms

我正在使用此代码发送短信。此代码发送短信,但无法送达:

public class SMSSend {

    public static final String ACTION_SMS_SENT = "com.test.client.SMS_SENT";
    public static final String ACTION_SMS_DELIVERED = "com.test.client.SMS_DELIVERED";
    public Context context;
    void sendSMS(String number, String msg)
    {
        SmsManager sm = SmsManager.getDefault();
        ArrayList<String> parts = sm.divideMessage(msg);

        Intent iSent = new Intent(ACTION_SMS_SENT);
        PendingIntent piSent = PendingIntent.getBroadcast(context, 0, iSent, 0);
        Intent iDel = new Intent(ACTION_SMS_DELIVERED);
        iDel.putExtra("addressee", number);
        PendingIntent piDel = PendingIntent.getBroadcast(context, 0, iDel, 0);

        if (parts.size() == 1)
        {
            msg = parts.get(0);
            sm.sendTextMessage(number, null, msg, piSent, piDel);
        }
        else
        {
            ArrayList<PendingIntent> sentPis = new ArrayList<PendingIntent>();
            ArrayList<PendingIntent> delPis = new ArrayList<PendingIntent>();

            int ct = parts.size();
            for (int i = 0; i < ct; i++)
            {
                sentPis.add(i, piSent);
                delPis.add(i, piDel);
            }

            sm.sendMultipartTextMessage(number, null, parts, sentPis, delPis);
        }
    }
}

我使用此广播接收器进行传送,但它无法正常工作:

public class deliverReceiver  extends BroadcastReceiver
{

    public static final String ACTION_SMS_SENT = "com.test.client.SMS_SENT";
    public static final String ACTION_SMS_DELIVERED = "com.test.client.SMS_DELIVERED";

    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();

        if (action.equals(ACTION_SMS_SENT))
        {
            switch (getResultCode())
            {
                case -1: //Activity.RESULT_OK
                    MyToast.Show(context,"sent");
                    break;

                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    break;

                case SmsManager.RESULT_ERROR_NULL_PDU:
                    break;

                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    break;

                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    break;

                default:

            }
        }
        else if (action.equals(ACTION_SMS_DELIVERED))
        {
            String number = intent.getStringExtra("addressee");
            switch (getResultCode())
            {
                case -1: //Activity.RESULT_OK
                   MyToast.Show(context,number);
                    break;

                case 0: //Activity.RESULT_CANCELED
                    MyToast.Show(context,number);
                    break;

                default:
            }
        }
    }
}

我在清单中设置了所有需要的权限,也注册了广播接收器类。          

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
                android:name="com.test.client.registerActivity2"
                android:label="@string/app_name"
                android:screenOrientation="portrait">
         </activity>
        <activity
                android:name="com.test.client.registerActivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait">
           </activity>
        <activity
              android:name="com.test.client.MainActivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".deliverReceiver"
                  android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>

    </application>

我的错误在哪里?请帮帮我。

0 个答案:

没有答案