关于 内容提供者的 详细准确代码。 -------获取联系人信息

前言

在学习 Android开发 时内容提供者老师讲的不是很全,讲的很快,那时候就很 烦躁,后来想着这块内容还是比较重要的。于是回过头来,慢慢沉下心来就感觉不是很难。在网上摸索了许久,整理了一下,写出了下面的比较详细的代码。 如有不足之处请指正

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {


    private List<Map<String, Object>> contactList = new ArrayList<>(); // 存储列表 --以map对象为list对象的子元素
    private ListView lv_main_listview; // 创建listview对象


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 绑定listview对象
        lv_main_listview = findViewById(R.id.lv_main_listview);
    }

    // 单击事件
    public void get_info(View view) {
        int id = 1;
        // 通过游标查询获取 电话uri对象
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            // 通过获取的uri对象进行 匹配获取
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            Log.v("test ----- :", number + name);
            //  新建 map 对象
            Map<String, Object> map = new HashMap<String, Object>();
            // 存入  “key”,"values"
            map.put("id", id);
            map.put("name", name);
            map.put("tel", number);
            contactList.add(map);
            id += 1;
        }
        // 适配器生成
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, contactList, R.layout.simple_listview, new String[]{"id", "name", "tel"}, new int[]{R.id.tv_items_id, R.id.tv_items_name, R.id.tv_items_tel});
        // 适配器设置
        lv_main_listview.setAdapter(simpleAdapter);
    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="适配器"

        android:textSize="32dp" />

    <ListView
        android:id="@+id/lv_main_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

    </ListView>

    <Button
        android:id="@+id/btn_get"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="get_info"
        android:text="获取联系人" />
</LinearLayout>

AndroidManifest

加入权限

    <uses-permission android:name="android.permission.READ_CONTACTS"/>

其它页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tv_items_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/tv_items_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/tv_items_tel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据引用和引用提供的信息,可以通过vue-baidu-map获取经纬度功能。然而,有时候获取的经纬度可能不准确。这可能是由于以下几个原因导致的: 1. 定位服务不可靠:获取位置信息需要依赖设备的定位服务,例如GPS或网络定位。如果定位服务不可靠或者设备所在位置信号弱,获取的经纬度可能会有偏差。 2. 地址解析不准确获取的经纬度是基于地理位置的地址解析结果。如果地址解析算法不准确或者地理位置信息存在误差,那么获取的经纬度也可能不准确。 3. 使用的百度地图AK(Access Key)不准确:在引用[1]中提到需要在baidu-map组件上添加百度地图的AK,如果AK填写不正确或者过期,可能会导致获取的经纬度不准确。 为了解决获取经纬度不准确的问题,可以尝试以下方法: 1. 确保设备的定位服务正常工作,并且在开放区域使用获取位置功能,以获得更精确的定位结果。 2. 检查地址解析算法是否准确,并尝试使用其他地理位置服务来获取准确的经纬度信息。 3. 确认百度地图的AK是否正确,并且没有过期。如果AK无效,可以申请新的AK并更新到baidu-map组件上。 总结起来,vue-baidu-map获取经纬度不准确可能是由于定位服务、地址解析或者AK的问题。通过确保定位服务正常工作、使用准确的地址解析算法和AK,可以提高获取经纬度的准确性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值