向联系人列表中的电话号码发送短信

Posted

技术标签:

【中文标题】向联系人列表中的电话号码发送短信【英文标题】:send sms to phone numbers from contact list 【发布时间】:2014-02-16 09:28:15 【问题描述】:

我在从电话簿中获取联系人后尝试发送短信。我的消息是固定的(必须发送),但数量取决于用户的选择。

String smsNumber =    " "  ;
                String smsText = "Hello Please";

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

                startActivity(intent);

我正在使用这个,但我如何向选定的人发送短信。

public class ContactList extends ListActivity 
    /** Called when the activity is first created. */

    private ArrayList<contact> contact_list = null;
    private ProgressDialog mProgressDialog = null;
    private contactAdapter mContactAdapter = null;
    private Runnable mViewcontacts = null;

    private ArrayList<contact> items;
    boolean[] isChecked;
    Cursor mCursor;
    ListView lv;
    Button b_alert;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);







        contact_list = new ArrayList<contact>();
        lv = getListView();

        mViewcontacts = new Runnable() 
            @Override
            public void run() 
                getContacts();
            
        ;

        Thread thread = new Thread(null, mViewcontacts, "ContactReadBackground");
        thread.start();
        mProgressDialog = ProgressDialog.show(ContactList.this,
                "Please Wait...", "Retriving Contacts...", true);
    

    @SuppressWarnings("unused")
    private void getContacts() 

        try 

            String[] projection = new String[] 
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts.HAS_PHONE_NUMBER,
                    ContactsContract.Contacts._ID ;

            mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                    projection, ContactsContract.Contacts.HAS_PHONE_NUMBER
                            + "=?", new String[]  "1" ,
                    ContactsContract.Contacts.DISPLAY_NAME);

            while (mCursor.moveToNext()) 
                contact contact = new contact();

                String contactId = mCursor.getString(mCursor
                        .getColumnIndex(ContactsContract.Contacts._ID));
                contact.setContactName(mCursor.getString(mCursor
                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
                contact_list.add(contact);
            
            isChecked = new boolean[mCursor.getCount()];

            for (int i = 0; i < isChecked.length; i++) 
                isChecked[i] = false;
            

            this.mContactAdapter = new contactAdapter(this, R.layout.listview,
                    contact_list);
            lv.setAdapter(this.mContactAdapter);
            mCursor.close();

            runOnUiThread(returnRes);

         catch (Exception e) 
            Log.d("getContacts", e.getMessage());
        
    

    public class contactAdapter extends ArrayAdapter<contact> 

        public contactAdapter(Context context, int textViewResourceId,
                ArrayList<contact> items1) 
            super(context, textViewResourceId, items1);
            items = items1;
        

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) 
            ViewHolder mViewHolder;

            mViewHolder = new ViewHolder();
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.listview, parent, false);
            mViewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox);

            convertView.setTag(mViewHolder);

            if (isChecked[position] == true)
                mViewHolder.cb.setChecked(true);
            else
                mViewHolder.cb.setChecked(false);
            mViewHolder.cb
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() 

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean ischecked) 
                            if (buttonView.isChecked())
                                isChecked[position] = true;
                            else
                                isChecked[position] = false;
                        
                    );

            contact contacts = items.get(position);
            if (contacts != null) 
                if (mViewHolder.cb != null) 
                    mViewHolder.cb.setText(contacts.getContactName());
                
            

            return convertView;
        
    

    public class ViewHolder 
        CheckBox cb;
    

    private Runnable returnRes = new Runnable() 

        @Override
        public void run() 
            if (mProgressDialog.isShowing())
                mProgressDialog.dismiss();
            mContactAdapter.notifyDataSetChanged();


        
    ;


【问题讨论】:

【参考方案1】:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
            sendIntent.putExtra("sms_body", "Check out this photo:it's via abc:https://www.abc.com/"); 
            sendIntent.setType("vnd.android-dir/mms-sms");
            sendIntent.putExtra("address",_phoneNo);//here phoneno is String that contain phone no
            startActivity(sendIntent);

【讨论】:

以上是关于向联系人列表中的电话号码发送短信的主要内容,如果未能解决你的问题,请参考以下文章

无法向菲律宾号码发送可编程短信 (Twilio)

ios:短信编辑器未显示

在 iphone 中以编程方式发送短信

如何从 Apple Watch 中的手表扩展发送短信

我们可以通过Android中的用户电话号码找到用户吗?

发送短信/拨打电话到选定的核心数据属性