質問

I am building an app that gets 3 contacts from CONTACTS ,, i want a ready made code to do this and to also show to the user in a layout,, which numbers he has choosen. I have heard that this task is to be done in a background thread I was thinking that if i would do this using

extending AsynTask    

is it true?? Please give me full code to do that.

My activity in which i have to include this code is like this:

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;    
}

}

役に立ちましたか?

解決

basically what you need to do to retrieve contacts from your phone is to use Content Providers: http://developer.android.com/guide/topics/providers/content-providers.html

You'll have to query your contacts and say what fields you want to retrieve from them, this query will return a Cursor to you and this cursor can be used to iterate through your contacts.

You can get a complete documentation about it on this page: http://developer.android.com/guide/topics/providers/contacts-provider.html

Once you get a hold of it, it's quite simple.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top