admin 管理员组

文章数量: 887006

【android】 微信分享详解(分享到朋友和朋友圈)+ PopupWindow的使用和分析

一. 微信分享的实现:

1.到微信开放平台   创创建应用申请AppID

2.下载签名生成工具,对签名不了解的自行百度,这里不做说明。


下面是简单的微信分享代码:

首先看一下包结构图


MainActivity:

public class MainActivity extends Activity {private static final String appid = "wx86b3d972e5ddd153";private IWXAPI wxApi;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);wxApi = WXAPIFactory.createWXAPI(this, appid);  wxApi.registerApp(appid); }

 private void share(int flag){WXWebpageObject webpage = new WXWebpageObject();webpage.webpageUrl = "";WXMediaMessage msg = new WXMediaMessage(webpage);msg.title = "test_myblog";msg.description = "test_myblog";//这里替换一张自己工程里的图片资源Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.drawable.rain);msg.setThumbImage(thumb);SendMessageToWX.Req req = new SendMessageToWX.Req();req.transaction =buildTransaction("webpage");req.message = msg;req.scene = flag==0?SendMessageToWX.Req.WXSceneSession:SendMessageToWX.Req.WXSceneTimeline;boolean fla = wxApi.sendReq(req);System.out.println("fla="+fla);}private String buildTransaction(final String type) {return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();}

WXEntryActivity:

package rain.share.wxapi;import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;public class WXEntryActivity extends Activity implements IWXAPIEventHandler {private IWXAPI api;// IWXAPI 是第三方app和微信通信的openapi接口@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);api = WXAPIFactory.createWXAPI(this, "wx86b3d972e5ddd153", false);api.handleIntent(getIntent(), this);}@Overridepublic void onReq(BaseReq arg0) {}@Overridepublic void onResp(BaseResp resp) {switch (resp.errCode) {case BaseResp.ErrCode.ERR_OK:Toast.makeText(getApplicationContext(), "分享成功", Toast.LENGTH_LONG).show();System.out.println("success");this.finish();// 分享成功break;case BaseResp.ErrCode.ERR_USER_CANCEL://分享取消Toast.makeText(getApplicationContext(), "分享取消", Toast.LENGTH_LONG).show();System.out.println("ERR_USER_CANCEL");this.finish();break;case BaseResp.ErrCode.ERR_AUTH_DENIED:Toast.makeText(getApplicationContext(), "分享拒绝", Toast.LENGTH_LONG).show();System.out.println("ERR_AUTH_DENIED");this.finish();//分享拒绝break;}}
}  

微信分享朋友、朋友圈就这么简单....

最后我会贴出源码和签名文件的下载地址,及已经申请成功的appid号,供大家方便测试...


接下里是popWindow的使用:

1.主界面就是一个button按钮,就不填代码了.

2.popWindow界面搭建share_popwindow.xml

<LinearLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="8dp"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/view_border"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="30dp"android:gravity="center"android:text="分享"android:textSize="25dp" /><Viewandroid:layout_width="match_parent"android:layout_height="0.5dp"android:layout_margin="10dp"android:background="@android:color/holo_orange_light" /><Buttonandroid:id="@+id/first"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="5dp"android:background="@android:color/holo_blue_light"android:text="微信好友" /><Buttonandroid:id="@+id/second"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:background="@android:color/holo_blue_light"android:text="微信朋友圈" /></LinearLayout></LinearLayout>

3.我们这里实现popWindow从下往上出来,再点击popWindow外面,popWindow又从上往下消失

在res/下新建一个文件夹anim,进而anim下新建两个xml文件

pophidden_anim.xml    ;   popshow_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=""><!-- 动画效果->移动动画效果  --><translateandroid:duration="1000"android:fromYDelta="0"android:toYDelta="50%p" /><!-- 动画效果->渐变透明度动画效果   --><alphaandroid:duration="1000"android:fromAlpha="1.0"android:toAlpha="0.0" />
</set><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=""><translateandroid:duration="1000"android:fromYDelta="100%p"android:toYDelta="0" /><alphaandroid:duration="1000"android:fromAlpha="0.0"android:toAlpha="1.0" />
</set>

4.在values/styles.xml加入以下代码:

    <!--  sharePopWindow --><style name="share_popWindow_anim_style"><item name="android:windowEnterAnimation">@anim/popshow_anim</item><!-- 指定显示的动画xml --><item name="android:windowExitAnimation">@anim/pophidden_anim</item><!-- 指定消失的动画xml --></style>

5.接下来就是activity的代码了(注释的很详细了)...

/**
 * 显示popupWindow
 */
private void showPopWindow() {//获取view
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.share_popwindow, null);

    //获取宽高
    popupWindow = new PopupWindow(view,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT);

    //设置popWindow弹出窗体可点击
    popupWindow.setFocusable(true);

    // 实例化一个ColorDrawable颜色为半透明
    ColorDrawable dw = new ColorDrawable(0xb0000000);
    popupWindow.setBackgroundDrawable(dw);

    // 设置popWindow的显示和消失动画
    popupWindow.setAnimationStyle(R.style.share_popWindow_anim_style);

    //在底部显示
    popupWindow.showAtLocation(MainActivity.this.findViewById(R.id.button),
            Gravity.BOTTOM,0,0);

    Button first = (Button) view.findViewById(R.id.first);
    Button second = (Button) view.findViewById(R.id.second);
    first.setOnClickListener(new View.OnClickListener() {@Override
        public void onClick(View v) {share(0);
            Toast.makeText(getApplicationContext(),"微信好友分享",Toast.LENGTH_LONG).show();
        }});
    second.setOnClickListener(new View.OnClickListener() {@Override
        public void onClick(View v) {share(1);
            Toast.makeText(getApplicationContext(),"微信朋友圈分享",Toast.LENGTH_LONG).show();
        }});

    //popWindow消失监听方法
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {@Override
        public void onDismiss() {System.out.println("popWindow消失");
        }});
}

好了,到此结束...大神勿喷...

源代码+打包签名文工具+打包签名密码都在一起...

代码下载地址:微信分享详解(分享到朋友和朋友圈)+ PopupWindow的使用和分析

本文标签: android 微信分享详解(分享到朋友和朋友圈) PopupWindow的使用和分析