Android的 - 我如何检测是否用户已选择分享到Facebook或使用意图叽叽喳喳? [英] Android - How do I detect if user has chosen to share to Facebook or twitter using intent?

查看:267
本文介绍了Android的 - 我如何检测是否用户已选择分享到Facebook或使用意图叽叽喳喳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这股到Facebook,Twitter等应用程序,但我想执行的不同的功能 依赖于谁的用户共享,以,例如,如果用户共享给Facebook做一件事,但如果用户共享叽叽喳喳做一套。

I've created an application which shares to facebook, twitter etc. But I want to perform different functions dependent on who the user is sharing to, for instance if the user is sharing to Facebook do one thing but if the user shares to twitter do another.

我如何做到这一点?

我的code到目前为止如下:

My code so far is below:

private void ShareSub() {
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");
    i.putExtra(
            Intent.EXTRA_TEXT,
            "You've just shared "
                    + "Awesome");
    startActivity(i);
}

以上code的进一步解释是:

Further explanation of above code:

当用户presses了 menu_item_share 的操作栏中的按钮,上面的函数被触发。此功能则使得像图像的框显示在下面,其允许用户选择特定选项,例如Facebook或Twitter。那么 Intent.EXTRA_TEXT 共享给特定的应用程序。

When the user presses the "menu_item_share" button in the action bar, the function above is triggered. This function then makes a box like the image below appear, which allows the user to select a particular option, e.g. Facebook or twitter. The Intent.EXTRA_TEXT is then shared to that particular app.

什么我想是,当用户点击比如实,一个特定的函数被例如所谓通过他们(Facebook的)共享API等。

What I'm wanting is, when the user clicks for instance Facebook, a particular function is called e.g. Sharing via their (Facebook's) api etc.

ShareActionProvider 象下面这样:

   <item
        android:id="@+id/your_share_item"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:showAsAction="ifRoom"
        android:title="@string/share"/>

相反,我创建了一个简单的按钮,调用函数的 ShareSub 的:

<item
    android:id="@+id/menu_item_share"
    android:icon="@android:drawable/ic_menu_share"
    android:showAsAction="ifRoom"
    android:title="Share"/>

我选择这样做的原因是,我不希望在最近共享按钮出现,如下图所示。这是造成我的问题,当我尝试使用code以下建议,因为我没有使用过的 ShareActionProvider

The reason I've chosen to do it this way is, I don't want the recently shared to button to appear, as in the image below. This is causing me problems when I attempt to use the code suggested below because I haven't used ShareActionProvider.

推荐答案

您不能直接查出被选为该应用程序,但是可以显示你自己的选择器对话框。

You can't directly find out which app was chosen, but you can display your own Chooser dialog.

而不是调用的 startActivity(我),得到的所有活动(Facebook,微博等)已注册的使用来处理你的意图的列表<一个href=\"http://developer.android.com/reference/android/content/pm/PackageManager.html#queryIntentActivities%28android.content.Intent,%20int%29\"相对=nofollow> queryIntentActivities()。

Instead of calling startActivity(i), get a list of all activities (facebook, twitter etc) that are registered to handle your intent using queryIntentActivities().

List<ResolveInfo> activities = getPackageManager().queryIntentActivities (i, 0);

每个 ResolveInfo 是一个应用程序,可以处理你的意图。现在,您可以显示一个 AlertDialog 含标签或图标的列表。当用户从列表中选择一个标签,你可以处理什么做在对话框中单击处理程序的应用程序特定的基础上。

Each ResolveInfo is an app that can handle your intent. Now you can show an AlertDialog containing a list of labels or icons. When the user selects a label from the list, you can handle what do to on an app-specific basis in the click handler for the dialog.

修改:这是一个完整的工作示例

EDIT: Here's a full working example

private void ShareSub() {
    final Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("text/plain");
    i.putExtra(Intent.EXTRA_TEXT,"text");

    final List<ResolveInfo> activities = getPackageManager().queryIntentActivities (i, 0);

    List<String> appNames = new ArrayList<String>();
    for (ResolveInfo info : activities) {
        appNames.add(info.loadLabel(getPackageManager()).toString());
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Complete Action using...");
    builder.setItems(appNames.toArray(new CharSequence[appNames.size()]), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            ResolveInfo info = activities.get(item);
            if (info.activityInfo.packageName.equals("com.facebook.katana")) {
                // Facebook was chosen
            } else if (info.activityInfo.packageName.equals("com.twitter.android")) {
                // Twitter was chosen
            }

            // start the selected activity
            i.setPackage(info.activityInfo.packageName);
            startActivity(i);
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

这篇关于Android的 - 我如何检测是否用户已选择分享到Facebook或使用意图叽叽喳喳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆