我是新来的android和尝试执行以下任务: - 我想打开Whatsapp联系人列表并获取选定的联系人数据。我能够完成一半的工作。 WhatsApp的联系人列表打开,但一旦我按任何联系我的应用程序崩溃。这里是我的代码:从android应用程序打开Whatsapp联系人列表
要打开WhatsApp的联系人列表:
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
}
}
});
要获得选择: -
@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;
}
}
如果我不选择任何联系人,然后按回,他们没有崩溃在这种情况下
在此先感谢。
如果应用程序崩溃,包括logcat的在你的问题 –
http://stackoverflow.com/questions/24276573/how-to-get-contacts-which-are-used-in -whatsapp-or-other-application-in-android现在已经有类似的问题了,请检查一下是否适合你。 – eshb
@baymaxx谢谢,但我已经看到它,它没有帮助 – Priyanka