Today I want to write about the sharing of alliances. This period of time is useful for the project. Here is a brief introduction.
Links to the Alliance's official website: Click Open Link
First, to WeChat QQ, Weibo Open Platform Add Applications, Get key, List the links below:
QQ Open Platform Wechat Open Platform Microblog Open Platform
To register on the Alliance's official website, add an application to get the only key, and download the SDK shared by the Alliance, we need to introduce it into our own project. I only used Weixin, QQ, Weibo, only selected some functions.
Add the corresponding res resource file to your project, and at this point, the preparation is complete.
Next, we begin to implement the sharing function. Alliance sharing has its own sharing interface, which can be used directly. The code is in the SDK integration document. Here we focus on using only the API provided by sharing to draw the sharing interface for our own projects.
Design sketch:
Click the Share button and a Share Diaog appears at the bottom of the screen.
The layout is very simple, no code to paste, write a ShareDIalog class inherits Dialog, introduces the layout file, transparent background settings, add an animation that appears and disappears.
-
public class ShareDialog extends Dialog {
-
private onClickback callback;
-
-
public ShareDialog(Context context, onClickback callback) {
-
this(context, R.layout.share_dialog, R.style.my_dialog_style,
-
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
-
this.callback = callback;
-
}
-
-
public ShareDialog(final Context context, int layout, int style, int width,
-
int height) {
-
super(context, style);
-
setContentView(layout);
-
setCanceledOnTouchOutside(true);
-
-
WindowManager.LayoutParams lp = getWindow().getAttributes();
-
lp.width = width;
-
lp.height = height;
-
getWindow().setAttributes(lp);
-
-
setListener();
-
}
-
-
-
private void setListener() {
-
findViewById(R.id.tv_wx).setOnClickListener(
-
new android.view.View.OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
callback.onShare(1);
-
dismiss();
-
}
-
});
-
findViewById(R.id.tv_wxp).setOnClickListener(
-
new android.view.View.OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
callback.onShare(2);
-
dismiss();
-
}
-
});
-
findViewById(R.id.tv_wb).setOnClickListener(
-
new android.view.View.OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
callback.onShare(3);
-
dismiss();
-
}
-
});
-
findViewById(R.id.tv_qq).setOnClickListener(
-
new android.view.View.OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
callback.onShare(4);
-
dismiss();
-
}
-
});
-
findViewById(R.id.tv_qqz).setOnClickListener(
-
new android.view.View.OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
callback.onShare(5);
-
dismiss();
-
}
-
});
-
findViewById(R.id.tv_cancal).setOnClickListener(
-
new android.view.View.OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
-
dismiss();
-
}
-
});
-
}
-
-
@Override
-
public void show() {
-
super.show();
-
/ Set up dialog Display animation
-
getWindow().setWindowAnimations(R.style.dialogWindowAnim);
-
-
getWindow().setGravity(Gravity.BOTTOM);
-
}
-
-
public interface onClickback {
-
-
abstract void onShare(int id);
-
}
-
}
anim animation files can be used directly by allies:
styles:
-
<resources>
-
<style name="dialogWindowAnim" parent="android:Animation" mce_bogus="1">
-
<item name="android:windowEnterAnimation">@anim/umeng_socialize_slide_in_from_bottom</item>
-
<item name="android:windowExitAnimation">@anim/umeng_socialize_slide_out_from_bottom</item>
-
</style>
-
</resources>
The window enters the animation umeng_socialize_slide_in_from_bottom.xml:
-
<?xml version="1.0" encoding="utf-8"?>
-
<translate xmlns:android="http://schemas.android.com/apk/res/android"
-
android:duration="300"
-
android:fromXDelta="0.0%"
-
android:fromYDelta="100.0%"
-
android:toXDelta="0.0%"
-
android:toYDelta="0.0%" />
Window disappearance animation umeng_socialize_slide_out_from_bottom.xml:
-
<?xml version="1.0" encoding="utf-8"?>
-
<translate xmlns:android="http://schemas.android.com/apk/res/android"
-
android:duration="300"
-
android:fromXDelta="0.0%"
-
android:fromYDelta="0.0%"
-
android:toXDelta="0.0%"
-
android:toYDelta="100.0%" />
The click of the share button is implemented in MainActivity:
-
public class MainActivity extends Activity {
-
-
private Button btn_share;
-
final UMSocialService mController = UMServiceFactory
-
.getUMSocialService("com.umeng.share");
-
-
private String title = "share", content = "Share content...",
-
url = "http://www.baidu.com";
-
SnsPostListener mSnsPostListener = null;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
btn_share = (Button) findViewById(R.id.btn_share);
-
-
btn_share.setOnClickListener(new OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
-
openShareDialog();
-
}
-
-
});
-
mSnsPostListener = new SnsPostListener() {
-
@Override
-
public void onStart() {
-
}
-
-
@Override
-
public void onComplete(SHARE_MEDIA platform, int eCode,
-
SocializeEntity entity) {
-
if (eCode == 200) {
-
Toast.makeText(MainActivity.this, "Sharing Success",
-
Toast.LENGTH_SHORT).show();
-
}
-
}
-
};
-
mController.registerListener(mSnsPostListener);
-
-
init();
-
-
initShare();
-
}
-
-
private void openShareDialog() {
-
new ShareDialog(MainActivity.this, new onClickback() {
-
-
@Override
-
public void onShare(int id) {
-
switch (id) {
-
case 1:
-
shareToMedia(SHARE_MEDIA.WEIXIN);
-
break;
-
case 2:
-
shareToMedia(SHARE_MEDIA.WEIXIN_CIRCLE);
-
break;
-
case 3:
-
shareToMedia(SHARE_MEDIA.SINA);
-
break;
-
case 4:
-
shareToMedia(SHARE_MEDIA.QQ);
-
break;
-
case 5:
-
shareToMedia(SHARE_MEDIA.QZONE);
-
break;
-
}
-
}
-
}).show();
-
}
-
-
private void shareToMedia(SHARE_MEDIA share_MEDIA) {
-
mController.postShare(MainActivity.this, share_MEDIA, snsPostListener());
-
-
}
-
-
private SnsPostListener snsPostListener() {
-
return mSnsPostListener;
-
}
-
-
-
-
-
private void init() {
-
mController.setShareContent(title);
-
mController.setShareMedia(new UMImage(MainActivity.this,
-
R.drawable.ic_launcher));
-
mController.getConfig().closeToast();
-
String appID = "Wechat Application id";
-
String appSecret = "########################";
-
-
UMWXHandler wxHandler = new UMWXHandler(MainActivity.this, appID,
-
appSecret);
-
wxHandler.setTitle(title);
-
wxHandler.addToSocialSDK();
-
wxHandler.showCompressToast(false);
-
-
UMWXHandler wxCircleHandler = new UMWXHandler(MainActivity.this, appID,
-
appSecret);
-
wxCircleHandler.setTitle(title);
-
wxCircleHandler.setToCircle(true);
-
wxCircleHandler.addToSocialSDK();
-
wxCircleHandler.showCompressToast(false);
-
String qqID = "QQ application id";
-
String qqSecret = "##############";
-
UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(MainActivity.this,
-
qqID, qqSecret);
-
qqSsoHandler.addToSocialSDK();
-
qqSsoHandler.setTitle(title);
-
QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(
-
MainActivity.this, qqID, qqSecret);
-
qZoneSsoHandler.addToSocialSDK();
-
SinaSsoHandler sinaSsoHandler = new SinaSsoHandler();
-
sinaSsoHandler.setShareAfterAuthorize(true);
-
sinaSsoHandler.addToSocialSDK();
-
-
}
-
-
-
-
-
private void initShare() {
-
-
WeiXinShareContent weixinContent = new WeiXinShareContent();
-
weixinContent.setShareContent(content);
-
weixinContent.setTitle(title);
-
weixinContent.setTargetUrl(url);
-
-
weixinContent.setShareImage(new UMImage(MainActivity.this,
-
R.drawable.ic_launcher));
-
mController.setShareMedia(weixinContent);
-
-
CircleShareContent circleMedia = new CircleShareContent();
-
circleMedia.setShareContent(content);
-
circleMedia.setTitle(title);
-
circleMedia.setShareImage(new UMImage(MainActivity.this,
-
R.drawable.ic_launcher));
-
circleMedia.setTargetUrl(url);
-
mController.setShareMedia(circleMedia);
-
-
QQShareContent qqShareContent = new QQShareContent();
-
qqShareContent.setShareContent(content);
-
qqShareContent.setTitle(title);
-
qqShareContent.setShareImage(new UMImage(MainActivity.this,
-
R.drawable.ic_launcher));
-
qqShareContent.setTargetUrl(url);
-
mController.setShareMedia(qqShareContent);
-
-
QZoneShareContent qzone = new QZoneShareContent();
-
qzone.setShareContent(content);
-
qzone.setTargetUrl(url);
-
qzone.setTitle(title);
-
qzone.setShareImage(new UMImage(MainActivity.this,
-
R.drawable.ic_launcher));
-
mController.setShareMedia(qzone);
-
-
SinaShareContent sinaShareContent = new SinaShareContent();
-
sinaShareContent.setShareContent(content);
-
sinaShareContent.setTargetUrl(url);
-
sinaShareContent.setTitle(title);
-
sinaShareContent.setShareImage(new UMImage(MainActivity.this,
-
R.drawable.ic_launcher));
-
mController.setShareMedia(sinaShareContent);
-
}
-
-
@Override
-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-
super.onActivityResult(requestCode, resultCode, data);
-
-
UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(
-
requestCode);
-
if (ssoHandler != null) {
-
ssoHandler.authorizeCallBack(requestCode, resultCode, data);
-
}
-
}
-
-
@Override
-
protected void onDestroy() {
-
super.onDestroy();
-
-
if (mController != null) {
-
mController.getConfig().cleanListeners();
-
}
-
}
-
}
Click the Share button to open the Share Dialog. SnsPostListener is a shared callback interface.
@Override
public void onComplete(SHARE_MEDIA platform, int eCode,
SocializeEntity entity) {
if (eCode == 200) {
Toast.makeText(MainActivity.this,'Sharing Success', Toast.LENGTH_SHORT).show();
}
}
The first parameter of onComplete, SHARE_MEDIA platform, is a shared platform. The second parameter, int eCode, is the status code returned, and 200 is successful.
When sharing, which platform is the input, such as Wechat: SHARE_MEDIA.WEIXIN
-
case 1:
-
shareToMedia(SHARE_MEDIA.WEIXIN);
-
break;
-
-
-
private void shareToMedia(SHARE_MEDIA share_MEDIA) {
-
mController.postShare(MainActivity.this, share_MEDIA, snsPostListener());
-
}
When the sharing interface is closed, you must remember to close the listener at the same time in the onDestroy method:
-
@Override
-
protected void onDestroy() {
-
super.onDestroy();
-
-
if (mController != null) {
-
mController.getConfig().cleanListeners();
-
}
-
}
Notes are written in more detail, let alone elaborate.
More detailed links to the Alliance's official website: Click Open Link
PS:
If you have a Wechat or Wechat Friendship Circle integrated into your project and you need to share callbacks accurately, you need to register the following callback Activity in Android Manifest. xml.
<activity
android:name=".wxapi.WXEntryActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:screenOrientation="portrait" />
Note that there is no need to modify it, otherwise it may cause incorrect execution of Wechat callback and affect the login and sharing function of Wechat.
Then copy the wxapi folder in the platforms/weixin directory of the SDK package to the package directory configured in your AndroidMainFest.xml file application tag, and then modify the full path of WXEntry Activity.
- Take our Demo project as an example
The package name of social_sdk_example is com.umeng.soexample, so copy the wxapi folder to com.umeng.soexample, and the complete path of WXEntryActivity is com.umeng.soexample.wxapi.WXEntryActivity.
Note WXEntry Activity. Java The complete path must be correct, otherwise the Weichat share callback can not be executed properly, and the Weichat login function can not be realized properly.
The content of the WXEntryActivity.java class can be empty, integrating the WXCallbackActivity class:
-
import com.umeng.socialize.weixin.view.WXCallbackActivity;
-
public class WXEntryActivity extends WXCallbackActivity {
-
-
}
Part of Android Manifest. XML code:
-
<uses-sdk
-
android:minSdkVersion="10"
-
android:targetSdkVersion="19" />
-
-
<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.READ_LOGS" />
-
<uses-permission android:name="android.permission.CALL_PHONE" />
-
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
-
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
-
<uses-permission android:name="android.permission.GET_TASKS" />
-
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
-
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
-
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
-
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
-
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
-
-
<application
-
android:allowBackup="true"
-
android:icon="@drawable/ic_launcher"
-
android:label="@string/app_name"
-
android:theme="@style/AppTheme" >
-
-
<activity
-
android:name="com.huodao.hdphone.MainActivity"
-
android:configChanges="orientation|keyboard"
-
android:label="@string/app_name" >
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
-
<category android:name="android.intent.category.LAUNCHER" />
-
</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>
-
-
-
<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" />
-
-
<data android:scheme="tencentQQ application ID" />
-
</intent-filter>
-
</activity>
-
<activity
-
android:name="com.tencent.connect.common.AssistActivity"
-
android:screenOrientation="portrait"
-
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
-
</activity>
-
<activity
-
android:name=".wxapi.WXEntryActivity"
-
android:configChanges="keyboardHidden|orientation|screenSize"
-
android:exported="true"
-
android:screenOrientation="portrait"
-
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
-
-
-
-
-
<meta-data
-
android:name="UMENG_APPKEY"
-
android:value="Alliance application key" >
-
</meta-data>
-
</application>
When integrating alliance sharing, we must pay attention to applying for application id and KEY values in various open platforms. Packet names and signatures officially issued by applications must not make mistakes, otherwise the sharing will not be successful.
Sharing effect map:
top