4

我正在尝试访问诺基亚 5130c-2 XpressMusic 上的手机电话簿和 SIM 电话号码簿。该应用程序运行没有错误,但它只返回手机电话簿中的号码。当我使用此代码列出可用的电话簿时

String[] all_contact_lists=PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);

它给了我电话簿和 SIM 卡列表。即 1. 电话 2. SIM

我已经尝试使用此代码从 SIM 卡中显式读取,但它仍然没有返回任何内容(即使我在 SIM 卡中保存了数字。)代码:

ContactList clist = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY,
                "SIM");

这是我的完整代码::

import javax.microedition.midlet.*;
import javax.microedition.pim.*;
import com.sun.lwuit.*;
import java.util.*;

public class contacts extends MIDlet
{
    private List my_list=new List();
    private String[] names=null;
    public void startApp()
    {
        Display.init(this);
       Form my_form=new Form("Contacts List");       
       String[] all_contact_lists=PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);
       //Iterate through available phonebooks
       for(int db=0; db<all_contact_lists.length; db++)
       {
        try {
            ContactList clist = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY,
                    all_contact_lists[db]);
            Enumeration contacts=clist.items();
            while(contacts.hasMoreElements())
            {
                Contact contact=(Contact)contacts.nextElement();
                try{
                    String phone_contact="";
                    names=contact.getStringArray(Contact.NAME, 0);
                    for(int i=0; i<names.length; i++)
                    {
                        if(names[i]!=null)
                            phone_contact+=" "+names[i];
                    }
                    //my_list.addItem(phone_contact);
                    int phone_numbers=contact.countValues(Contact.TEL);
                    if(phone_numbers>0)
                    {
                         String number=contact.getString(Contact.TEL,0);
                        my_list.addItem(phone_contact+":"+number);
                    }
                    else
                    {
                        my_list.addItem(phone_contact);
                    }
                    //clist.removeContact(contact);
                }
                catch (Throwable t) {
            t.printStackTrace();
            }

            }
        } catch (PIMException ex) {
            ex.printStackTrace();
        }
       }
       //my_list.addItem(all_contact_lists);
       my_list.setRenderingPrototype("WWWWWWWWWWWWWWWWWWWW");
       my_form.addComponent(my_list);
       my_form.show();
       }
    public void pauseApp(){}
    public void destroyApp(boolean unconditional){}


}
4

2 回答 2

2

If you are using the phone number for a text or to call you can do that with only one line of code. Its now evident that phone software issues can affect the way the app accesses the PIM API. Also, if memory in use in the phones contact settings is set to SIM(alone), you can't access contacts in phone memory and vice versa, make sure both are in use. Try this if you still have an issue,

//make a text field in LWUIT that is declared globally

PhnNmbr = new TextField();

//set it to only accept phonenumber

PhnNmbr.setConstraint(TextField.PHONENUMBER);

//tell the user how to access phonebook

PhnNmbr.setHint("Press T9 then 'Search' to search phonebook");

//add a button or command 
//that either sends an sms to
//or calls the number in the text field
//or anything else you want to do with it

When the user presses T9, the TextField is considered an LCDUI text field with parameter PHONENUMBER which allows it to search for contacts in both Sim and phone memory, thats why you'll notice a Search command(usually at the bottom center). Also make sure that the memory in use for the phone is set to both Phone and Sim.

于 2012-07-06T21:54:17.463 回答
1

摘自 PIM javadoc

PIMItems 通过字段引用其数据。字段是一组具有相似特征的数据值。字段的一个示例是 TEL,它表示该特定字段的数据值是电话号码。实现 PIMItem 接口的类定义了该特定类的可能字段(例如,在 Contact 接口中将 TEL 定义为联系人可能支持的字段)。

PIM 实现不需要支持在实现 PIMItem 接口的类中定义的所有可能字段。这是因为没有本地 PIM 数据库包含此 API 中定义的所有字段。PIMItem 所属的 PIMList 决定了 PIMItem 可以支持和存储哪些字段(特定 PIMList 中的所有 PIMItem 都支持同一组字段)。来自特定 PIMItem 的 PIMList的 PIMList.getSupportedFields()方法用于找出该项目中支持哪些字段。由于并非所有可能的字段都在特定 PIMItem 中实际受支持,因此在用于任何检索或存储方法之前,应检查 项目的 PIMList中的所有字段是否支持。PIMList.isSupportedField(int)

每个字段都有以下可用信息:

  • 与字段关联的零个或多个数据值
  • 字段数据值的属性
  • 字段的描述性标签
  • 与字段关联的数据的数据类型

完整的 PIM javadoc 可以在这个链接上阅读。

检查设备是否支持 PIM 字段Contact.NAMEContact.TEL您的设备。如果没有,那么您需要致电PIMList.getSupportedFields()以获取设备上支持的字段,并相应地获取该设备的名称和电话号码。如果我没记错的话,名称的替代字段是Contact.FORMATTED_NAME.

于 2011-12-06T20:56:14.260 回答