私はアンドロイドSMSアプリケーションに取り組んでいます。SMSを送信するために使用した次のコード。
public void sendsms(){
sendBroadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
System.out.println("hereeeeeeeeeeeeeeeeeeeeeeeeeeeeee8888888888888888");
System.out.println("1111111111111111111111111111111111");
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
ContentValues values = new ContentValues();
values.put("address", phoneNumber);
values.put("body", message);
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
afterSend();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
System.out.println("222222222222222222222222222");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
System.out.println("333333333333333333333333333333333");
ContentValues values1 = new ContentValues();
values1.put("address", phoneNumber);
values1.put("body", message);
getContentResolver().insert(Uri.parse("content://sms/queued"), values1);
afterSend();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
System.out.println("44444444444444444444444444");
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
System.out.println("4444444444444444444444444444444444");
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT));
//--- when the SMS hasbeen delivered---
deliveryBroadcastReciever=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode())
{
case Activity.RESULT_OK:
System.out.println("okkkkkkkkkkkkkkkkkkkkkkkkkkk");
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
System.out.println("cancellllllllllllllllllll");
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReciever);
}
sentsms
今私の問題は、 SMSを送信しonreceive()
た場合です。登録解除の呼び出しを書かないと、2 番目の SMS を送信するときに、onreceive()
最初と 2 番目の SMS の両方がリッスンされます。
したがって、完了後に登録解除の呼び出しを行って、この問題を解決してくださいonreceive()
。