Android umeng shares the detailed integration process and solutions to problems.
Recently, projects need to integrate the sharing functions of umeng with friends, QQ, and QQ. To be honest, this is also my first time. During this period, I encountered many problems, in this article, let's take a look at how I integrated umeng and shared it. What problems have I encountered and how they were solved!
In fact, integration with umeng is not very difficult. You can follow the integration documentation step by step, and the downloaded umeng SDK will be accompanied by a Demo, but there are many things in the Demo that cannot be used, so we need to selectively extract useful parts!
If you need to reprint please indicate the source: http://www.cnblogs.com/upwgh/p/6382085.html
1-Get umeng Appkey
If you have already registered an application in umeng and obtained the Appkey, you can continue to use it.
If you have not yet registered a developer account in umeng, You need to register the account first. Then, log on to your account and click Add new application. after entering the basic information of the application, the "Download SDK and add code" page is displayed. The Appkey is displayed.
2-download the SDK and decompress it to your local device.
Download the latest U-Share SDK from the umeng official website. The current version is v6.2.2.
Because the project only requires integration, circle of friends, QQ, and QQ space, only these two items are checked. The full version has the payment function. If there is no payment function, you only need to check the lite version.
Next we will go to our project.
3-Environment configuration (AndroidManifestXml)
3-1 permission Configuration
<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.READ_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
3-2 register related activities
:
<activity android:name=".wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
Full version of qq:
<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="tencent100424468" /> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="orientation|keyboardHidden|screenSize"/>
Note: The id of your project on the QQ open platform should be put here, and the preceding tencent must not be omitted. Otherwise, the QQ configuration error will be reported during operation.
3-3 umeng Appkey and related third-party key Configuration
Set umeng Appkey in AndroidManifest. xml
<meta-data android:name="UMENG_APPKEY" android:value="561cae6ae0f55abd990035bf"> </meta-data>
Configure the key of a third-party platform in Application
{
// AppId and Appkey PlatformConfig. setWeixin ("wx967daebe835fbeac", "signature"); PlatformConfig. setQQZone ("100424468", "signature ");}
Initialize the sdk in the application. It is best to put this initialization in the application program portal to prevent exceptions:
Public void onCreate () {super. onCreate (); // enable the debug mode to locate errors. For details about how to check the errors, refer to http://dev.umeng.com/social/android/quick-integration. if the error is correct, disable Config in this mode. DEBUG = true; umbench API. get (this );}
4-code implementation
4-1. decompress the downloaded SDK and import the jar package in libs under main and platforms to the project (copy and paste ...), And add dependencies.
4-2. Create a wxapi folder under the package name directory. Create an activity named WXEntryActivity that inherits WXCallbackActivity. Note that it must be under the package name path. For example, my package name is com. umeng. soexample, the configuration is as follows: (Note that if you use the lite WXCallbackActivity, the path is com. umeng. weixin. callback. WXCallbackActivity. If the full version path is com. umeng. socialize. weixin. view. WXCallbackActivity)
4-3. Function Code (only some key content is posted in the Code part. If you have any questions, please leave a message)
4-3-1 the listener will be dropped
// Share callback private UMShareListener umShareListener = new UMShareListener () {@ Override public void onResult (pai_media platform) {Log. d ("plat", "platform" + platform); ToastUtil. showToast ("shared successfully"); // if (platform. toString (). equals ("QQ") {// ToastUtil. showToast ("QQ sharing successful"); //} else if (platform. toString (). equals ("QZONE") {// ToastUtil. showToast ("QQ space sharing succeeded"); //} else if (platform. toString (). equals ("WEIXIN ")) {// ToastUtil. showToast ("shared by friends"); //} else if (platform. toString (). equals ("WEIXIN_CIRCLE") {// ToastUtil. showToast ("shared by friends"); // }}@ Override public void onError (pai_media platform, Throwable t) {ToastUtil. showToast ("sharing failed"); if (t! = Null) {Log. d ("throw", "throw:" + t. getMessage () ;}@ Override public void onCancel (pai_media platform) {ToastUtil. showToast ("Sharing canceled ");}};
This is the callback corresponding to the sharing success, failure, and cancellation, and the corresponding processing of the Sharing result (this part of code should be placed in the member variable)
4-3-2 click to share events
@ Override public void onClick (View view) {switch (view. getId () {case R. id. TV _cancle: popupWindow. dismiss (); break; case R. id. rl_common_title_exit: initPopupWindow (); // set the position popupWindow. showAtLocation (mPopView, Gravity. BOTTOM, 0, 0); backgroundAlpha (0.5f); break; case R. id. qq: new billing action (ShowWebViewActivity. this ). setPlatform (pai_media.qq ). withText (content ). withTitle (title ). withTargetUrl (linkHref ). withMedia (image ). setCallback (umShareListener ). share (); popupWindow. dismiss (); break; case R. id. weixin: new jsonaction (ShowWebViewActivity. this ). setPlatform (pai_media.weixin ). withText (content ). withTitle (title ). withTargetUrl (linkHref ). withMedia (image ). setCallback (umShareListener ). share (); popupWindow. dismiss (); break; case R. id. kongjian: new jsonaction (ShowWebViewActivity. this ). setPlatform (pai_media.qzone ). withText (content ). withTitle (title ). withTargetUrl (linkHref ). withMedia (image ). setCallback (umShareListener ). share (); popupWindow. dismiss (); break; case R. id. pengyouquan: new jsonaction (ShowWebViewActivity. this ). setPlatform (pai_media.weixin_circle ). withText (content ). withTitle (title ). withTargetUrl (linkHref ). withMedia (image ). setCallback (umShareListener ). share (); popupWindow. dismiss (); break ;}}
The red part is the content to be shared and initialized as needed.
/** \ * Share link, title, content */String linkHref = "share link"; String title = "share title! "; String content =" shared content "; // share an image private UMImage image = null;
4-3-3 Share the pop-up window
As you can see, my sharing panel is a PopupWindow
About PopupWindow I have written a related essay http://www.cnblogs.com/upwgh/p/5910261.html
The difference is that, when the pop-up window is shared, the background will change color, and when the pop-up window disappears, the background will be restored and a simple animation will be written at will.
initPopupWindow():
Private void initPopupWindow () {/*** popupWindow object of the Instance */popupWindow = new PopupWindow (mPopView, GridLayout. layoutParams. MATCH_PARENT, GridLayout. layoutParams. WRAP_CONTENT); // set the items in the popupWindow to be clicked. This sentence is the required popupWindow. setFocusable (true );
// Set the animation popupWindow. setAnimationStyle (R. style. mypopwindow_anim_style); // set the background of the PopupWindow. // if the background is not set, the popupWindow ColorDrawable dw = new ColorDrawable (0xb0000000) cannot be dismiss whether you click the external region or the Back key ); popupWindow. setBackgroundDrawable (dw );}
Pop-up animation:
<Style name = "mypopwindow_anim_style"> <! -- Specify the displayed animation xml --> <item name = "android: windowEnterAnimation"> @ anim/popu_show </item> <! -- Specify the xml for the animation to disappear --> <item name = "android: javaswexitanimation"> @ anim/popu_hide </item> </style>
Popu_show.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fromYDelta="100%p" android:toYDelta="0" /> <alpha android:duration="200" android:fromAlpha="0.0" android:toAlpha="1.0" /></set>
Popu_hide.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="200" android:fromYDelta="0" android:toYDelta="50%p" /> <alpha android:duration="200" android:fromAlpha="1.0" android:toAlpha="0.0" /></set>
Finally, set the background changes when the pop-up window is shared:
/*** Set the background transparency of the added screen * @ 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 );}
The above is the main content of this sharing function. The content may not be well written. Please forgive me. If you have any errors or do not understand it, please leave a message ~~~