我的源代码有问题,当我发短信多个numbber时,我只发送了一个ID相同的报告,这是发送的最后一个号码
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI;
PendingIntent deliverPI;
try {
JSONObject json = new JSONObject(content);
status = json.getString("status");
message = json.getString("message");
pesan = json.getString("pesan");
contacts = json.getJSONArray("contacts");
Log.i("INFO","status " + status + ", message " + message + ", pesan "+ pesan + " , jml contacts " + contacts.length());
for (int i = 0; i < contacts.length(); i++) {
JSONObject datauser = contacts.getJSONObject(i);
Log.i("INFO", "no :" + (i + 1) + " " + datauser.getString(NO));
}
Intent sendinten = new Intent(SENT);
Intent deliverIntent = new Intent(DELIVERED);
SmsManager sms = SmsManager.getDefault();
int[] id_recever = new Gson().fromJson(id_message, int[].class);
for (int i = 0; i < id_recever.length; i++) {
JSONObject datauser = contacts.getJSONObject(i);
String id = String.valueOf(id_recever[i]);
Log.i("INFO", "ID message " + id);
deliverIntent.putExtra("id_send", id);
deliverPI = PendingIntent.getBroadcast(getBaseContext(), 0,deliverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
sentPI = PendingIntent.getBroadcast(getBaseContext(), 0,sendinten, PendingIntent.FLAG_UPDATE_CURRENT);
sms.sendTextMessage(datauser.getString(NO), null, pesan,sentPI, deliverPI);
}
} catch (Exception e) {
e.printStackTrace();
Log.d("DATA", "GAGAL");
}
// ----------- SMS Delivert ----------- //
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS send",Toast.LENGTH_SHORT).show();
Log.i("INFO", "SMS SEND");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:// no salah
Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE: // didn't get service
// provider
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: // mode airplane
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
// ----------- SMS Delivert ----------- //
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String id_message_send = intent.getStringExtra("id_send");
switch (getResultCode()) {
case Activity.RESULT_OK:
Log.i("INFO", "SMS Delivered " + id_message_send);
AsyncHttpClient postId = new AsyncHttpClient();
RequestParams id = new RequestParams();
id.put("id_message_send", id_message_send);
postId.post(URL_UPDATE, id, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
public void onSuccess(int arg0, String response) {
Log.i("INFO", "Response Server " + response);
super.onSuccess(arg0, response);
}
});
break;
case Activity.RESULT_CANCELED:
Log.i("INFO", "SMS NOT Deliver");
break;
}
}
}, new IntentFilter(DELIVERED));
}
答案 0 :(得分:0)
您需要确保PendingIntent
都是唯一的。由于您始终在传递给Intent
的{{1}}中提供相同的操作,因此您将始终获得相同的createBroadcast()
。
每次拨打PendingIntent
时,请尝试将requestCode
参数设置为唯一值(假设您的createBroadcast()
参数是唯一的):
id