2

SMSの送受信結果アプリケーションをテストしていました。期待どおりに SMS を送信し、「SMS Sent」とトーストします。しかし、「SMSが配信されました」と乾杯することはありません。また、決して到達することはありません

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>
4

1 に答える 1

0

メソッドにレシーバー (smsSendBroadcast & smsDeliveryBroadcast)onCreate()を登録し、 でレシーバーを登録解除する必要があると思いますonDestroy()

私はここで似たようなことをしています!!! Android PendingIntent エクストラ、BroadcastReceiver で受信されない

于 2012-10-29T06:08:29.547 に答える