如何知道在Intent.ACTION_SEND中选择了哪个意图?

ant*_*afe 19 android android-intent

我想使用Android Intent.ACTION_SEND快速分享内容.所以我有一个像这样的共享列表: 分享意图清单

但我希望为每个操作分享不同的内容,例如:

  • 如果通过电子邮件/ Gmail共享,内容应为"通过电子邮件分享".

  • 如果通过Facebook分享,内容应该是"通过Facebook分享".

那么,有可能这样做吗?

Tom*_*mik 28

你无法得到这样的信息.

除非您为活动选择创建自己的对话框实现.

要创建此类对话框,您需要使用PackageManager它的queryIntentActivities()功能.该函数返回List<ResolveInfo>.

ResolveInfo包含有关activity(resolveInfo.activityInfo.packageName)的一些信息,并在PackageManager您的帮助下可以获取其他信息(对于在对话框中显示活动很有用) - 应用程序图标drawable,应用程序标签,....

在对话框(或作为对话框设置样式的活动)的列表中显示结果.单击某个项目时,创建新的Intent.ACTION_SEND,添加所需的内容并添加所选活动的包(intent.setPackage(pkgName)).


Vin*_*shi 12

没有直接的方法来访问这类信息....

第1步:在您的代码中首先需要声明一个适配器,该适配器将包含要在...上共享的列表的自定义视图...

//sharing implementation
                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);

                    // what type of data needs to be send by sharing
                    sharingIntent.setType("text/plain");

                    // package names
                    PackageManager pm = getPackageManager();

                    // list package
                    List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);

                    objShareIntentListAdapter = new ShareIntentListAdapter(CouponView.this,activityList.toArray());

                    // Create alert dialog box
                    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                    builder.setTitle("Share via");
                    builder.setAdapter(objShareIntentListAdapter, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int item) {

                            ResolveInfo info = (ResolveInfo) objShareIntentListAdapter.getItem(item);

                            // if email shared by user
                            if(info.activityInfo.packageName.contains("Email") 
                                    || info.activityInfo.packageName.contains("Gmail")
                                    || info.activityInfo.packageName.contains("Y! Mail")) {

                                PullShare.makeRequestEmail(COUPONID,CouponType);
                            }

                            // start respective activity
                            Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                            intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
                            intent.setType("text/plain");
                            intent.putExtra(android.content.Intent.EXTRA_SUBJECT,  ShortDesc+" from "+BusinessName);
                            intent.putExtra(android.content.Intent.EXTRA_TEXT, ShortDesc+" "+shortURL);
                            intent.putExtra(Intent.EXTRA_TITLE, ShortDesc+" "+shortURL);                                                            
                            ((Activity)context).startActivity(intent);                                              

                        }// end onClick
                    });

                    AlertDialog alert = builder.create();
                    alert.show();
Run Code Online (Sandbox Code Playgroud)

第2步:现在您已为适配器创建布局inflater(ShareIntentListAdapter.java)

package com.android;

import android.app.Activity;
import android.content.pm.ResolveInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ShareIntentListAdapter extends ArrayAdapter{

    private final Activity context; 
    Object[] items;


    public ShareIntentListAdapter(Activity context,Object[] items) {

        super(context, R.layout.social_share, items);
        this.context = context;
        this.items = items;

    }// end HomeListViewPrototype

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        LayoutInflater inflater = context.getLayoutInflater();

        View rowView = inflater.inflate(R.layout.social_share, null, true);

        // set share name
        TextView shareName = (TextView) rowView.findViewById(R.id.shareName);

        // Set share image
        ImageView imageShare = (ImageView) rowView.findViewById(R.id.shareImage);

        // set native name of App to share
        shareName.setText(((ResolveInfo)items[position]).activityInfo.applicationInfo.loadLabel(context.getPackageManager()).toString());

        // share native image of the App to share
        imageShare.setImageDrawable(((ResolveInfo)items[position]).activityInfo.applicationInfo.loadIcon(context.getPackageManager()));

        return rowView;
    }// end getView

}// end main onCreate
Run Code Online (Sandbox Code Playgroud)

第3步:创建xml布局类型以在对话框中显示列表视图(social_share.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/categoryCell"
    android:layout_width="match_parent"
    android:layout_height="30dip"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/shareName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginBottom="15dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="15dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/shareImage"
        android:layout_width="35dip"
        android:layout_height="35dip"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:contentDescription="@string/image_view" />

</RelativeLayout>

// vKj
Run Code Online (Sandbox Code Playgroud)


Ste*_*ven 5

不确定您是否仍在寻找答案,但ClickClickClack有一个示例实现,您可以如何拦截ACTION_SEND意图并根据包名称和某些特征选择最终发生的事情.涉及Tomik提到的大多数步骤.

http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/

关于此实现的一个强大方面是您可以为您的调用添加分析.