发送短信存储在一个TextView多个联系人 [英] Sending SMS to multiple contacts stored in a textview
问题描述
在这里,我已经创建了一个TextView,我存储在TextView的多个联系人,然后我走过意图的另一个activity.Now我想发短信给它们存储在多个联系人接收到的TextView通过这个TextView的值。 。而我已经通过以下codeS below..But问题是这样做,我可以只发送到存储在textview..So的第一个号码请推荐一些另类codeS将其发送给所有中存储的联系人。
下面是我的code ..
捆绑包= intent2.getExtras();
最后弦乐getudisp = bundle.getString(InvisibleNum);
串号= getudisp;
sendSMS(号码,消息);
私人无效sendSMS(串号,串消息){
意图sentIntent =新的意图(INTENT_ACTION_SENT);
PendingIntent pendingSentIntent = PendingIntent.getBroadcast(这一点,
REQUEST_ code_ACTION_SENT,sentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
意图deliveryIntent =新的意图(INTENT_ACTION_DELIVERY);
PendingIntent pendingDeliveryIntent = PendingIntent.getBroadcast(这一点,
REQUEST_ code_ACTION_DELIVERY,deliveryIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
SmsManager的SmsManager的= SmsManager.getDefault();
//第二个参数是服务中心号码。如果你想使用空
//使用默认号码
smsManager.sendTextMessage(数字,空,消息,pendingSentIntent,
pendingDeliveryIntent);
}
@覆盖
保护无效的onPause(){
super.onPause();
unregisterReceiver(smsSentDeliveredReceiver);
}
@覆盖
保护无效onResume(){
super.onResume();
IntentFilter的过滤器=新的IntentFilter(INTENT_ACTION_SENT);
filter.addAction(INTENT_ACTION_DELIVERY);
registerReceiver(smsSentDeliveredReceiver,过滤器);
}
私人无效initializeReceivers(){
smsSentDeliveredReceiver =新的BroadcastReceiver(){
公共无效onReceive1(上下文的背景下,意图意图){
processBroadcasts(意向);
}
@覆盖
公共无效的onReceive(背景为arg0,意图ARG1){
// TODO自动生成方法存根
}
};
}
私人无效processBroadcasts(意向意图){
串动= intent.getAction();
Log.i(TAG,收稿日期:+动作);
如果(action.equals(INTENT_ACTION_SENT)){
捆绑包= intent.getExtras();
//需要检查错误信息
Log.i(TAG,消息:发送);
Toast.makeText(这一点,消息派,Toast.LENGTH_LONG).show();
}否则,如果(action.equals(INTENT_ACTION_DELIVERY)){
捆绑包= intent.getExtras();
设置<字符串>键= bundle.keySet();
//需要检查错误信息
Log.i(TAG,消息:交货);
Toast.makeText(这一点,消息传递,Toast.LENGTH_LONG).show();
}
}
创建一个类的 MultipleSMS 这样的。
私人无效MultipleSMS(字符串phoneNumber的,字符串消息){
字符串SENT =SMS_SENT;
字符串DELIVERED =SMS_DELIVERED;
PendingIntent sentPI = PendingIntent.getBroadcast(此,0,新的意向(
SENT),0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(此,0,
新的意图(交付),0);
// ---当短信已发送---
registerReceiver(新BroadcastReceiver的(){
@覆盖
公共无效的onReceive(背景为arg0,意图ARG1){
开关(的getResult code()){
案例Activity.RESULT_OK:
ContentValues值=新ContentValues();
的for(int i = 0; I< MobNumber.size() - 1;我++){
values.put(地址,MobNumber.get(ⅰ)的ToString());
// txtPhoneNo.getText()的toString());
values.put(身体,MessageText.getText()的toString());
}
getContentResolver()。插入(
Uri.parse(内容://短信/派),价值);
Toast.makeText(getBaseContext(),短信发送
Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(),一般故障,
Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(),无服务,
Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(),空的PDU,
Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(),无线电关
Toast.LENGTH_SHORT).show();
打破;
}
}
},新的IntentFilter(发送));
// ---当SMS已交付---
registerReceiver(新BroadcastReceiver的(){
@覆盖
公共无效的onReceive(背景为arg0,意图ARG1){
开关(的getResult code()){
案例Activity.RESULT_OK:
Toast.makeText(getBaseContext(),SMS递送,
Toast.LENGTH_SHORT).show();
打破;
案例Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(),短信未交付,
Toast.LENGTH_SHORT).show();
打破;
}
}
},新的IntentFilter(交付));
SmsManager的短信= SmsManager.getDefault();
sms.sendTextMessage(phoneNumber的,空,消息,sentPI,deliveredPI);
}
现在叫 MultipleSMS 是这样的:
下面的 MobNumber 是包含在 phoneNumbers一个Array
的for(int i = 0; I< MobNumber.size();我++)
{
字符串消息= MessageText.getText()的toString()。
串tempMobileNumber = MobNumber.get(ⅰ)的ToString();
MultipleSMS(tempMobileNumber,消息);
}
Here ,I have created a textview and I stored multiple contacts in that textview and then I have passed this textview values through intent to an another activity.Now I want to send Sms to multiple contacts which are stored in that received textview..And i have done this by using the following codes below..But the problem is that I could send only to the first number stored in that textview..So Please suggest some alternative codes to send it to all the stored contacts .
Here is my code..
Bundle bundle=intent2.getExtras();
final String getudisp=bundle.getString("InvisibleNum");
String number =getudisp;
sendSMS(number, message);
private void sendSMS(String number, String message) {
Intent sentIntent = new Intent(INTENT_ACTION_SENT);
PendingIntent pendingSentIntent = PendingIntent.getBroadcast(this,
REQUEST_CODE_ACTION_SENT, sentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent deliveryIntent = new Intent(INTENT_ACTION_DELIVERY);
PendingIntent pendingDeliveryIntent = PendingIntent.getBroadcast(this,
REQUEST_CODE_ACTION_DELIVERY, deliveryIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
SmsManager smsManager = SmsManager.getDefault();
// Second parameter is the service center number. Use null if you want
// to use the default number
smsManager.sendTextMessage(number, null, message, pendingSentIntent,
pendingDeliveryIntent);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(smsSentDeliveredReceiver);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter(INTENT_ACTION_SENT);
filter.addAction(INTENT_ACTION_DELIVERY);
registerReceiver(smsSentDeliveredReceiver, filter);
}
private void initializeReceivers() {
smsSentDeliveredReceiver = new BroadcastReceiver() {
public void onReceive1(Context context, Intent intent) {
processBroadcasts(intent);
}
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
}
};
}
private void processBroadcasts(Intent intent) {
String action = intent.getAction();
Log.i(TAG, "Received: " + action);
if (action.equals(INTENT_ACTION_SENT)) {
Bundle bundle = intent.getExtras();
// Need to check for error messages
Log.i(TAG, "Message: Sent");
Toast.makeText(this, "Message sent", Toast.LENGTH_LONG).show();
} else if (action.equals(INTENT_ACTION_DELIVERY)) {
Bundle bundle = intent.getExtras();
Set<String> keys = bundle.keySet();
// Need to check for error messages
Log.i(TAG, "Message: Delivered");
Toast.makeText(this, "Message delivered", Toast.LENGTH_LONG).show();
}
}
Create a class MultipleSMS this way.
private void MultipleSMS(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);
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
ContentValues values = new ContentValues();
for (int i = 0; i < MobNumber.size() - 1; i++) {
values.put("address", MobNumber.get(i).toString());
// txtPhoneNo.getText().toString());
values.put("body", MessageText.getText().toString());
}
getContentResolver().insert(
Uri.parse("content://sms/sent"), values);
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));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
@Override
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;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
Now call MultipleSMS this way:
Here MobNumber is an Array containing the phoneNumbers
for (int i = 0; i < MobNumber.size(); i++)
{
String message = MessageText.getText().toString();
String tempMobileNumber = MobNumber.get(i).toString();
MultipleSMS(tempMobileNumber, message);
}
这篇关于发送短信存储在一个TextView多个联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!