public class DialogShare extends Dialog implements android.view.View.OnClickListener{
private Context context;
private PlatformActionListener platformActionListener;
private Platform.ShareParams shareParams;
private int[] ids={R.id.ly_wechat,R.id.ly_wechat_friend,R.id.ly_qq,R.id.ly_qzone};
public DialogShare(Context context){
this(context, R.style.Theme_Dialog_From_Bottom);
}
public DialogShare(Context context,int theme){
super(context,theme);
this.context=context;
init();
initValues();
}
public PlatformActionListener getPlatFormActionListener(){
return platformActionListener;
}
public void setPlatFormActionListener(PlatformActionListener platformActionListener){
this.platformActionListener=platformActionListener;
}
private void init(){
this.setCanceledOnTouchOutside(true);
this.setCancelable(true);
}
private void initValues(){
//获取DialogShare所在的窗口
Window window=this.getWindow();
window.getDecorView().setPadding(0, 0, 0, 0);
//获取当前屏幕的参数
WindowManager.LayoutParams lp=window.getAttributes();
//宽度占满屏幕
lp.width=WindowManager.LayoutParams.MATCH_PARENT;
//高度
lp.height=WindowManager.LayoutParams.WRAP_CONTENT;
//位于底部
lp.gravity=Gravity.BOTTOM;
window.setAttributes(lp);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pop_share);
initView();
}
private void initView(){
for(int rid:ids){
this.findViewById(rid).setOnClickListener(this);
}
}
private class ShareItemClickListener implements OnItemClickListener{
private DialogShare dialogShare;
public ShareItemClickListener(DialogShare dialogShare){
this.dialogShare=dialogShare;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
share(position);
dialogShare.dismiss();
}
}
private void share(int position){
if (position==2) {
qqFriend();
}else if(position==3){
//Q空间
qqZone();
}else {
Platform plat=null;
plat=ShareSDK.getPlatform(context,getPlatform(position));
if(platformActionListener!=null){
plat.setPlatformActionListener(platformActionListener);
}
plat.share(shareParams);
}
}
/**
* 获取平台
*/
private String getPlatform(int vid){
String platform="";
switch (vid) {
case R.id.ly_wechat:
platform="WeChat";
break;
case R.id.ly_wechat_friend:
platform="WeChatMoments";
break;
case R.id.ly_qq:
platform="QQ";
break;
case R.id.ly_qzone:
platform="QZone";
break;
default:
break;
}
return platform;
}
/**
* 初始化分享内容参数
* @param shareModel
*/
public void initShareParams(BeanShareData shareModel){
if (shareModel!=null) {
Platform.ShareParams sp=new Platform.ShareParams();
sp.setShareType(Platform.SHARE_TEXT);
sp.setShareType(Platform.SHARE_WEBPAGE);
sp.setTitle(shareModel.getTitle());//设置分享标题
sp.setText(shareModel.getText());
if (!TextUtils.isEmpty(shareModel.getImageUrl())) {
sp.setImageUrl(shareModel.getImageUrl());
}
sp.setUrl(shareModel.getUrl());
if (!TextUtils.isEmpty(shareModel.getImagePath())) {
sp.setImagePath(shareModel.getImagePath());
}
shareParams=sp;
}
}
public void initShareParams_in(BeanShareData shareModel){
if (shareModel!=null) {
Platform.ShareParams sp=new Platform.ShareParams();
sp.setShareType(Platform.SHARE_TEXT);
sp.setShareType(Platform.SHARE_WEBPAGE);
sp.setTitle(shareModel.getTitle());//设置分享标题
sp.setText(shareModel.getText());
sp.setUrl(shareModel.getUrl());
sp.setImageUrl(shareModel.getImageUrl());
shareParams=sp;
}
}
/**
* 分享到QQ好友
*/
private void qqFriend(){
Platform.ShareParams sp=new Platform.ShareParams();
sp.setTitle(shareParams.getTitle());
sp.setTitleUrl(shareParams.getUrl());//标题的超链接
sp.setText(shareParams.getText());
if (!TextUtils.isEmpty(shareParams.getImageUrl())) {
sp.setImageUrl(shareParams.getImageUrl());
}
if (!TextUtils.isEmpty(shareParams.getImagePath())) {
sp.setImagePath(shareParams.getImagePath());
}
sp.setComment(context.getString(R.string.saysomething));
sp.setSite(shareParams.getTitle());
sp.setSiteUrl(shareParams.getUrl());
Platform qq=ShareSDK.getPlatform(context,"QQ");
qq.setPlatformActionListener(platformActionListener);
qq.share(sp);
}
/**
* 分享到QQ空间
*/
private void qqZone(){
Platform.ShareParams sp=new Platform.ShareParams();
sp.setTitle(shareParams.getTitle());
sp.setTitleUrl(shareParams.getUrl());//标题的超链接
sp.setText(shareParams.getText());
if (!TextUtils.isEmpty(shareParams.getImageUrl())) {
sp.setImageUrl(shareParams.getImageUrl());
}
if (!TextUtils.isEmpty(shareParams.getImagePath())) {
sp.setImagePath(shareParams.getImagePath());
}
sp.setComment(context.getString(R.string.saysomething));
sp.setSite(shareParams.getTitle());
sp.setSiteUrl(shareParams.getUrl());
Platform qZone=ShareSDK.getPlatform(context,"QZone");
//设置分享时间回调
qZone.setPlatformActionListener(platformActionListener);
qZone.share(sp);
}
@Override
public void onClick(View v) {
ShareSDK.initSDK(context);
dismiss();
switch (v.getId()) {
case R.id.ly_qq:
qqFriend();
break;
//QQ空间
case R.id.ly_qzone:
qqZone();
break;
//微信
case R.id.ly_wechat:
break;
case R.id.ly_wechat_friend:
Platform plat=null;
plat=ShareSDK.getPlatform(context,getPlatform(v.getId()));
if (plat!=null&&platformActionListener!=null) {
plat.setPlatformActionListener(platformActionListener);;
}
plat.share(shareParams);
break;
default:
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:id="@+id/ll_share"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分享"
android:textColor="@color/orange"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- 微信 -->
<LinearLayout
android:id="@+id/ly_wechat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/iconfontweixing"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="微信"
android:textColor="@color/black"/>
</LinearLayout>
<!-- 朋友圈 -->
<LinearLayout
android:id="@+id/ly_wechat_friend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/iconfontzhangshangcaifuyemianshoujiban344"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="朋友圈"
android:textColor="@color/black"/>
</LinearLayout>
<!-- QQ -->
<LinearLayout
android:id="@+id/ly_qq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/iconfontqql"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="QQ"
android:textColor="@color/black"/>
</LinearLayout>
<!-- QQ空间 -->
<LinearLayout
android:id="@+id/ly_qzone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/iconfontqqkongjian"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="QQ空间"
android:textColor="@color/black"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>