gpt4 book ai didi

Android:以同步方式发送短信

转载 作者:行者123 更新时间:2023-11-29 22:15:49 24 4
gpt4 key购买 nike

我指的是 this有关从 Android 发送短信的文章。

我尝试通过实现循环向 10 个不同的号码发送 10 条短信。例如循环 sendSMS(phoneNo, message);但是在发送 6 条短信后,sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 给出 NullPointerExeption。

仅供引用,我在 deliveredPI 中传递了空值,因为我不关心短信的传递。但是知道是否真的发送了短信非常重要,这可以通过注册广播接收器来检索。

我已经注册了一个广播接收器,但似乎整个过程没有同步。只有在收到前一条短信的状态后,我们才应该发送第二条短信。

我假设放慢发送短信的速度将帮助我删除 NullPointerExeption。等待状态将是在发送另一条短信之间保持时间间隔的最佳主意。

简而言之,我想做 -> 发送一条短信 -> 等待状态 -> 在数据库中更新状态 -> 发送另一条短信。

最佳答案

我个人没有测试我发布的代码,但它被接受为 this other question 的答案所以我认为它会起作用。

    SmsManager smsMan = new SmsManager.getDefault();
ArrayList<String> contactList = new ArrayList();
//add contacts to contactList with contactList.add(string)
for (int i = 0; i <= contactList().size(); i++) {
String SENT = contactList.get(i).toString();// you could replace this with i,
//or something like "sms_sent_myappname" + i.toString());

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT, 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 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));
smsManager.sendTextMessage(contactList.get(i).toString(), null, message, sentPI, null);
}

上面的方式,你会向Android发送一个请求,将每条消息一个接一个地发送出去。如果您想在 Activity.RESULT_OK 之后实际发送下一条 SMS,那么我建议仍然使用 ArrayList 方法,但您可以使用类似:

public void onCreate(Bundle savedInstanceState) {
smsMan = new SmsManager.getDefault(); //assuming you declared SmsManager smsMan in class body.
contactList = new ArrayList(); //assuming you declared ArrayList<String> contactList in class body.
//add contacts to contactList with contactList.add(string);
}
public void sendSms(int position){
//add contacts to contactList with contactList.add(string)
String SENT = contactList.get(position).toString();// you could replace this with i,
//or something like "sms_sent_myappname" + i.toString());

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

//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK
context.unregisterReceiver(this);
i++;
if (contactList.size()<i){
sendSms(i);
} else {
//You are done sending - Do what you want.
}
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));
smsManager.sendTextMessage(contactList.get(position).toString(), null, message, sentPI, null);

}

再次。我还没有测试过,但它应该可以工作。如果您还有其他问题或者我有什么不清楚的地方,请告诉我。

关于Android:以同步方式发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8471175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com