Android:打电话和发短信:由姓名获取到电话

在手机联系人中:由姓名查找到电话号码(根据模糊“汉字”查找/姓名查找/电话查找到光标cursor,然后通过光标获取该组的信息:姓名,联系电话,头像等)
由光标获取信息时:
1、光标首先指到某个位置
cursor.move(offset);
cursor.moveToPosition(position);
cursor.moveToFirst();
2、获取多少个元组,>0才进行之后的步骤。
cursor.getCount()
3、获取姓名…..
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
4、别忘加权限哈:

<!--打电话和发信息的-->
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
String[] projection =
                        { ContactsContract.PhoneLookup.DISPLAY_NAME,
                                ContactsContract.CommonDataKinds.Phone.NUMBER };
                        Cursor cursor = this
                                .getContentResolver()
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        projection, // Which columns to return.
                                        // 这是模糊查询
                                        ContactsContract.PhoneLookup.DISPLAY_NAME
                                                + " LIKE"
                                                + " '%"
                                                + call_name
                                                + "%'",
                                        // WHERE clause.
                                        // (如果你根据具体的条件来查询也可以这样写:)
                                        // ContactsContract.CommonDataKinds.Phone.NUMBER
                                        // +
                                        // " = '"
                                        // + number + "'", // WHERE clause.
                                        // // 这是根据电话号码来查询
                                        // ContactsContract.Contacts.DISPLAY_NAME
                                        // + " = '"
                                        // + call_name + "'", // WHERE
                                        // clause. //
                                        // 这是根据姓名来查询
                                        null, // WHERE clause value
                                                // substitution
                                        null); // Sort order.
                        if (cursor == null)
                        {
                            // 找不到该人
                            Log.i("TAG", "getPeople null");

                        } else
                        {
                            Log.i("TAG", "getPeople cursor.getCount() = "
                                    + cursor.getCount());
                            if (cursor.getCount() > 0)
                            {
                                // 找到该人
                                cursor.moveToFirst();
                                callName = cursor
                                        .getString(cursor
                                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                                callNumber = cursor
                                        .getString(cursor
                                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            } 

打电话:

//Intent.ACTION_CALL:直接拨打:拨打按钮已经按下
//Intent.ACTION_VIEW:联系人列表
// 打电话(启动的页面是未拨打:拨打按钮还未按下)
startActivity(new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + callNumber)));

发短信:

SmsManager smsManager = SmsManager.getDefault();
// 发送短信
//mNumber号码,message短信内容
                if (message.length() > 70)
                {
                    List<String> contents = smsManager.divideMessage(message);
                    for (String sms : contents)
                    {
                        smsManager.sendTextMessage(mNumber, null, sms, null,
                                null);
                    }
                } else
                {
                    smsManager.sendTextMessage(mNumber, null, message, null,
                            null);
                }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中关闭或开启移动网络数据,可以通过代码或者系统设置完成。 方法一:通过代码实现 1. 首先,在AndroidManifest.xml文件中添加以下权限: ``` <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> ``` 2. 在需要开启或关闭移动网络数据的地方,添加以下代码: ``` ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Network[] networks = connectivityManager.getAllNetworks(); for (Network network : networks) { NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network); if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { if (isNetworkEnabled) { connectivityManager.bindProcessToNetwork(network); } else { connectivityManager.bindProcessToNetwork(null); } return; } } } else { NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (networkInfo != null) { if (isNetworkEnabled) { setMobileDataEnabled(true); } else { setMobileDataEnabled(false); } } } ``` 其中,`isNetworkEnabled`表示是否开启移动网络数据。如果为true,则开启移动网络数据;否则关闭移动网络数据。 3. 添加以下方法: ``` private void setMobileDataEnabled(boolean enabled) { try { Method method = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class); method.invoke(connectivityManager, enabled); } catch (Exception e) { e.printStackTrace(); } } ``` 方法二:通过系统设置实现 1. 打开系统设置,选择“数据使用”或“移动网络”。 2. 打开“移动数据”开关即可开启移动网络数据,关闭开关即可关闭移动网络数据。 注意:不同版本的Android系统可能会有不同的设置界面和操作方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值