如何获得短信发送的确认为每个联系人/人机器人? [英] How to get sms sent confirmation for each contact/person in android?
问题描述
我想发短信给多人,并确认短信是否发送与否。我查了多条链路(这里提到),并得到了使用 PendingIntent
和广播接收器
进行确认的想法。
I want to send sms to multiple people and verify whether sms sent or not. I checked multiple links (mentioned here) and got the idea of using PendingIntent
and broadCast Receiver
for confirmation.
<一个href="http://stackoverflow.com/questions/9520277/practical-way-to-find-out-if-sms-has-been-sent">Practical办法找出如果短信已发送
<一href="http://stackoverflow.com/questions/8578689/sending-text-messages-programmatically-in-android">Sending短信编程的机器人
http://mobiforge.com/design-development/sms-messaging-android
Practical way to find out if SMS has been sent
Sending text messages programmatically in android
http://mobiforge.com/design-development/sms-messaging-android
但关键的问题是,我有一个的ArrayList
并在另一个各自不同的封邮件的ArrayList $ C $不同的50个联系人号码C>。
But the key problem is that, I have different 50 contacts number in an arrayList
and their different msgs in another arrayList
.
我用这个code:
for (Condition) {
sms = SmsManager.getDefault();
try {
. . . sms.sendTextMessage(phoneNumbers[i], null, messages[i], sentPI, deliveredPI);
}
catch(IllegalArgumentException e) { }
}
现在,我不能确定有多少人做得到的味精和多少不知道。因为在后(上述链接)如图所示,每次我们只得到一个MSG时,短信发送。
Now, I can't identify how many people do get their msg and how many don't. Because as shown in post(mentioned link above), every time we just get one msg, "SMS delivered".
所以,请让我知道,我怎样才能把群众演员,在意图
,当我发送味精和广播接收器获得额外
来获取特定联系人/人的详细信息。
So please let me know, how can I put "extras" in Intent
, when I send msg and get the extras from broadcast Receiver
to get the detail of specific contact/person.
一件事:有在 PendingIntent
四个不同的选项标志值
( FLAG_ONE_SHOT
, FLAG_NO_CREATE
, FLAG_CANCEL_CURRENT
, FLAG_UPDATE_CURRENT
)。
哪一个我应该使用,当我在发送邮件for循环正确的结果?
One More thing : There are four different option for flag value in PendingIntent
(FLAG_ONE_SHOT
, FLAG_NO_CREATE
, FLAG_CANCEL_CURRENT
,FLAG_UPDATE_CURRENT
).
Which one should I use when I send messages in for loop for correct result?
推荐答案
举个例子,这里有一个简单的活动
类,发送短信至pre数字-defined清单:
For an example, here is a simple Activity
class that sends text messages to a pre-defined list of numbers:
public class SmsActivity extends Activity implements View.OnClickListener
{
private static final String SENT = "SMS_SENT";
private static final String DELIVERED = "SMS_DELIVERED";
private static final String EXTRA_NAME = "name";
private static final String EXTRA_NUMBER = "number";
private static final String[] names = new String[] { "Name1", "Name2", "Name3", "Name4" };
private static final String[] numbers = new String[] { "1234567890", "1234567890", "1234567890", "1234567890" };
SmsManager smsMgr;
IntentFilter filter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
((Button) findViewById(R.id.button1)).setOnClickListener(this);
smsMgr = SmsManager.getDefault();
filter = new IntentFilter(SENT);
filter.addAction(DELIVERED);
}
@Override
protected void onResume()
{
super.onResume();
registerReceiver(receiver, filter);
}
@Override
protected void onPause()
{
super.onPause();
unregisterReceiver(receiver);
}
public void onClick(View v)
{
for (int i = 0; i < names.length; i++)
{
sendText(numbers[i], names[i], i);
}
}
private void sendText(String conNumber, String conName, int requestCode)
{
Intent sentIntent = new Intent(SENT);
Intent deliveredIntent = new Intent(DELIVERED);
sentIntent.putExtra(EXTRA_NUMBER, conNumber);
sentIntent.putExtra(EXTRA_NAME, conName);
PendingIntent sentPI = PendingIntent.getBroadcast(this, requestCode, sentIntent, 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, requestCode, deliveredIntent, 0);
smsMgr.sendTextMessage(conNumber, null, "Hello", sentPI, deliveredPI);
}
private BroadcastReceiver receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if (SENT.equals(intent.getAction()))
{
String name = intent.getStringExtra("name");
String number = intent.getStringExtra("number");
switch (getResultCode())
{
case Activity.RESULT_OK:
toastShort("SMS sent to " + name + " & " + number);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
toastShort("Generic failure");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
toastShort("No service");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
toastShort("Null PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
toastShort("Radio off");
break;
}
}
else if (DELIVERED.equals(intent.getAction()))
{
switch (getResultCode())
{
case Activity.RESULT_OK:
toastShort("SMS delivered");
break;
case Activity.RESULT_CANCELED:
toastShort("SMS not delivered");
break;
}
}
}
};
private void toastShort(String msg)
{
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
您需要在您的清单以下权限:
You will need the following permission in your manifest:
<uses-permission android:name="android.permission.SEND_SMS" />
这篇关于如何获得短信发送的确认为每个联系人/人机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!