代码:http://download.youkuaiyun.com/detail/huangbin95487710/9260971
依赖包模式
完整运行还需要用到以下依赖包
基于友盟分享SDK再次封装,可抛开友盟独立重写某个分享
- AndroidManifest.xml添加配置
<application android:icon="@drawable/umeng_socialize_tx_on" > <!-- ###################Umeng分享################### --> <!-- ###################添加UmengAppkey###################### --> <meta-data android:name="UMENG_APPKEY" android:value="50cc517c5270153893000013" > </meta-data> <meta-data android:name="UMENG_CHANNEL" android:value="muzhiwan" /> <!-- ###################注册SDK使用的Activity###################### --> <!-- 微信登录 --> <activity android:name=".wxapi.WXEntryActivity" android:exported="true" android:launchMode="singleTask" android:screenOrientation="portrait" > <intent-filter> <action android:name="app.general.lib.wxapi.WXEntryActivity" /> <category android:name="android.intent.category.EMBED" /> </intent-filter> </activity> <!-- 微信登录 --> <!-- 分享编辑页 --> <activity android:name="com.umeng.socialize.view.ShareActivity" android:configChanges="orientation|keyboard" android:launchMode="singleTask" android:noHistory="true" android:theme="@style/Theme.UMDialog" android:windowSoftInputMode="stateVisible|adjustResize" > </activity> <!-- ############ QQ空间和QQ SSO授权的Activity注册 ############ --> <activity android:name="com.tencent.tauth.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" /> <!-- 801291865,如果你使用的公用账号,则不需要修改;否则修改成你在QQ开放平台申请的 APP ID --> <data android:scheme="tencent801291865" /> </intent-filter> </activity> <activity android:name="com.tencent.plus.ImageActivity" /> <activity android:name="com.tencent.connect.common.AssistActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" > </activity> <!-- ###################Umeng分享################### --> <!-- ###################淘宝登录################### --> <activity android:name="com.alibaba.sdk.android.webview.BaseWebViewActivity" /> <activity android:name="com.alibaba.sdk.android.login.ui.LoginWebViewActivity" /> <activity android:name="com.alibaba.sdk.android.login.ui.QrLoginActivity" android:theme="@style/tae_sdk_login_qr_activity_style" /> <!-- ###################淘宝登录################### --> </application> <!-- ###################Umeng分享相关权限###################### --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" /> <!-- ###################Umeng分享相关权限###################### -->
- 添加混淆,详见依赖包proguard.cfg
- 需把以下的文件复制到目标项目包路径xxx.wxapi
- WXEntryActivity
package cn.whonow.whonow.wxapi; import General.Share.WXShareReceiver; import General.System.MyLog; import General.ThirdLogin.WeiXin.WeiXinLogin; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import cn.whonow.whonow.R; import com.tencent.mm.sdk.constants.ConstantsAPI; import com.tencent.mm.sdk.modelbase.BaseReq; import com.tencent.mm.sdk.modelbase.BaseResp; import com.tencent.mm.sdk.modelmsg.SendAuth; 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 { // IWXAPI 是第三方app和微信通信的openapi接口 private IWXAPI api; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initWeixin(); } private void initWeixin() { // 通过WXAPIFactory工厂,获取IWXAPI的实例 api = WXAPIFactory.createWXAPI(this, getString(R.string.thirdlogin_weixin_appid), false); boolean rigit = api .registerApp(getString(R.string.thirdlogin_weixin_appid)); MyLog.d(WXEntryActivity.class, "=======>rigit:" + rigit); boolean handle = api.handleIntent(getIntent(), this); MyLog.d(WXEntryActivity.class, "=======>handle:" + handle); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); api.handleIntent(intent, this); } @Override protected void onResume() { super.onResume(); } @Override public void onReq(BaseReq arg0) { } @Override public void onResp(BaseResp resp) { MyLog.d(WXEntryActivity.class, "=======>errCode:" + resp.errCode + "=========>getType:" + resp.getType()); if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH && resp.errCode == BaseResp.ErrCode.ERR_OK) { // 登录授权 String mCode = ((SendAuth.Resp) resp).code; // 获取 access_token WeiXinLogin.sendLoginAction(this, mCode); } else if (resp.getType() == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) { // 分享成功 WXShareReceiver.sendShareAction(this, resp.errCode); } finish(); } }
- 需要在自己的任意配置文件下重写
<string name="thirdlogin_weixin_appid">**</string>
- 示例代码
- 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ll" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#0099cc" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="8dip" > <Button android:id="@+id/test_share_image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="8dip" android:layout_marginRight="4dip" android:layout_weight="1" android:text="分享图片" /> <Button android:id="@+id/test_share_url" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="4dip" android:layout_marginRight="8dip" android:layout_weight="1" android:text="分享链接" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="8dip" > <Button android:id="@+id/test_share_div" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="8dip" android:layout_marginRight="4dip" android:layout_weight="1" android:text="重定义邮件分享" /> <Button android:id="@+id/test_share_add" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="4dip" android:layout_marginRight="8dip" android:layout_weight="1" android:text="添加功能集合" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="8dip" > <Button android:id="@+id/test_share_sever" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="8dip" android:layout_marginRight="4dip" android:layout_weight="1" android:text="服务器分享开关" /> <Button android:id="@+id/test_share_insert" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="4dip" android:layout_marginRight="8dip" android:layout_weight="1" android:text="动态插入功能" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="8dip" > <Button android:id="@+id/test_share_new" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="8dip" android:layout_marginRight="4dip" android:layout_weight="1" android:text="当前项目需要" /> <Button android:id="@+id/test_share_new_dialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="4dip" android:layout_marginRight="8dip" android:layout_weight="1" android:text="dialog形式" /> </LinearLayout> <Button android:id="@+id/test_share_txt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="只分享文字" android:layout_marginLeft="8dip" android:layout_marginRight="8dip"/> </LinearLayout>
- 自定义样式
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 自定义分享序列 --> <integer-array name="my_div_share"> <item>@string/umeng_div_weixin</item> <item>@string/umeng_div_weixin_circle</item> <item>@string/umeng_div_tencent</item> <item>@string/umeng_div_sina</item> </integer-array> <!-- 重写取消按钮样式 --> <style name="Widget.Button.UmengDiv.New" parent="@style/Widget.Button.UmengDiv"> <item name="android:text">@string/umeng_div_cancel</item> <item name="android:background">@drawable/share_btn</item> <item name="android:textSize">18sp</item> <item name="android:textColor">#FFf53e22</item> </style> <!-- 重写样式 --> <style name="MyDivShare" parent="@style/DivShare"> <item name="qq_appid">@string/qq_appid</item> <item name="qq_secret">@string/qq_appsecret</item> <item name="weixin_appid">@string/wx_appid</item> <item name="div_numcolumns">4</item> <item name="div_exit_style">@style/Widget.Button.UmengDiv.New</item> <item name="div_url">http://www.hongfu001.com</item> <item name="div_icon_default">@drawable/icon</item> <item name="div_share">@array/my_div_share</item> <item name="div_x_splace">15dip</item> <item name="div_icon_size">75</item> </style> </resources>
- 代码
- ShareActivity
package cn.whonow.whonow.share; import java.util.Map; import General.Share.Share; import General.Share.ShareBase; import General.Share.ShareResultListener; import General.Share.View.ShareItemBase; import General.System.MyLog; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import cn.whonow.whonow.R; /** * 分享 必须实现ShareResultListener接口 * * @author HuangYi QQ:95487710 */ public class ShareActivity extends Activity implements ShareResultListener { private Button mButtonImage; private Button mButtonUrl; private Button mButtonDiv; private Button mButtonAdd; private Button mButtonSever; private Button mButtonInsert; private Button mButtonNew; private Button mButtonNewDialog; private Button mButtonTxt; private ShareBase mShareBase; private Share mShare; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_share); mShare = new Share(); mShareBase = new ShareBase(ShareActivity.this, ShareActivity.this); mButtonImage = (Button) findViewById(R.id.test_share_image); mButtonImage.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mShareBase.mUrl = ""; mShareBase.mTitle = "测试"; mShareBase.mText = "测试Neoisdhosdfh拉萨到很疯狂了射发"; mShareBase.mImage = R.drawable.test_share; mShare.share(mShareBase, R.id.ll); } }); mButtonUrl = (Button) findViewById(R.id.test_share_url); mButtonUrl.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mShareBase.mUrl = "http://www.baidu.com"; mShareBase.mTitle = "测试"; mShareBase.mText = "测试Neoisdhosdfh拉萨到很疯狂了射发"; mShareBase.mImage = R.drawable.umeng_div_tencent; mShare.share(mShareBase, R.id.ll); } }); mButtonDiv = (Button) findViewById(R.id.test_share_div); mButtonDiv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ShareBase mShareBase = new ShareBase(ShareActivity.this, ShareActivity.this); mShareBase.mUrl = "http://www.baidu.com"; mShareBase.mTitle = "测试"; mShareBase.mText = "测试Neoisdhosdfh拉萨到很疯狂了射发"; mShareBase.mImage = R.drawable.umeng_div_tencent; // 重写自定义的邮件分享 mShareBase.updateNewClass(ShareDivEmail.class); mShare.share(mShareBase, R.id.ll); } }); mButtonAdd = (Button) findViewById(R.id.test_share_add); mButtonAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ShareBase mShareBase = new ShareBase(ShareActivity.this, ShareActivity.this); mShareBase.mUrl = "http://www.baidu.com"; mShareBase.mTitle = "测试"; mShareBase.mText = "测试Neoisdhosdfh拉萨到很疯狂了射发"; mShareBase.mImage = R.drawable.umeng_div_tencent; // 添加功能集合 mShareBase.updateNewClass(R.drawable.test_share, R.string.app_name, ShareAdd.class); mShare.share(mShareBase, R.id.ll); } }); mButtonSever = (Button) findViewById(R.id.test_share_sever); mButtonSever.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ShareBase mShareBase = new ShareBase(ShareActivity.this, ShareActivity.this); mShareBase.mUrl = "http://www.baidu.com"; mShareBase.mTitle = "测试"; mShareBase.mText = "测试Neoisdhosdfh拉萨到很疯狂了射发"; mShareBase.mImage = R.drawable.umeng_div_tencent; // 服务器关闭QQ与豆瓣开关 mShareBase.closeQQ(); mShareBase.closeDouBan(); mShare.share(mShareBase, R.id.ll); } }); mButtonInsert = (Button) findViewById(R.id.test_share_insert); mButtonInsert.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ShareBase mShareBase = new ShareBase(ShareActivity.this, ShareActivity.this); mShareBase.mUrl = "http://www.baidu.com"; mShareBase.mTitle = "测试"; mShareBase.mText = "测试Neoisdhosdfh拉萨到很疯狂了射发"; mShareBase.mImage = R.drawable.umeng_div_tencent; // 插入功能集合 ShareItemBase mShareItemBase = new ShareItemBase( R.drawable.test_share, R.string.app_name, ShareAdd.class); mShareBase.insertNewClass(1, mShareItemBase); mShare.share(mShareBase, R.id.ll); } }); mButtonNew = (Button) findViewById(R.id.test_share_new); mButtonNew.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 当前项目xuyao ShareBase mShareBase = new ShareBase(ShareActivity.this, R.style.MyDivShare, ShareActivity.this); mShareBase.mUrl = "http://www.baidu.com"; mShareBase.mTitle = "测试"; mShareBase.mText = "测试Neoisdhosdfh拉萨到很疯狂了射发"; mShareBase.mImage = R.drawable.umeng_div_tencent; mShare.share(mShareBase, R.id.ll); } }); mButtonNewDialog = (Button) findViewById(R.id.test_share_new_dialog); mButtonNewDialog.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ShareBase mShareBase = new ShareBase(ShareActivity.this, R.style.MyDivShare, ShareActivity.this); mShareBase.mUrl = "http://www.baidu.com"; mShareBase.mTitle = "测试"; mShareBase.mText = "测试Neoisdhosdfh拉萨到很疯狂了射发"; mShareBase.mImage = R.drawable.umeng_div_tencent; mShare.share(mShareBase); } }); mButtonTxt = (Button) findViewById(R.id.test_share_txt); mButtonTxt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ShareBase mShareBase = new ShareBase(ShareActivity.this, ShareActivity.this); mShareBase.mTitle = "测试"; mShareBase.mText = "测试Neoisdhosdfh拉萨到很疯狂了射发"; mShare.share(mShareBase); } }); } @Override public void onSucess(String mType) { // TODO Auto-generated method stub MyLog.show(this, mType + "分享成功!"); } @Override public void onError(String mError) { // TODO Auto-generated method stub MyLog.show(this, "分享失败:" + mError); } @Override public void onCancel() { // TODO Auto-generated method stub MyLog.show(this, "取消分享"); } @Override public void onSucess(String mType, Map<String, Object> arg1){ } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (mShare != null) { mShare.onActivityResult(requestCode, resultCode, data); } } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if (mShare != null) { mShare.onDestroy(); } } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); if (mShare != null) { mShare.onPause(); } } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); if (mShare != null) { mShare.onResume(); } } }
- ShareAdd
package cn.whonow.whonow.share; import General.Share.ShareUMImage; import General.System.MyLog; import android.app.Activity; import android.content.Intent; import cn.whonow.whonow.R; import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener; /** * 添加额外功能 * * @author HuangYi **/ public class ShareAdd extends ShareUMImage { public ShareAdd() { super(); // TODO Auto-generated constructor stub } public ShareAdd(Activity mContext) { super(mContext); // TODO Auto-generated constructor stub } @Override public void share(String title, String text, String url, Object mImage, SnsPostListener mListener) { // 执行自定义方式 MyLog.show(mContext, R.string.app_name); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub } @Override public String getType() { // TODO Auto-generated method stub return null; } }
- ShareDivEmail
package cn.whonow.whonow.share; import General.Share.ShareEmail; import General.System.MyLog; import android.app.Activity; import cn.whonow.whonow.R; import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener; /*** * * 自定义邮件分享 * * @author HuangYi */ public class ShareDivEmail extends ShareEmail { public ShareDivEmail() { super(); // TODO Auto-generated constructor stub } public ShareDivEmail(Activity mContext) { super(mContext); // TODO Auto-generated constructor stub } @Override public void share(String title, String text, String url, Object mImage, final SnsPostListener mListener) { // 是否调用父类封装好的方法 super.share(title, text, url, mImage, mListener); // 执行自定义方式 MyLog.show(mContext, R.string.app_name); } }
- 详细介绍