1

SMSステータスを確認するためにbroadcastreceiverを使用している間、SMSが送信されたときにトーストが表示されますが、SMSが送信または配信されていないときは何も表示されません(突然の番号を入力してテストしています)。私が使用しているコードは、SMS 配信ステータスをチェックするすべてのサイトで最も多く見たものです。しかし、私のコードは、SMS が正常に送信されたときのステータスのみを表示しています。誰でも私が間違っていることのヒントを得ることができますか? 私はこのメソッドを doInBackground() に持っているので、明らかに AsyncTask を使用しています。

public void send_SMS(String list, String msg, AtaskClass task)
{
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    SmsManager sms = SmsManager.getDefault();

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT), 0);

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

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show();
                    break;

                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(context, "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(context, "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(context, "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent arg1) {

              switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(context, "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

     StringTokenizer st = new StringTokenizer(list,",");    
        int count= st.countTokens();
        int i =1;
        count = 1;

            while(st.hasMoreElements())

            {

                //  PendingIntent pi = PendingIntent.getActivity(this,0,new Intent(this, SMS.class),0);

                    String tempMobileNumber = (String)st.nextElement();

                    //SmsManager sms = SmsManager.getDefault();

                    sms.sendTextMessage(tempMobileNumber, null, msg , sentPI, deliveredPI); 

                      Double cCom = ((double)i/count) * 100; 


                    int j = cCom.intValue();

              task.doProgress(j);
              i++;
              count ++;
               }

     // class ends
    }
4

0 に答える 0