SMS DeliveryBroadcast(BroadCastReceiver)永远不会调用OnReceive

时间:2012-07-12 13:12:20

标签: android sms broadcastreceiver

我正在测试一些短信发送和接收结果应用。它按预期发送短信,并播放"短信发送"。但它永远不会祝酒"短信发送"。它也永远不会到达

Log.i("BROADCASTRECEIVER", String.valueOf(getResultCode())); 

这意味着永远不会为SMS_DELIVERED结果调用OnReceive ...任何想法为什么?...

public class C extends Activity {

    static String[] params;

    static Context context;
    ProgressDialog pd;
    BroadcastReceiver smsDeliveryBroadcast, smsSendBroadcast;
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c);
        params = new String[3];
        params[0] = "00905067161660";
        params[1] = "smsC.";
        params[2] = "smsC..";
        new ServerSMSThread().execute(params);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        unregisterReceiver(smsDeliveryBroadcast);
        unregisterReceiver(smsSendBroadcast);
        super.onPause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub

        super.onResume();
    }

    public void sendSMS(String phoneNumber, String message) {
        Log.i("NO", phoneNumber);
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

        registerSMS();

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

    }

    private void registerSMS() {
        smsSendBroadcast = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {


                // TODO Auto-generated method stub
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT)
                            .show();
                    Log.i("MONITOR", "SMS Gonderildi");
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(context,
                            "SMS sending failed: Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(context, "SMS sending failed: No service",
                            Toast.LENGTH_SHORT).show();
                    Log.i("MONITOR", "SMS sending failed: No service");
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(context, "SMS sending failed: Null PDU",
                            Toast.LENGTH_SHORT).show();
                    Log.i("MONITOR", "SMS sending failed: Null PDU");
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(context, "SMS sending failed: Radio off",
                            Toast.LENGTH_SHORT).show();
                    Log.i("MONITOR", "SMS sending failed: Radio off");
                    break;
                }
            }
        };
        smsDeliveryBroadcast = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                Log.i("BROADCASTRECEIVER", String.valueOf(getResultCode()));
                // TODO Auto-generated method stub
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT)
                            .show();
                    Log.i("MONITOR", "SMS Teslim edildi");
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(context, "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    Log.i("MONITOR", "SMS not delivered");
                    break;

                default:
                    Toast.makeText(context, "SMS result bilinmiyor",
                            Toast.LENGTH_SHORT).show();
                    Log.i("MONITOR", "SMS result bilinmiyor");
                    break;
                }
            }
        };

        // ---when the SMS has been sent---
        registerReceiver(smsSendBroadcast, new IntentFilter(SENT));
        // ---when the SMS has been delivered---
        registerReceiver(smsDeliveryBroadcast, new IntentFilter(DELIVERED));
    }



    public class ServerSMSThread extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (pd.isShowing())
                pd.dismiss();
            startActivity(new Intent(C.this,B.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pd = ProgressDialog.show(C.this, "Sending SMS...", "Please wait.");
            Log.i("MONITOR", "SMS Gonderiliyor");
        }

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            for (int i = 1; i < params.length; i++) {
                sendSMS(params[0], params[i]);
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    }

}

的AndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="11" />
    <uses-permission android:name="android.permission.SEND_SMS" ></uses-permission>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ActivityOrderActivity"
            android:label="@string/app_name">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".B" ></activity>
         <activity android:name=".C"></activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

我认为您应该尝试使用onCreate()方法注册接收方(smsSendBroadcast&amp; smsDeliveryBroadcast),并在onDestroy()中取消注册接收方。

我正在做类似的事情! Android PendingIntent extras, not received by BroadcastReceiver