0

我正在尝试创建自定义 Android 主屏幕。我使用了示例主屏幕应用程序并对其进行了调整。我想做的事情是从主屏幕中的按钮打开联系人选择器,并使用用户在下一个操作中选择的联系人。我偶然发现了这个问题中提到的相同问题

我该如何解决这个问题,以便主屏幕保持“singleInstance”并且我还可以调用 startActivityForResult()?

联系人选择器是否是我可以子类化的活动(我已经搜索但找不到任何活动),以便我可以使用 David Wasser 在上述问题中提出的解决方案?

4

1 回答 1

0

I've found an elegant solution after all:

My main activity launches an intermediate, invisible activity that has android:theme="@android:style/Theme.NoDisplay"

This intermediate activity calls the contact picker in its onCreate

Intent phoneContactIntent = new Intent(Intent.ACTION_PICK, 
    ContactsContract.Contacts.CONTENT_URI);
// Show user only contacts w/ phone numbers
phoneContactIntent.setType(Phone.CONTENT_TYPE); 
startActivityForResult(phoneContactIntent, CHOOSE_CONTACT_TO_CALL);

Then, in onActivityResult, it creates a new intent for the main application, with the data that the contact picker returned.

    switch (requestCode) {
    case (CHOOSE_CONTACT_TO_CALL):
        if (resultCode == Activity.RESULT_OK) {
            Intent resultIntent = new Intent(this, Home.class);
            resultIntent.putExtras(data);

            Uri contactData = data.getData();
            if (contactData != null)
            {
                resultIntent.setData(contactData);
            }
            startActivity(resultIntent);
        }
    }
    finish();

and in my Home class, in onCreate I call getIntent() and inspect the data in the intent that launched the main activity.

于 2013-08-20T19:37:15.140 回答