2014-04-25 60 views
-2

我建立一個應用程序,得3個接觸從聯繫人拿到手機號,, 我希望有一個現成的代碼來做到這一點,也顯示給用戶的佈局等,其中他選擇的數字。我聽說,這個任務是在後臺線程我在想,如果我會做到這一點使用代碼從通訊錄中的Android

extending AsynTask  

是真的做?請給我完整的代碼來做到這一點。

我的活動中,我必須包括該代碼是這樣的:

public class Registration extends Activity implements View.OnClickListener { 
RegisteredUser user; 
EditText name,mobile ; 
Button guardian1,guardian2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    Button submit; 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.registration); 
    submit = (Button) findViewById(R.id.register_registration_submit); 
    submit.setOnClickListener(this);  
} 

@Override 
public void onClick(View view) { 
    switch(view.getId()){ 

    case R.id.register_registration_submit: 
     if(isConnected()){ 
      if(inputData()) 
       Log.d("gone","hum"); 
       new SendRegistrationData(getBaseContext(),Registration.this).execute(user); 
     } 
     else 
      Toast.makeText(getBaseContext(), "You are not connected to internet.Please connect yourself and try again.", Toast.LENGTH_LONG).show(); 

     break; 

     default: Log.d("application","no button match"); 
    } 
} 

private boolean inputData() { 
    user = new RegisteredUser(); 

    name = (EditText) findViewById(R.id.registration_name); 
    mobile = (EditText) findViewById(R.id.registration_mob); 
    user.setName(name.getText().toString().trim()); 
    user.setMobile(mobile.getText().toString().trim()); 

      //code to input name and mobile and also validation 

      //** HERE I WANT CODE TO GET 3 CONTACTS .WHEN USER CLICK ON BUTTON. 
      button.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View arg0) {  
      new GetContact().execute(); 

     } 
    }); 

      /*/ also put a validation to check either the user has choosen 
      all three contacts if not,, return false to input data ,, 
      so that form can not be submitted */ 
    return true; 
} 






public boolean isConnected(){ 
    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
    if (networkInfo != null && networkInfo.isConnected()) 
     return true; 
    else 
     return false;  
} 

}

回答

0

基本上你需要做檢索聯繫人從您的手機是使用內容提供商的內容:http://developer.android.com/guide/topics/providers/content-providers.html

你必須查詢您的聯繫人和說你想從他們那裏獲取哪些字段,這個查詢會返回一個指針,以你和這個光標可以通過使用您的聯繫人進行迭代。

您可以在此頁面上獲取有關它的一個完整的文檔:http://developer.android.com/guide/topics/providers/contacts-provider.html

一旦你熟悉了它保持,這是相當簡單的。

+0

感謝..但它是我的項目的一個非常小的一部分,,所以我希望一個現成的代碼,,, – sagar