gpt4 book ai didi

android - Toast 消息指定消息发送到的号码

转载 作者:行者123 更新时间:2023-11-30 03:24:03 25 4
gpt4 key购买 nike

所以我几乎完成了我的应用程序的 SMS 功能。它需要做的最后一件事是显示一个 Toast 消息发送到的号码。我有以下代码:

全局变量:

private BroadcastReceiver sendBroadcastReceiver;
private BroadcastReceiver deliveryBroadcastReceiver;

String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";

在 void onCreate(Bundle savedInstanceState) 上声明:

sendBroadcastReceiver = new BroadcastReceiver()
{

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;
}
}
};

deliveryBroadcastReceiver = new BroadcastReceiver()
{
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;
}
}
};
registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT));

发送短信功能:

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);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

将联系人(存储在首选项中)调用到方法调用:

ArrayList<String> contacts = new ArrayList();

List<String> list = SM.getAllValue();
for(String string : list){
contacts.add(string);
}
for(int i = 0; i < contacts.size(); i++){
String numberToSend = contacts.get(i).toString();
sendSMS(numberToSend, sms);
}

其中 SM.getAllValue() 返回用户联系人的列表

最后onStop()

@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReceiver);
super.onStop();

}

在向多个号码发送消息时,以下代码一遍又一遍地向我提示 SMS Sent toast。我现在如何在 toast 中指定数字,例如 SMS Sent to Number1SMS sent to Number2 等,或者统称为 All the SMS's were delivered。请帮助:)

最佳答案

在发送的广播 Intent (new Intent(SENT))中,您可以添加您发送它的号码:

Intent intent = new Intent(SENT);
intent.putExtra("number", phoneNumber);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, intent, 0);

然后在您的接收器中,您可以使用该号码:

public void onReceive(Context arg0, Intent arg1) {
String phoneNumber = arg1.getStringExtra("number");
// Use it in the toast
}

关于android - Toast 消息指定消息发送到的号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560210/

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