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

发布于 2024-12-05 19:26:27 字数 286 浏览 0 评论 0原文

我想使用 Android Intent.ACTION_SEND 来快速分享一些东西。所以我得到了这样的共享列表: 共享意图列表

但我想为每个操作共享不同的内容,例如:

  • 如果通过电子邮件/Gmail 共享,内容应为“通过电子邮件共享”。

  • 如果通过 Facebook 共享,内容应为“通过 Facebook 共享”。

那么,可以这样做吗?

I want to use Android Intent.ACTION_SEND for quickly sharing something. So I got a sharing list like this:
Sharing intent list

But I want to share different content for each action, such as:

  • If sharing by Email/Gmail, content should be "Share by email".

  • If sharing by Facebook, content should be "Share by Facebook".

So, is it possible to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷途知返 2024-12-12 19:26:27

你无法获得这样的信息。

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

要创建此类对话框,您需要使用 PackageManager 及其 queryIntentActivities() 函数。该函数返回List

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

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

You can't get such information.

Unless you create your own implementation of the dialog for the activity selection.

To create such dialog you need to use PackageManager and its queryIntentActivities() function. The function returns List<ResolveInfo>.

ResolveInfo contains some information about an activity (resolveInfo.activityInfo.packageName), and with the help of PackageManager you can get other information (useful for displaying the activity in the dialog) - application icon drawable, application label, ... .

Display the results in a list in a dialog (or in an activity styled as a dialog). When an item is clicked create new Intent.ACTION_SEND, add the content you want and add the package of the selected activity (intent.setPackage(pkgName)).

怎会甘心 2024-12-12 19:26:27

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

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

第 2 步:现在您已为您的适配器创建布局膨胀器 (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

步骤 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

There is not direct method to access such kind of information....

Step 1: Inside your code first of all you need to declare an adapter which will contain your custom view of list to be shared on...

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

Step 2: Now you have create a layout inflater for your adapter(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

Step 3: Create your xml layout type to show list view in dialog box(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
伴我心暖 2024-12-12 19:26:27

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

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

此实现的一个强大功能是您可以向调用添加分析。

Not sure if you are still looking for an answer, but ClickClickClack has an example implementation of how you can intercept the ACTION_SEND intent and choose based off package name and certain characteristics what ends up happening. In involves most of the steps mentioned by Tomik.

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

One powerful aspect about this implementation is you can add analytics to your calls.

冬天旳寂寞 2024-12-12 19:26:27

使用 Tomik 很棒的答案,我可以使用 PackageManager 生成自己的自定义共享列表
加载标签和加载图标:

public class MainActivity extends FragmentActivity
{

    ArrayList<Drawable> icons;
    ArrayList<String> labels;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);
        icons=new ArrayList<Drawable>();
        labels=new ArrayList<String>();
        PackageManager manager=getPackageManager();
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        List<ResolveInfo> activities=manager.
                queryIntentActivities(intent,0);
        for(int i=0;i<activities.size();i++)
        {
            ApplicationInfo appInfo=null;
            try {
                appInfo=manager.getApplicationInfo(activities.get(i).activityInfo.packageName,0);
                labels.add(name=appInfo.loadLabel(getPackageManager()).toString()); 
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            icons.add(appInfo.loadIcon(getPackageManager()));
        }
    }

}

Using Tomik great Answer i'm able to produce my own Custom share List using PackageManager
loadLabel and LoadIcon:

public class MainActivity extends FragmentActivity
{

    ArrayList<Drawable> icons;
    ArrayList<String> labels;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);
        icons=new ArrayList<Drawable>();
        labels=new ArrayList<String>();
        PackageManager manager=getPackageManager();
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        List<ResolveInfo> activities=manager.
                queryIntentActivities(intent,0);
        for(int i=0;i<activities.size();i++)
        {
            ApplicationInfo appInfo=null;
            try {
                appInfo=manager.getApplicationInfo(activities.get(i).activityInfo.packageName,0);
                labels.add(name=appInfo.loadLabel(getPackageManager()).toString()); 
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            icons.add(appInfo.loadIcon(getPackageManager()));
        }
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文