6

I am new to android and trying to do following task:- I want to open Whatsapp contact list and get selected contact data. I am able to achieve half of the work. Whatsapp contact list opens , but once i press on any contact my app crashes. Here is my code:

To open Whatsapp contact list:

btnChoose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setPackage("com.whatsapp");
            try{
                Toast.makeText(AddScheduleActivity.this, "going out activity", Toast.LENGTH_SHORT).show();
                startActivityForResult(intent, 1);
                Toast.makeText(AddScheduleActivity.this, "getting In activity", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(AddScheduleActivity.this, "Whatsapp not found", Toast.LENGTH_SHORT).show();  //no activity found to handle this intent means whatsapp is not installed
            }
        }
    });

To get selection:-

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    Toast.makeText(AddScheduleActivity.this, "Ok In", Toast.LENGTH_SHORT).show();

    switch (requestCode) {
        case 1:
            if(resultCode == RESULT_OK){
                if(intent.hasExtra("contact")){

                    Toast.makeText(AddScheduleActivity.this, "Ok working", Toast.LENGTH_SHORT).show();
                    Uri uri = intent.getData();
                    //Query the content uri
                    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                    cursor.moveToFirst();
                    // column index of the phone number
                    int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    // column index of the contact name
                    int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    contactNumber = cursor.getString(phoneIndex);
                    name = cursor.getString(nameIndex);

                }
            }
            break;

        default:
            break;
    }
}

If i don't select any contact and press back , their is no crash in that case

Thanks in advance.

Priyanka
  • 207
  • 2
  • 10
  • If the application crashes, include the logcat in your question – OneCricketeer Nov 25 '16 at 08:14
  • http://stackoverflow.com/questions/24276573/how-to-get-contacts-which-are-used-in-whatsapp-or-other-application-in-android Already, a similar question is asked.Just check whether this works for you. – eshb Nov 25 '16 at 09:29
  • @baymaxx Thanks but i already seen it, its not helpful – Priyanka Nov 25 '16 at 09:34
  • @Priyanka Whats the reason that your app is getting crashed? Use the debugger in android studio..Atleast finding that error can help us find the problem.. – eshb Nov 25 '16 at 09:38

2 Answers2

2

intent.getdata() will always return null.

Use intent.getExtras().getString("contact"); to get the contact number.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
Ashwani Garg
  • 112
  • 8
1

Finally resolved the issue myself. Here is edited code:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode) {
        case 1:
            if(resultCode == RESULT_OK){

                if(intent.hasExtra("contact")){
                    String number = intent.getStringExtra("contact");

                    Toast.makeText(AddScheduleActivity.this, number, Toast.LENGTH_LONG).show();

                }
            }
            break;

        default:
            break;
    }
}
Priyanka
  • 207
  • 2
  • 10