Sharing to facebook, twitter via share intent android

前端 未结 3 1104
野趣味
野趣味 2020-12-12 23:31

I am currently trying to share text to gmail, facebook and twitter. I am using share intent method to share. Below is the code i used to share

Intent sharing         


        
3条回答
  •  执笔经年
    2020-12-12 23:42

    Sharing with Facebook has the problem that it only accepts an url in the EXTRA_TEXT. The native FB app will then grab a text and an image from this url to compose the post. That's not a "bug" but the intended behaviour they give to the official app :-/

    Take into account Twitter will only accept text/plain without any markup and with 144 chars at max.

    I've made my own "Share" dialog to intercept all different behaviours and give to each target intent what they accepts: HTML, plain text, FB URL and Twitter short text.

    Here comes the code:

    public class ShareHelper implements AdapterView.OnItemClickListener {
                private static final String TAG = ShareHelper.class.getSimpleName();
    
                private Activity mContext;
                private Dialog mDialog;
                private LayoutInflater mInflater;
                private GridView mGrid;
    
    
       private ShareIntentAdapter mAdapter;
            private int mMaxColumns;
    
            private List plainTextActivities;
            private Set htmlActivitiesPackages;
    
            private String subject;
            private String textbody;
            private CharSequence htmlbody;
            private String twitterBody;
            private String facebookBody;
    
            public ShareHelper(Activity context, String subject, String textbody, CharSequence htmlbody, String twitterBody, String facebookBody) {
                this.mContext = context;
    
                this.subject = subject;
                this.textbody = textbody;
                this.htmlbody = htmlbody;
                this.twitterBody = twitterBody;
                this.facebookBody = facebookBody;
            }
    
            @SuppressLint("NewApi")
            public void share() {
    
                this.mInflater = LayoutInflater.from(mContext);
    
                final Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
    
                sendIntent.setType(HTTP.PLAIN_TEXT_TYPE);
                plainTextActivities = mContext.getPackageManager().queryIntentActivities(sendIntent, 0);
    
                if (plainTextActivities.size() > 0) { 
                    htmlActivitiesPackages = new HashSet();
                    sendIntent.setType("text/html");
                    final List htmlActivities = mContext.getPackageManager().queryIntentActivities(sendIntent, 0);
                    for (ResolveInfo resolveInfo : htmlActivities) {
                        htmlActivitiesPackages.add(resolveInfo.activityInfo.packageName);
                    }
    
                    mAdapter = new ShareIntentAdapter();
    
                    final View chooserView = mInflater.inflate(R.layout.dialog_share_us_chooser, null);
                    mGrid = (GridView) chooserView.findViewById(R.id.resolver_grid);
                    mGrid.setAdapter(mAdapter);
                    mGrid.setOnItemClickListener(this);
    
                    mMaxColumns = mContext.getResources().getInteger(R.integer.maxResolverActivityColumns);
                    mGrid.setNumColumns(Math.min(plainTextActivities.size(), mMaxColumns));
    
                    AlertDialog.Builder builder;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                        builder = new AlertDialog.Builder(mContext, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
                    } else {
                        builder = new AlertDialog.Builder(mContext);
                    }
                    builder.setTitle(R.string.text_share_us_title);
                    builder.setView(chooserView);
    
                    mDialog = builder.create();
                    mDialog.show();
                } else {
                    Toast.makeText(mContext, "No social apps installed to share ChurchLink!", Toast.LENGTH_LONG).show();
                }
            }
    
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
    
                ResolveInfo info = (ResolveInfo) mAdapter.getItem(position);
    
                Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TITLE, subject);
                intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    
                if (info.activityInfo.packageName.contains("facebook")) {
                    intent.putExtra(Intent.EXTRA_TEXT, facebookBody);
    
                } else if (info.activityInfo.packageName.contains("twitter")) {
                    intent.putExtra(Intent.EXTRA_TEXT, twitterBody);
    
                } else if (htmlActivitiesPackages.contains(info.activityInfo.packageName)) {
                    intent.putExtra(Intent.EXTRA_TEXT, htmlbody);
    
                } else {
                    intent.putExtra(Intent.EXTRA_TEXT, textbody);
                }
                Log.d(TAG, info.activityInfo.packageName);
                ((Activity) mContext).startActivity(intent);
    
                mDialog.dismiss();
            }
    
            public class ShareIntentAdapter extends BaseAdapter {
    
                public ShareIntentAdapter() {
                    super();
                }
    
                @Override
                public int getCount() {
                    return plainTextActivities != null? plainTextActivities.size() : 0;
                }
    
                @Override
                public ResolveInfo getItem(int position) {
                    return plainTextActivities.get(position);
                }
    
                @Override
                public long getItemId(int position) {
                    return position;
                }
    
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View view;
                    if (convertView == null) {
                        view = mInflater.inflate(R.layout.griditem_share_us, parent, false);
                    } else {
                        view = convertView;
                    }
                    bindView(view, plainTextActivities.get(position));
                    return view;
                }
    
                private final void bindView(View view, ResolveInfo info) {
                    TextView text = (TextView)view.findViewById(android.R.id.text1);
                    ImageView icon = (ImageView)view.findViewById(android.R.id.icon);
    
                    text.setText(info.activityInfo.applicationInfo.loadLabel(mContext.getPackageManager()).toString());
                    icon.setImageDrawable(info.activityInfo.applicationInfo.loadIcon(mContext.getPackageManager()));
                }
            }
        }
    

    Seems like some people wants the layout files too. Here are they:

    dialog_share_us_chooser.xml

    
    
    

    griditem_share_us.xml (from own AOSP project):

    
    
    
    
    
        
        
    
        
        
    
    
    

提交回复
热议问题