2014-10-31 87 views
0

我正在開發一個android應用程序,並且我想通過點擊按鈕閱讀或獲取SIM卡聯繫人。 你能幫我嗎?閱讀SIM卡聯繫人

我的按鈕XML代碼:

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/third" 
    android:layout_below="@+id/third" 
    android:layout_marginTop="30dp" 
    android:text="Read contact" 
    android:onClick="contactsAccess"/> 

我需要的Java代碼梅索德(contactsAccess)內使用。

+0

看[這裏](http://stackoverflow.com/questions/10412634/fetch-local-phonebook-contacts-from-sim-card-only-android/10412757#10412757) – 2014-10-31 13:20:46

+0

感謝您的答案,但我只需要SIM卡的聯繫人,而不是保存在手機上的聯繫人 – user3873819 2014-10-31 13:23:37

回答

0
public class MainActivity extends ActionBarActivity implements OnClickListener { 


    Button sim_btn; 


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

    sim_btn = (Button)findViewById(R.id.button1); 
    sim_btn.setOnClickListener(this); 
    getallsimnumber(); 
     } 


getallsimnumber(){ 
try 
     { 
      String ClsSimPhonename = null; 
      String ClsSimphoneNo = null; 

      Uri simUri = Uri.parse("content://icc/adn"); 
      Cursor cursorSim = this.getContentResolver().query(simUri,null,null,null,null); 

      Log.i("PhoneContact", "total: "+cursorSim.getCount()); 

      while (cursorSim.moveToNext()) 
      {  
       ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name")); 
       ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number")); 
       ClsSimphoneNo.replaceAll("\\D",""); 
       ClsSimphoneNo.replaceAll("&", ""); 
       ClsSimPhonename=ClsSimPhonename.replace("|",""); 

       Log.i("PhoneContact", "name: "+ClsSimPhonename+" phone: "+ClsSimphoneNo); 
      }   
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
}