首先介绍一下最简单最常用的分享方式,就是先获取系统里有分享功能的app列表,然后用intent传输一些值过去


下面是获取系统中可分享的app的代码

private List<ResolveInfo> activityList;
 private void init() {        PackageManager pm = mContext.getPackageManager();        Intent clipboardIntent = new Intent(Intent.ACTION_ALL_APPS);        clipboardIntent.setType("text/plain");               Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);        sharingIntent.setType("text/plain");        activityList = pm.queryIntentActivities(sharingIntent, 0);        if(activityList==null)return;        for (int i = 0; i < activityList.size(); i++) {            JLogUtils.i("Alex","可分享的app是"+activityList.get(i).activityInfo.packageName);        }    }
这样就获得了一个装有多个app调用的List

/**     * 显示可以用于分享的app的对话框列表     */    public void show() {        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);        sharingIntent.setType("text/plain");        ShareIntentListAdapter objShareIntentListAdapter = new ShareIntentListAdapter((Activity) mContext, activityList.toArray());        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);        builder.setTitle("Share your link");        builder.setAdapter(objShareIntentListAdapter, new DialogInterface.OnClickListener() {//建立一个listView状的弹出对话框            public void onClick(DialogInterface dialog, int item) {                JLogUtils.i("Alex", "点击了第" + item + "个");                ResolveInfo info = (ResolveInfo) activityList.get(item);                JLogUtils.i("Alex", "点击的项目是" + info.activityInfo.packageName + "  class是" + info.activityInfo.name);                // if email shared by user                if (info.activityInfo.packageName.contains("facebook")) {                    shareFacebook();//faceBook SDK有自己专门的分享方法                    return;                }                // start respective activity                Intent intent = new Intent(android.content.Intent.ACTION_SEND);                intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);                intent.setType("text/plain");//除了facebook以外,其他的只发送文本,只把title和content发送出去                if (!TextUtils.isEmpty(title)) {                    JLogUtils.i("Alex", "title是" + title);                    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);//把标题发过去                }                JLogUtils.i("Alex", "content是" + content);                intent.putExtra(android.content.Intent.EXTRA_TEXT, content);//把内容发过去                mContext.startActivity(intent);//这里有一个传来的环境变量            }// end onClick        });        AlertDialog alert = builder.create();        alert.setCancelable(true);        alert.setCanceledOnTouchOutside(true);        alert.show();    }
附适配器的写法

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;import com.imaginato.qravedconsumer.widget.CustomTextView;import com.qraved.app.R;public class ShareIntentListAdapter extends ArrayAdapter{    private final Activity context;    Object[] items;    public ShareIntentListAdapter(Activity context,Object[] items) {        super(context, R.layout.layout_shared, items);        this.context = context;        this.items = items;    }// end HomeListViewPrototype    class ShareHolder{        CustomTextView shareName;        ImageView imageShare;    }    @Override    public View getView(int position, View view, ViewGroup parent) {        ShareHolder hodler=null;        if(view==null){            LayoutInflater inflater = context.getLayoutInflater();            view=  inflater.inflate(R.layout.layout_shared, null, true);            hodler=new ShareHolder();            hodler.imageShare= (ImageView) view.findViewById(R.id.shareImage);            hodler.shareName= (CustomTextView) view.findViewById(R.id.shareName);            view.setTag(hodler);        }else{            hodler= (ShareHolder) view.getTag();        }        // set native name of App to share        hodler.shareName.setText(((ResolveInfo) items[position]).activityInfo.applicationInfo.loadLabel(context.getPackageManager()).toString());        // share native image of the App to share        hodler.imageShare.setImageDrawable(((ResolveInfo) items[position]).activityInfo.applicationInfo.loadIcon(context.getPackageManager()));        return view;    }// end getView}// end main onCreate

附,list单个布局的写法

<?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="wrap_content"    android:paddingTop="10dp"    android:paddingBottom="10dp"    android:paddingLeft="15dp"    android:paddingRight="15dp"    android:background="@android:color/white" >    <ImageView        android:id="@+id/shareImage"        android:layout_width="40dip"        android:layout_height="40dip"        android:layout_centerVertical="true"        />    <TextView        android:id="@+id/shareName"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_marginLeft="10dp"        android:layout_toRightOf="@+id/shareImage"        android:textAppearance="?android:attr/textAppearanceMedium"        android:textColor="@color/black666666"        imaginato:textFont="openSansBold"         /></RelativeLayout>

下面写一下facebookshare的方法


/**     * 配置要发到facebook的内容     */    private void shareFacebook() {        String link = webUrl;        String picture = (imageUrl == null ? "" : imageUrl);        String applicationname = mContext.getResources().getString(R.string.app_name);        JShareUtils.FacebookStoryEntity entity = new JShareUtils.FacebookStoryEntity();        JLogUtils.i("Alex","要分享到facebook的entity是"+entity);        entity.setLink(link);        entity.setApplicationName(applicationname);        entity.setDescription(facebookDesc);        entity.setName(title);        entity.setCaption("see now on Qraved");        if (imageUrl != null) {            entity.setPicture(picture);        }        JShareUtils.publishFacebookStoryByNativeApp((Activity) mContext, entity, shareHandler);    }
 public static void publishFacebookStoryByNativeApp(Activity activity, FacebookStoryEntity entity, Handler handler) {        if (activity == null || entity == null) {            callBackByHandler(handler, HANDLER_WHAT_FACEBOOK_ERROR_INIT, null);            return;        }        try {            String link = entity.getLink();            String applicationname = entity.getApplicationName();            String description = entity.getDescription();            String name = entity.getName();            String caption = entity.getCaption();            String picture = entity.getPicture();            try {                FacebookDialog.ShareDialogBuilder shareDialogBuilder = new FacebookDialog.ShareDialogBuilder(activity);//facebook sdk自己的类真的很好用                if (link != null) {                    shareDialogBuilder.setLink(link);                }                if (applicationname != null) {                    shareDialogBuilder.setApplicationName(applicationname);                }                if (description != null) {                    shareDialogBuilder.setDescription(description);                }                if (name != null) {                    shareDialogBuilder.setName(name);                }                if (caption != null) {                    shareDialogBuilder.setCaption(caption);                }                if (picture != null) {                    shareDialogBuilder.setPicture(picture);                }                if (shareDialogBuilder.canPresent()) {                    FacebookDialog shareDialog = shareDialogBuilder.build();                    shareDialog.present();                } else {                    callBackByHandler(handler, HANDLER_WHAT_FACEBOOK_ERROR_UNINSTALLED, null);                }            } catch (FacebookException ex) {                if (ex != null) {                    ex.printStackTrace();                }                callBackByHandler(handler, HANDLER_WHAT_FACEBOOK_ERROR_NOINTERNET, null);            }        } catch (Exception e) {            callBackByHandler(handler, HANDLER_WHAT_FACEBOOK_ERROR_NOINTERNET, null);        }    }

 public static class FacebookStoryEntity implements Serializable {        private static final long serialVersionUID = -1368296115091129534L;        private String link, applicationName, description, name, caption,                picture;        public String getLink() {            return link;        }        public void setLink(String link) {            this.link = link;        }        public String getApplicationName() {            return applicationName;        }        public void setApplicationName(String applicationName) {            this.applicationName = applicationName;        }        public String getDescription() {            return description;        }        public void setDescription(String description) {            this.description = description;        }        public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }        public String getCaption() {            return caption;        }        public void setCaption(String caption) {            this.caption = caption;        }        public String getPicture() {            return picture;        }        public void setPicture(String picture) {            this.picture = picture;        }          }

facebook的sdk

dependencies {    compile project(':libraries:facebook')  }
需要引入一个项目当库,facebook项目下载地址:http://download.csdn.net/detail/lvshaorong/9396080

ps:这个库同时也支持facebook的登录功能



更多相关文章

  1. android style中出现自定义属性时的写法
  2. Android(安卓)各种功能代码收集
  3. 开发中最常用的GitHub上 优秀的 Android(安卓)开源项目整理(精品)
  4. Android(安卓)Studio3.4新功能和改进
  5. To Do List
  6. 【Android(安卓)Developers Training】 1. 创建一个Android项目
  7. Android调用手机新浪微博客户端分享
  8. Android(安卓)studio 使用git提交项目到oschina
  9. Android(安卓)SipDemo项目实现SIP协议

随机推荐

  1. Android(安卓)警告提示:Activity has leak
  2. Android上下文服务管理查询过程
  3. Android(安卓)UI设计:ImageView
  4. android,java实现NanoHTTPD 轻量级 服务器
  5. Android在shell环境下运行linux命令
  6. Android(安卓)自定义带圆角的dialog
  7. Android中Activity状态的保存和恢复:onSav
  8. Android之自定义ContentProvider详解
  9. android Toast
  10. android模拟器发送短信和打电话