在android中选择联系人发送短信

时间:2014-07-14 12:55:33

标签: android sms

我一直在开发一个应用程序,它将从我的手机联系人那里获取所有联系人,并向该列表中的选定联系人发送短信。我可以通过复选框获取所有联系人以供选择。当我选择联系人并按下"发送消息"按钮消息将自动发送一些文本,如" whats up!"等等

我需要帮助将短信发送给多个联系人。

我的主要活动:

public class MainActivity extends  ListActivity {
    ArrayList<String> name1 = new ArrayList<String>();
    ArrayList<String> phno1 = new ArrayList<String>();
    protected static final String TAG = null;
    public String[] Contacts = {};
    public int[] to = {};
    public ListView myListView;


    //@SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final Button done_Button = (Button) findViewById(R.id.done_Button);
        final Button clear_Button =(Button) findViewById(R.id.clear_Button);
        Cursor mCursor = getContacts();

        startManagingCursor(mCursor);
        ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, mCursor,
                Contacts = new String[] {ContactsContract.Contacts.DISPLAY_NAME },
                to = new int[] { android.R.id.text1 });

        setListAdapter(adapter);
        myListView = getListView();
        myListView.setItemsCanFocus(false);
        myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        clear_Button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Selection Cleared", Toast.LENGTH_SHORT).show();
                ClearSelections();
            }
        });

        /** When 'Done' Button Pushed: **/

        done_Button.setOnClickListener(new View.OnClickListener() {

            public void onClick (View v){   
                String name = null;
                String number = null;
                long [] ids = myListView.getCheckedItemIds();
                for(long id : ids) {
                    Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id + "" }, null);
                    while(contact.moveToNext()){
                        name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        //name+=name;
                        number = contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        //number+=number;
                    }
                    Toast.makeText(getApplicationContext(), "Name: " +name + "\n" + "Number: " + number , Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void ClearSelections() {
        int count = this.myListView.getAdapter().getCount();
        for (int i = 0; i < count; i++) {
            this.myListView.setItemChecked(i, false);
        }
    }

    @SuppressWarnings("deprecation")
    private Cursor getContacts() {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                                        ContactsContract.Contacts.DISPLAY_NAME};
        String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '"
                + ("1") + "'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";

        return managedQuery(uri, projection, selection, selectionArgs,
                sortOrder);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }     
}

2 个答案:

答案 0 :(得分:2)

您需要使用SmsManager发送短信。以下是供您参考的代码:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null); 

将此代码应用于您的发送消息按钮单击侦听器。

答案 1 :(得分:0)

public class sos extends AppCompatActivity {
TextView t1,t2;
Button sos;
ImageView add;
LinearLayout l1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sos);
    setTitle("SOS");
    sos= findViewById(R.id.sos);
    add= findViewById(R.id.add);
    t2= findViewById(R.id.t2);
    t1= findViewById(R.id.t1);

    sos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String s="";
            s=t2.getText().toString();
            Intent intent = new Intent(Intent.ACTION_SENDTO,Uri.parse("sms:"+s));
            intent.putExtra("sms_body", "Hello");
            startActivity(intent);

        }
    });
    //open contracts
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent= new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent,1);

        }
    });
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        Uri contactData = data.getData();
        Cursor c =  getContentResolver().query(contactData, null, null, null, null);
        if (c.moveToFirst()) {
            String phoneNumber="";
            String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
            //http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list   our upvoted answer

            String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            if ( hasPhone.equalsIgnoreCase("1"))
                hasPhone = "true";
            else
                hasPhone = "false" ;

        if (Boolean.parseBoolean(hasPhone))
            {
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                while (phones.moveToNext())
                {
                    phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                phones.close();
            }


            t1.setText("Name: "+name);
            t2.setText("Phone: "+phoneNumber);



          Log.d("curs", name + " num" + phoneNumber );
        }
        c.close();
    }
}

}