2011-01-23 2 views
-1

SMSメッセージを1つずつ送信しようとしています。私は1つを送り、それが次のものを送る前に届けられるのを待っています。誰かが a)は、送信メッセージに配信確認を要求関与し得る一度に1つずつSMSメッセージを送信しようとしているとき

package com.smith.johnathan.jssms; 

//import android.telephony.SmsManager; 
import android.telephony.gsm.SmsManager; 
import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.gsm.SmsManager; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import android.util.Log; 
import android.widget.Button; 

import java.io.*; 
import android.util.LogPrinter; 

import java.io.*; 

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.widget.TextView; 
import android.os.*; 

import android.app.Activity; 

import android.os.Bundle; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.Toast; 
import java.io.*; 

import android.app.Activity; 

import android.os.Bundle; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.Toast; 

public class JSSMS extends Activity { 

    boolean sendingSMS = false; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button button = (Button) findViewById(R.id.Button01); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Toast.makeText(JSSMS.this, "Starting SMS", Toast.LENGTH_LONG) 
         .show(); 

       String message = "This is Johnathan and this is my new number"; 
       String number; 
       try { 

        BufferedReader buffreader = new BufferedReader(
          new FileReader(Environment 
            .getExternalStorageDirectory().toString() 
            + "/numbers.txt")); 

        int i = 0; 
        while ((number = buffreader.readLine()) != null) { 

         Toast.makeText(JSSMS.this, "Sending text to:" + number, 
           Toast.LENGTH_SHORT).show(); 
         if(sendingSMS) 
         { 
          wait(100); 
         } 
         sendingSMS = true; 
         sendSMS(number, message); 

        } 

        buffreader.close(); 
       } catch (java.io.FileNotFoundException e) { 
        Toast.makeText(JSSMS.this, e.toString(), Toast.LENGTH_SHORT) 
          .show(); 

       } catch (Exception e) { 
        Toast.makeText(JSSMS.this, e.toString(), Toast.LENGTH_SHORT) 
          .show(); 
       } 

       Toast.makeText(JSSMS.this, "DONE!!", Toast.LENGTH_LONG).show(); 

      } 

     }); 

    } 

    // ---sends an SMS message to another device--- 
    private void sendSMS(String phoneNumber, String message) { 
     String SENT = "SMS_SENT"; 
     String DELIVERED = "SMS_DELIVERED"; 

     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 arg0, Intent arg1) { 
       sendingSMS = false; 
       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)); 

     // ---when the SMS has been delivered--- 
     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(phoneNumber, null, message, sentPI, deliveredPI); 
    } 

} 

答えて

0

一つの技術を私を助けてくださいすることができ (これは受信機の登録) b)のアンドロイドAPI上で行う必要があるかは明らかではありません領収書を聞きます c)メッセージの送信を続ける前に、スレッドを使用して領収書を待機してください。 http://developer.android.com/resources/articles/painless-threading.html

+0

SMSメッセージは消えて忘れていませんか?私は送っているハンドセットがメッセージがネットワークにあるという応答を得ると思った。ネットワークがそれを実際に受信者に配信したかどうかはわかりません。 – whitey04

+0

これはほとんど正しいです。しかし、[このウェブサイト](http://www.developershome.com/sms/sms_tutorial.asp?page=basicConcepts)は、テキストメッセージ自体にフラグを設定して、宛先の電話がメッセージを受信します。 – Gary