android应用程序发送消息并获取发送报告不起作用

时间:2014-01-07 14:42:10

标签: android sms broadcastreceiver

我正在创建使用广播接收器发送消息并返回发送报告的Android应用程序。 应用程序发送消息,但我没有收到任何发送报告通知我消息已经发送。

任何人都可以帮我解决这个问题

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.devleb.smssenddemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.devleb.smssenddemo.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>

        <receiver android:name=".SMS" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" >
                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

MainActivity.java

package com.devleb.smssenddemo;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    EditText edit_txt_phone;
    EditText edit_txt_SMS;
    Button btn;

    // private static final String SMS_DELIVERED = "SMS_DELIVERED";
    BroadcastReceiver smsDelivered = new SMSdelivered();

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

        edit_txt_phone = (EditText) findViewById(R.id.editText1);

        edit_txt_SMS = (EditText) findViewById(R.id.editText2);

        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                String strphone = edit_txt_phone.getText().toString();
                String strSMS = edit_txt_SMS.getText().toString();

                if (strphone.length() > 0 && strSMS.length() > 0) {
                    sendSMS(strphone, strSMS);
                } else {
                    Toast.makeText(getBaseContext(),
                            "plz enter the phone or the sms",
                            Toast.LENGTH_SHORT).show();
                }

            }

            private void sendSMS(String phoneNo, String SMS) {
                // TODO Auto-generated method stub

                String SENT = "SMS_SENT";
                String DELIVERED = "SMS_DELIVERED";

                PendingIntent pi = PendingIntent.getBroadcast(getBaseContext(),
                        0, new Intent(SENT), 0);

                PendingIntent piDelevered = PendingIntent.getBroadcast(
                        getBaseContext(), 0, new Intent(DELIVERED), 0);

                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();
                            break;
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            Toast.makeText(getBaseContext(), "Generic failure",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_NO_SERVICE:
                            Toast.makeText(getBaseContext(), "No service",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_NULL_PDU:
                            Toast.makeText(getBaseContext(), "Null PDU",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_RADIO_OFF:
                            Toast.makeText(getBaseContext(), "Radio off",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        }
                    }
                }, new IntentFilter(SENT));
                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));
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phoneNo, null, SMS, pi, piDelevered);

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

SMS.java

package com.devleb.smssenddemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

public class SMS extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";

        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];

            for (int i = 0; i < pdus.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                str += "SMS from" + msgs[i].getOriginatingAddress();
                str += " :";
                str += msgs[i].getMessageBody().toString();
                str += "\n";

            }
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }
    }

}

1 个答案:

答案 0 :(得分:1)

以下是Manikandan SMS Delivery Report in Android的一个有用链接,其中他帮助另一个人解决投递报告问题。