Android之调取本地通讯录、发送短信、邮箱、微信发送

前言:在我们开发中会遇到一些信息发送开发需求,我们那时就会用到我们要选择我们要发送的客户对象、选择发送的方式如短信、邮箱、微信等等方式来进行信息的发送。

 

如下str为要发送的内容,account 为我们要发送的用户。

调用本地的通讯录:

调用 :

//读取通讯录
                startActivityForResult(new Intent(
                        Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 0);

调用回调 :

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            ContentResolver reContentResolverol = getContentResolver();
            Uri contactData = data.getData();
            @SuppressWarnings("deprecation")
            Cursor cursor = managedQuery(contactData, null, null, null, null);
            cursor.moveToFirst();
            username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone1 = reContentResolverol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
                    null,
                    null);
            while (phone1.moveToNext()) {
                usernumber = phone1.getString(phone1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                phone.setText(usernumber);
                editText.setText(username);
            }
//            cursor.close();
        }

    }

 通过短信发送:

Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + account));
                            sendIntent.putExtra("sms_body", str);
                            startActivity(sendIntent);

       通过微信发送信息:

                            Intent intent=new Intent(Intent.ACTION_SEND);
                            intent.setType("text/plain");
                            intent.setPackage("com.tencent.mm");//intent.setPackage("com.sina.weibo");
                            intent.putExtra(Intent.EXTRA_TEXT, str);
                            startActivity(Intent.createChooser(intent, "请选择"));

通过邮箱发送信息

   Intent i = new Intent(Intent.ACTION_SEND);
                        // i.setType("text/plain"); //模拟器请使用这行
                        i.setType("message/rfc822"); // 真机上使用这行
                        i.putExtra(Intent.EXTRA_EMAIL,
                                new String[] { account });
                        i.putExtra(Intent.EXTRA_SUBJECT, "门锁密码");
                        i.putExtra(Intent.EXTRA_TEXT, str);
                        startActivity(Intent.createChooser(i, "Select email application."));

猜你喜欢

转载自blog.csdn.net/weixin_39460667/article/details/81435481
今日推荐