Mobile 获取手机上和SIM卡上的联系人

在做Mobile手机应用的时候要做一个类似于通讯录的功能,而通讯录上的内容有两部份组成一部分是使手机上自带的通讯录,一部分时Sim卡上的联系人,所以需要将两部分合并在一起才可以说是一个完美的通讯录

1.获取手机上的联系人的方法我使用MS提供的PocketOutlook.dll来实现,具体如下:

代码
using System;
using System.Collections.Generic;
using System.Text;
using PocketOutlook;
namespace BesttoneFramework.util
{
    
public class PocketOutlookExtension
    {
        ApplicationClass outlookApp 
= null;
        IFolder contactsFolder 
= null;
        
/// <summary>
        
/// 初始化
        
/// </summary>
        public PocketOutlookExtension()
        {
            
try
            {
                outlookApp 
= new PocketOutlook.ApplicationClass();
                outlookApp.Logon(
0);
                contactsFolder 
= outlookApp.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
            }
            
catch (System.Exception ex)
            {
                Log.LogWrite(ex);
                
throw ex;
            }
        }
        
/// <summary>
        
/// 返回通讯录列表
        
/// </summary>
        
/// <returns></returns>
        public PocketOutlook.Items GetList()
        {
            
try
            {
                PocketOutlook.Items contacts 
= (PocketOutlook.Items)contactsFolder.Items;
                
return contacts;
            }
            
catch (System.Exception err)
            {
                Log.LogWrite(err);
                
throw err;
            }
        }
    }
}

 这样手机上的联系人就轻松的拿到了。

2.获取SIM卡上的联系人:

代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace BesttoneFramework.util
{
    
public class SIMContactManage
    {
        
private const Int64 S_OK = 0x00000000;
        
public const int SIM_CAPSTYPE_ALL = 0x3F// 所有联系人
        public const int SIM_PBSTORAGE_SIM = 0x10// 
        public const int SIM_SMSSTORAGE_SIM = 0x2//

        [StructLayout(LayoutKind.Sequential)]
        
public struct SIMPHONEBOOKENTRY
        {
            
public uint cbSize; // 
            public uint dwParams; // 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            
public string lpszAddress; // 联系人电话
            public uint dwAddressType; //
            public uint dwNumPlan; //
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
            
public string lpszText; // 联系人姓名
        }

        [DllImport(
"cellcore.dll")]
        
public static extern int SimInitialize(uint dwFlags,
        
int lpfnCallBack, uint dwParam, ref int lphSim);
        [DllImport(
"cellcore.dll")]
        
public static extern int SimGetPhonebookStatus(int hSim,
        
uint dwLocation, ref uint lpdwUsed, ref uint lpdwTotal);
        [DllImport(
"cellcore.dll")]
        
public static extern int SimReadPhonebookEntry(int hSim, uint dwLocation, uint dwIndex, ref SIMPHONEBOOKENTRY entry);
        [DllImport(
"cellcore.dll", SetLastError = true)]
        
public static extern int SimDeinitialize(int hSim);

        
/// <summary>
        
/// 获取SIM卡联系人信息
        
/// </summary>
        
/// <returns></returns>
        public static List<string[]> GetSIMContactList()
        {
            
int hSim = 0;
            List
<string[]> list = new List<string[]>();
            
try
            {
                
int result = SimInitialize(000ref hSim);
                
if (result != 0)
                    
throw new Exception("SIM打卡失败,请检测SIM是否安装!");
                
uint uiUsed = 0;
                
uint uiTotal = 0;
                result 
= SimGetPhonebookStatus(hSim, SIM_PBSTORAGE_SIM, ref uiUsed, ref uiTotal);
                
for (int i = 1; i <= uiUsed; i++)
                {
                    SIMPHONEBOOKENTRY entry 
= new SIMPHONEBOOKENTRY();
                    entry.cbSize 
= (uint)Marshal.SizeOf(typeof(SIMPHONEBOOKENTRY));
                    result 
= SimReadPhonebookEntry(hSim, SIM_PBSTORAGE_SIM, (uint)i, ref entry);
                    list.Add(
new string[2] { entry.lpszText.Trim(), entry.lpszAddress.Trim() });
                }
                
return list;
            }
            
catch
            {
                
throw;
            }
            
finally
            {
                SimDeinitialize(hSim);
            }
        }
    }
}

 将手机上的里联系人和通讯录上的联系人合并到一起就可以得到整个通讯录了

原文地址:https://www.cnblogs.com/vaiyanzi/p/1662575.html