发送短信到多用户 [英] Send SMS to multi users

查看:152
本文介绍了发送短信到多用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我已经创建了一个编辑文本视图和三个按钮。当用户点击添加更多联系人按钮,它会添加其他编辑文本视图,当用户点击添加联系人,这将带他到电话簿中的联系人在那里他可以选择的联系人,他要发送短信。我写的程序对于这一切,但我不知道如何自动发送消息给所有用户,而无需用户确认时,他只是pressed发送消息按钮。我想SmsManager的,但其德preciated.Please给我建议。

 公共类MainActivity延伸活动{
    私人按钮btn_cntct;
    公众诠释请求code = 1;
    私人的LinearLayout layoutLinear;
    私人按钮btn_addmore_cntct;
    私人诠释ID = 1;

    按钮b_alert;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        //定义按钮元素从电话簿采摘接触
        btn_cntct =(按钮)findViewById(R.id.bpickperson);
        btn_cntct.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(查看为arg0){
                // TODO自动生成方法存根
                //使用意图获取从电话簿联系人
                意向意图=新的意图(Intent.ACTION_GET_CONTENT);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                startActivityForResult(意图,询价code);
            }

        });
        //定义添加辑阵编辑,文本按钮和编辑文本值
        //意见
        layoutLinear =(的LinearLayout)findViewById(R.id.mLayout);
        btn_addmore_cntct =(按钮)findViewById(R.id.baddmorecontacts);
        btn_addmore_cntct.setOnClickListener(的OnClick());
        EditText上EditView中=新的EditText(本);
        editview.setText(添加更多);

    }

    //实现OnClickListener的OnClick()方法btn_addmore_cntct
    //按钮
    私人OnClickListener的OnClick(){
        // TODO自动生成方法存根
        //改变返回类型空到新OnClickListner
        返回新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                最后的EditText选项卡=新的EditText(getApplicationContext());
                tab.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                //为编辑文本的意见设置ID
                tab.setId(ID);
                //和增量
                ID ++;

                //添加(0),以在顶部显示编辑文本视图
                layoutLinear.addView(标签,0);
                tab.requestFocus();
                发送短信//
                b_alert =(按钮)findViewById(R.id.balert);
                b_alert.setOnClickListener(新Button.OnClickListener(){

                    @覆盖
                    公共无效的onClick(查看为arg0){
                        // TODO自动生成方法存根

                        字符串smsNumber = tab.getText()的toString()。
                        字符串smsText =我在危险;

                        开放的我们的uri = Uri.parse(smsto:+ smsNumber);
                        意向意图=新的意图(Intent.ACTION_SENDTO,URI);
                        intent.putExtra(sms_body,smsText);
                        startActivity(意向);


                    }
                });

            }
        };

    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

    //调用onActivityResult触头时已选择从
    //电话簿,给后面的请求codeI开始它的RC它
    //返回与
    //从它的任何附加数据。
    @覆盖
    保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        // TODO自动生成方法存根
        super.onActivityResult(要求code,因此code,数据);

        如果(数据!= NULL){
            开放的我们的uri = data.getData();
            Log.i(数据,uri.toString());
            如果(URI!= NULL){
                光标C = NULL;
                尝试 {
                    C = getContentResolver()
                            .query(URI,
                                    新的String [] {
                                            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                            ContactsContract.CommonDataKinds.Phone.NUMBER,
                                            ContactsContract.CommonDataKinds.Phone.TYPE},
                                    NULL,NULL,NULL);

                    如果(C =空&安培;!&安培; c.moveToFirst()){
                        字符串名称= c.getString(0);
                        串号= c.getString(1);
                        整型= c.getInt(2);

                        showSelectedNumber(名称,数量,类型);
                    }
                } 最后 {
                    如果(C!= NULL){
                        c.close();
                    }
                }
            }
        }
    }

    //定义showSelectedNummber显示从电话簿中选择联系人
    //编辑文本浏览
    公共无效showSelectedNumber(字符串名称,串号,int型的){
        如果(layoutLinear == NULL){
            Log.i(layoutLinear为空,空);
        } 其他 {
            Log.i(layoutLinear不为空,不为空);

        }

        的EditText userNumber =(EditText上)layoutLinear.getChildAt(0);
        如果(userNumber == NULL){
            Log.i(EditText上为空,空);
        } 其他 {
            Log.i(EditText上不为空,不为空);

        }
        字符串typeNumber =(字符串)ContactsContract.CommonDataKinds.Phone
                .getTypeLabel(getResources(),类型,);
        userNumber.setText(名字+:+数字++ typeNumber);

    }

}
 

解决方案

的用数字环路您需要发送

  SmsManager.getDefault()。sendTextMessage(数字,
                                空,TEXT_TO_SEND,NULL,NULL);
 

In my application,i have created an edit-text view and three buttons. When user clicks on "Add More Contacts" button, it will add another edit-text view and when user clicks "Add Contact" this will take him to contacts of phone-book from where he can choose the contacts to which he wants to send the SMS. I have written program for all this but i don't know how to send message to all users automatically without the user confirmation when he just pressed the "Send Message"Button. I tried SMSManager but its depreciated.Please suggest me.

public class MainActivity extends Activity {
    private Button btn_cntct;
    public int REQUESTCODE = 1;
    private LinearLayout layoutLinear;
    private Button btn_addmore_cntct;
    private int id = 1;

    Button b_alert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // defining button elements for picking contacts from phone-book
        btn_cntct = (Button) findViewById(R.id.bpickperson);
        btn_cntct.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                // using Intent for fetching contacts from phone-book
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                startActivityForResult(intent, REQUESTCODE);
            }

        });
        // defining button and edit-text values for adding mutli edit-texts
        // views
        layoutLinear = (LinearLayout) findViewById(R.id.mLayout);
        btn_addmore_cntct = (Button) findViewById(R.id.baddmorecontacts);
        btn_addmore_cntct.setOnClickListener(OnClick());
        EditText editview = new EditText(this);
        editview.setText("Add more");

    }

    // implementing OnClickListener OnClick() method for "btn_addmore_cntct"
    // button
    private OnClickListener OnClick() {
        // TODO Auto-generated method stub
        // changing return type "null" to "new OnClickListner"
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final EditText tab = new EditText(getApplicationContext());
                tab.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                // setting id for edit-text views
                tab.setId(id);
                // and increment
                id++;

                // adding (0) to display edit-text view on the top
                layoutLinear.addView(tab, 0);
                tab.requestFocus();
                // for sending sms
                b_alert = (Button) findViewById(R.id.balert);
                b_alert.setOnClickListener(new Button.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                        String smsNumber = tab.getText().toString();
                        String smsText = "I am in danger";

                        Uri uri = Uri.parse("smsto:" + smsNumber);
                        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
                        intent.putExtra("sms_body", smsText);
                        startActivity(intent);               


                    }
                });

            }
        };

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // calling onActivityResult when contacts has been selected from the
    // phone-book, giving back the REQUESTCODE i started it with, the RC it
    // returned with
    // any additional data from it.
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null) {
            Uri uri = data.getData();
            Log.i("data", uri.toString());
            if (uri != null) {
                Cursor c = null;
                try {
                    c = getContentResolver()
                            .query(uri,
                                    new String[] {
                                            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                            ContactsContract.CommonDataKinds.Phone.NUMBER,
                                            ContactsContract.CommonDataKinds.Phone.TYPE },
                                    null, null, null);

                    if (c != null && c.moveToFirst()) {
                        String name = c.getString(0);
                        String number = c.getString(1);
                        int type = c.getInt(2);

                        showSelectedNumber(name, number, type);
                    }
                } finally {
                    if (c != null) {
                        c.close();
                    }
                }
            }
        }
    }

    // defining showSelectedNummber to display selected contact from phone-book
    // to Edit-Text View
    public void showSelectedNumber(String name, String number, int type) {
        if (layoutLinear == null) {
            Log.i("layoutLinear is null", "null");
        } else {
            Log.i("layoutLinear is not  null", "not null");

        }

        EditText userNumber = (EditText) layoutLinear.getChildAt(0);
        if (userNumber == null) {
            Log.i("edittext is null", "null");
        } else {
            Log.i("edittext is not  null", "not null");

        }
        String typeNumber = (String) ContactsContract.CommonDataKinds.Phone
                .getTypeLabel(getResources(), type, "");
        userNumber.setText(name + ": " + number + " " + typeNumber);

    }

}

解决方案

You this with loop of Numbers you need to send

SmsManager.getDefault().sendTextMessage("NUMBER",
                                null, "TEXT_TO_SEND", null, null);

这篇关于发送短信到多用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆