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

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

问题描述

我已经创建了一个应用程序,该应用程序与Facebook,Twitter等共享。但是,我想执行不同的功能 取决于用户共享的用户,例如用户正在分享给Facebook做一件事,但如果用户与Twitter进行了分享,则另行进行。



我该怎么做?



我的代码到目前为止:

  private void ShareSub(){
Intent i = new意图(Intent.ACTION_SEND);
i.setType(text / plain);
i.putExtra(
Intent.EXTRA_TEXT,
你刚刚分享了
+Awesome);
startActivity(i);
}

上述代码的进一步说明:



当用户按下操作栏中的 menu_item_share 按钮时,将触发上述功能。然后,该功能使得像下面的图像一样的框出现,这允许用户选择特定的选项,例如。 Facebook或Twitter。

我想要的是,当用户点击例如Facebook,一个特定的功能称为例如通过他们的(Facebook的)api等共享。





我没有使用 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/>

我以这种方式选择的原因是,我不想要最近共享到按钮出现,如下图所示。当我尝试使用下面提供的代码,因为我没有使用 ShareActionProvider 时,这会导致我出现问题。



解决方案

你不能直接找到选择哪个应用程序,但是您可以显示自己的Chooser对话框。



而不是调用 startActivity(i),获取所有使用 queryIntentActivities()

 code>列表与LT; ResolveInfo> activities = getPackageManager()。queryIntentActivities(i,0); 

每个 ResolveInfo 是一个可以处理的应用程序你的意图现在,您可以显示包含标签或图标列表的 AlertDialog 。当用户从列表中选择一个标签时,您可以在对话框的点击处理程序中处理在特定于应用程序的方法。



编辑:这是一个完整的工作示例

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

final列表< ResolveInfo> activities = getPackageManager()。queryIntentActivities(i,0);

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

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(使用...完成操作);
builder.setItems(appNames.toArray(new CharSequence [appNames.size()]),新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int item){
ResolveInfo info = activities.get(item);
if(info.activityInfo.packageName.equals(com.facebook.katana)){
// Facebook被选择
} else if info.activityInfo.packageName.equals(com.twitter.android)){
// Twitter被选为
}

//启动所选活动
i.setPackage(info.activityInfo.packageName);
startActivity(i);
}
});
AlertDialog alert = builder.create();
alert.show();
}


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.

How do I do this?

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);
}

Further explanation of above code:

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.

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.

I'm not using ShareActionProvider like below:

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

Instead, I've created a simple button which calls the function ShareSub:

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

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.

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);

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或Twitter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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