Android 常见弹框样式以及第三方分享实现

private PopupWindow popupWindow;

private View popupView;

private String ids="https://www.baidu.com/";

// 这是一个点击事件,大多是分享的那个图标 ,searchImage是一个控件

searchImage.setOnClickListener(v -> {
 
  
initPopupWindow();
//设置popupWindow显示的位置 现在是显示在窗体的最下面的 可以根据情况自己定义
popupWindow .showAtLocation( popupView , Gravity. BOTTOM , 0 , 0 );
backgroundAlpha( 0.5f );
});
//主要第三方分享的实现
private void initPopupWindow() {
    /**
     * 实例popupWindow对象
     */
    popupView = View.inflate(this, R.layout.pop_share, null);
    popupWindow = new PopupWindow(popupView, GridLayout.LayoutParams.MATCH_PARENT, 
  GridLayout.LayoutParams.WRAP_CONTENT);
    //设置popupWindow中的item可以被点击,这句话是必须要添加的
    popupWindow.setFocusable(true);
    popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
    popupView.findViewById(R.id.text_cancel).setOnClickListener(v -> {
        popupWindow.dismiss();
    });
    //qq分享
    popupView.findViewById(R.id.image_qq).setOnClickListener(v -> {
        popupWindow.dismiss();
    });
    //微信分享
    popupView.findViewById(R.id.image_wechar).setOnClickListener(v -> {
        if (Build.VERSION.SDK_INT >= 23) {
        String[] mPermissionList = new String[]{WRITE_EXTERNAL_STORAGE, 
        ACCESS_FINE_LOCATION, CALL_PHONE, READ_LOGS, READ_PHONE_STATE, 
        READ_EXTERNAL_STORAGE, SET_DEBUG_APP,SYSTEM_ALERT_WINDOW, 
        GET_ACCOUNTS, WRITE_APN_SETTINGS};
     ActivityCompat.requestPermissions(this, mPermissionList, 123);
    }  
    UMImage thumb = new UMImage(ShareActivity.this, R.mipmap.sharemm);
       UMWeb web = new UMWeb(ids);   //ids是要分享出去的网址
        web.setTitle("啊哈");//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription("我就是我");//描述

        new ShareAction(ShareActivity.this)
                .setPlatform(SHARE_MEDIA.WEIXIN)
                .withMedia(web)
                .setCallback(shareListener)
                .share();
        popupWindow.dismiss();
    });
    //朋友圈分享
    popupView.findViewById(R.id.image_friend).setOnClickListener(v -> {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{WRITE_EXTERNAL_STORAGE, ACCESS_FINE_LOCATION, 
      CALL_PHONE,READ_LOGS,READ_PHONE_STATE, READ_EXTERNAL_STORAGE, SET_DEBUG_APP, 
      SYSTEM_ALERT_WINDOW, GET_ACCOUNTS, WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }
        UMImage thumb = new UMImage(ShareActivity.this, R.mipmap.sharemm);
        UMWeb web = new UMWeb(ids);   //ids是要分享出去的网址
        web.setTitle("啊哈");//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription("我就是我");//描述

        new ShareAction(ShareActivity.this)
                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                .withMedia(web)
                .setCallback(shareListener)
                .share();
        popupWindow.dismiss();
    });
    //微博分享
    popupView.findViewById(R.id.image_sina).setOnClickListener(v -> {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{WRITE_EXTERNAL_STORAGE, ACCESS_FINE_LOCATION, 
      CALL_PHONE,READ_LOGS, READ_PHONE_STATE, READ_EXTERNAL_STORAGE, SET_DEBUG_APP, 
      SYSTEM_ALERT_WINDOW,GET_ACCOUNTS, WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }
        UMImage thumb = new UMImage(ShareActivity.this, R.mipmap.sharemm);
        UMWeb web = new UMWeb(ids);  //ids是要分享出去的网址
        web.setTitle("啊哈");//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription("我就是我");//描述

        new ShareAction(ShareActivity.this)
                .setPlatform(SHARE_MEDIA.SINA)
                .withMedia(web)
                .setCallback(shareListener)
                .share();
        popupWindow.dismiss();
    });
    //设置PopupWindow的背景
    //如果不设置背景,会导致无论是点击外部区域还是Back键都无法dismiss掉popupWindow
    ColorDrawable dw = new ColorDrawable(0xb0000000);
    popupWindow.setBackgroundDrawable(dw);
}
/**
 * 设置添加屏幕的背景透明度
 *
 * @param bgAlpha
 */
public void backgroundAlpha(float bgAlpha) {
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.alpha = bgAlpha; //0.0-1.0
    getWindow().setAttributes(lp);
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.alpha = 1f;
            getWindow().setAttributes(lp);
        }
    });
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}

final UMShareListener shareListener = new UMShareListener() {
    /**
     * @descrption 分享开始的回调
     * @param platform 平台类型
     */
    @Override
    public void onStart(SHARE_MEDIA platform) {

    }

    /**
     * @descrption 分享成功的回调
     * @param platform 平台类型
     */
    @Override
    public void onResult(SHARE_MEDIA platform) {
        Toast.makeText(ShareActivity.this, "分享成功", Toast.LENGTH_LONG).show();
    }

    /**
     * @descrption 分享失败的回调
     * @param platform 平台类型
     * @param t 错误原因
     */
    @Override
    public void onError(SHARE_MEDIA platform, Throwable t) {
        Toast.makeText(ShareActivity.this, "分享失败,稍候重试" + t.getMessage(), Toast.LENGTH_LONG).show();
    }

    /**
     * @descrption 分享取消的回调
     * @param platform 平台类型
     */
    @Override
    public void onCancel(SHARE_MEDIA platform) {
        Toast.makeText(ShareActivity.this, "用户已取消", Toast.LENGTH_LONG).show();
    }

};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}

//其中123是requestCode,可以根据这个code判断,用户是否同意了授权。如果没有同意,可以根据回调进行相应处理
@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {

}
//在style里面添加的内容
<style name="mypopwindow_anim_style">
    
    <item name="android:windowEnterAnimation">@anim/popu_showitem>
    
    <item name="android:windowExitAnimation">@anim/popu_hideitem>
style>

//友盟第三方分享集成

比较重要的是QQ、微信、微博的jar包要导入进来,还有友盟的第三方jar包。

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        UMShareAPI.get(this);
        PlatformConfig.setWeixin("wx832cd1fe83b16b22", "4a2a788792764010ff565ce5f3a0d6aa");
        PlatformConfig.setQQZone("", "");
        PlatformConfig.setSinaWeibo("1703037997", "a3a85ef61930de16df07cc34a105d0e5", 
    "http://sns.whalecloud.com");
    }
}

//在清单里面配置

<meta-data
    android:name="UMENG_APPKEY"
    android:value="5a431851b27b0a03b400000e" />

<activity
    android:name="com.umeng.qq.tencent.AuthActivity"
    android:launchMode="singleTask"
    android:noHistory="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data android:scheme="tencent100424468" />
    intent-filter>
activity>
<activity
    android:name="com.umeng.qq.tencent.AssistActivity"
    android:configChanges="orientation|keyboardHidden|screenSize"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />


<activity
    android:name=".wxapi.WXEntryActivity"
    android:exported="true"
    android:label="内容"
    android:launchMode="singleTop">

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:scheme="wx832cd1fe83b16b22" />
    intent-filter>
activity>
<activity
    android:name=".wxapi.WXPayEntryActivity"
    android:exported="true"
    android:launchMode="singleTop"
    android:theme="@style/Transparent" />
<activity
    android:name="com.umeng.socialize.media.WBShareCallBackActivity"
    android:configChanges="keyboardHidden|orientation"
    android:exported="false"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

<activity
    android:name="com.sina.weibo.sdk.share.WbShareTransActivity"
    android:launchMode="singleTask"
    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
    <intent-filter>
        <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />

        <category android:name="android.intent.category.DEFAULT" />
    intent-filter>
activity>   

问题总结:

集成时也是遇到了一些问题的,但是微信一直调不起来。报的错在网上也搜了一些,最后重新配置检查,还是没有找到问题的原因。看到一篇帖子说的有可能是签名以前配置错误,然后微信本身有缓存的机制,把错误的签名给缓存到本地了。我想试试吧。没想到真的是,我就在杀毒软件里面,把微信的数据全部清空了。登录了微信之后去调分享。神奇的可以了。这也是遇到的很奇葩的问题。和各位分享一下,要是各位也是这样,配置的都很好,该写的也都写了,但就是不能调起第三方。那就试试把微信的本地数据给清空了。有可能就可以了。就分享这么多,希望对你们有一些帮助!


你可能感兴趣的:(Android 常见弹框样式以及第三方分享实现)