How to display QuickContact card from widget

2019-03-29 07:14发布

问题:

I have a widget that displays the picture of some of my contacts and I would like to display the QuickContact card when the user taps on one of the pictures. I know I should be using the method ContactsContract.QuickContact.showQuickContact(), but it requires a View or a Rect as one of the input parameters. My problem is that Widgets only have RemoteViews, so I'm no sure what to pass as the View or Rect parameter. Any ideas would be appreciated.

回答1:

You can reference the badge in the XML

I have this in the XML file:

     <QuickContactBadge
     android:id="@+id/photo"
    android:layout_width="54dip"
    android:layout_height="57dip"
    android:layout_marginLeft="5dip"
    android:background="@drawable/quickcontact_photo_frame"
    style="?android:attr/quickContactBadgeStyleWindowSmall"
     />

and this code:

private QuickContactBadge mPhotoView;
mPhotoView = (QuickContactBadge) findViewById(R.id.photo);
mPhotoView.assignContactUri(objItem.getUri());
mPhotoView.setMode(QuickContact.MODE_MEDIUM);

and this is the calling mode (but the click on the badge is handling this popup, this call too popup the chooser is made by clicking on something else)

QuickContact.showQuickContact(viewContactQuick.this, mPhotoView,objItem.getLookupUri() , QuickContact.MODE_MEDIUM, null);


回答2:

To show the QuickContact UI over a widget, you can make a callback PendingIntent using the technique illustrated here:

http://advback.com/android/working-with-app-widgets-android/

In your widget onUpdate(), create the intent and associate it with the RemoteView:

intent = new Intent(context, MyWidget.class);  
intent.setAction(ACTION_WIDGET_RECEIVER);  
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
intent.setData(uri);
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.my_widget_view, pendingIntent);

When the view is clicked, you'll get an onReceive() notification in your widget. Use Intent.getSourceBounds() to retrieve the rect, and show the QuickContact:

public void onReceive(Context context, Intent intent) {  
 if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {  
    Uri uri = intent.getData();
    if ( uri != null ) {
        QuickContact.showQuickContact(context, intent.getSourceBounds(), uri, ContactsContract.QuickContact.MODE_SMALL, null);
    }
 }  
 super.onReceive(context, intent);
}  


回答3:

I've been looking for this as well. Maybe the source of the Contacts app will be helpful. I'm trying to digg in: link text



回答4:

I've been struggling with this for some time now too. Looking at the Android source code, it appears that Google made a transparent activity called QuickContactActivity and placed the QuickContactWindow (the class that creates the popup) inside of that. I tried the same thing and the transparent activity does work but I'm having trouble getting the badge to show up. I know about Qberticus's QuickActions code and I did try it out but I'd rather just use the quickcontacts that google wrote because to duplicate its functionality and looks is pretty challenging.

I've also been getting ActivityNotFoundException when I try to use the QuickContact.showQuickContact() method on Eclair - it works great on Froyo though.

Here's my question. It would be really awesome if we could work together to get this issue solved: link