【解決方法】確認付きSMSを送信する


やあ

先生、私は Android プラットフォームを初めて使用しており、確認コードを含む SMS を送信するための別のクラスを作成しましたが、これを介して SMS を送信すると、ヌル ポインター例外によりアプリケーションが予期せず終了するという問題に直面します。実際には、SMS を送信しました。ここで定義されている send_SMS メソッドを介して、SettingScreen アクティビティから、この点に関してそのコードをクエリにバインドするのを手伝ってください。

ありがとう
オム・プラカシュ・カウシク

Java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import android.app.PendingIntent;
import android.telephony.SmsManager;
import android.widget.Toast;
import android.app.Activity;
public class SendSMS extends Activity {
	private Context ctx;
	public SendSMS(Context object){
		this.ctx = object;
		}
	  //---sends an SMS message to another device---
	public void send_SMS(String phoneNumber, String message)
    { 
		try{
        String SENT = "SMS_SENT";
       // String DELIVERED = "SMS_DELIVERED";
 
       PendingIntent sentPI = PendingIntent.getBroadcast(ctx.getApplicationContext(), 0, new Intent(SENT),0);
    		   //getBroadcast( ctx, 0, new Intent(ctx,ctx.getClass()), 0);
      // PendingIntent deliveredPI = PendingIntent.getBroadcast( ctx, 0, new Intent(DELIVERED), 0);
 
       /// ---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                case Activity.RESULT_OK:
                    	Toast.makeText(getBaseContext(), "SMS_SNT", 
                                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, null);        
  
    }catch(Exception e){
    	Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }
    }
	

}

解決策 1

次の行を変更します…

catch(例外 e)

キャッチ(投げられるe)

これを実行すると、例外が発生します (そこにあるコードが機能すると仮定します)。

解決策 3

SMS 確認が送信されました

コメント

タイトルとURLをコピーしました