如何以编程方式获取 Android 中的所有 Sim 联系人? [重复]

问题描述 投票:0回答:3

如何以编程方式在 Android 中列出 SIM 卡联系人?我获得了获取电话联系人的代码此处,但我也需要 SIM 联系人。

android android-contacts
3个回答
6
投票
Uri simUri = Uri.parse("content://icc/adn");
   
Cursor cursorSim = this.getContentResolver().query(simUri, null, null,null, null);

while (cursorSim.moveToNext()) {           
    listName.add(cursorSim.getString(cursorSim.getColumnIndex("name")));
    listContactId.add(cursorSim.getString(cursorSim.getColumnIndex("_id")));      
    listMobileNo.add(cursorSim.getString(cursorSim.getColumnIndex("number")));
}

使用此代码获取 SIM 卡详细信息。效果很好。


0
投票

试试这个::

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    ArrayList<String>listName=new ArrayList<String>();
    ArrayList<String>listContactId=new ArrayList<String>();
    ArrayList<String>listMobileNo=new ArrayList<String>();
    ArrayList<String>idArr=new ArrayList<String>();
    ArrayList<String>nameArr=new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Uri simUri = Uri.parse("content://icc/adn");

        ContentResolver cr = getContentResolver();
        Cursor cursorSim = cr.query(ContactsContract.Contacts.CONTENT_URI,
               null, null, null, null);
       // Cursor cursorSim = this.getContentResolver().query(simUri, null, null,null, null);

               while (cursorSim.moveToNext()) {           
                //   listName.add(cursorSim.getString(cursorSim.getColumnIndex("name")));
                 //  listContactId.add(cursorSim.getString(cursorSim.getColumnIndex("_id")));      
                 //  listMobileNo.add(cursorSim.getString(cursorSim.getColumnIndex("number")));
                   String id = cursorSim.getString(cursorSim.getColumnIndex(ContactsContract.Contacts._ID));
                   String name = cursorSim.getString(cursorSim.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


                 //  String number=cursorSim.getString(cursorSim.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                   idArr.add(id);
                   nameArr.add(name);

                 //  listMobileNo.add(number);


                   if (Integer.parseInt(cursorSim.getString(cursorSim.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                   System.out.println("name : " + name + ", ID : " + id);

                   // get the phone number
                   Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                          ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                          new String[]{id}, null);
                   while (pCur.moveToNext()) {
                         String phone = pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         System.out.println("phone=" + phone);
                         listMobileNo.add(phone);
                   }
                   pCur.close();

                  }
               }
               System.out.println("idArr="+idArr);
               System.out.println("nameArr="+nameArr);

             //  System.out.println("listName="+listName);
              // System.out.println("listContactId="+listContactId);
               System.out.println("listMobileNo="+listMobileNo);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

将此代码添加到您的 mainifest 文件中:

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

-3
投票

你试过这个吗

TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();

还在清单文件中包含 READ_PHONE_STATE 权限。

© www.soinside.com 2019 - 2024. All rights reserved.