Android 友盟统计 友盟推送 友盟分享快速集成

一.友盟统计

1.首先需要友盟官网 http://mobile.umeng.com/apps注册并且添加新应用,获得Appkey

2.导入SDK,新版SDK支持Android 6.0,http://download.csdn.net/download/a704755096/9676752

如果是Android Studio:在Gradle依赖中添加:

dependencies {
   compile 'com.umeng.analytics:analytics:latest.integration'
}

如果无法正常集成请添加如下代码 :

allprojects {
repositories {
mavenCentral()
}
}
3.配置xml

<manifest……>
<uses-sdk android:minSdkVersion="8"></uses-sdk>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application ……>
……
<activity ……/>
<meta-data android:value="xxx" android:name="UMENG_APPKEY"/>
<meta-data android:value="xxx" android:name="UMENG_CHANNEL"/>
</application>    
</manifest>
4.添加代码,主入口文件调用//要发送错误统计为true

在每个Activity的onResume方法中调用 

在每个Activity的onPause方法中调用 

public void onResume() {
super.onResume();
MobclickAgent.onResume(this);
}
public void onPause() {
super.onPause();
MobclickAgent.onPause(this);
}
小技巧:6.0获取设备信息有变,当xml中的 targetSdkVersion<23时候, 可以正常获取信息(相当于跳过了6.0权限检查)。。到此基本功能已经完成……

二.友盟推送

1.首先需要友盟官网 http://push.umeng.com注册并且添加新应用,用包名添加应用时获得的AppKey和Umeng Message Secret替换掉PushDemo下的AndroidManifest.xml中的默认的AppKey和Umeng Message Secret

2.导入PushSDK,新版SDK支持Android 6.0,http://download.csdn.net/download/a704755096/9676752

PushSDK 3.0 libs默认只提供armeabi和x86两种so文件夹,若主工程中的so文件夹与PushSDK下的so文件夹不一致,则可以添加缺少的so文件夹至 PushSDK下libs,或删除PushSDK libs下多余的so文件夹,必须使PushSDK libs的so文件夹与主工程的so文件夹保持一致。

如果是Android Studio在Gradle依赖中添加:

  1. 在Application Module的build.gradle文件的dependencies下添加compile project(':PushSDK')
  2. 请确保Application Module的build.gradle文件中的applicationId与应用包名package一致。

3.配置xml把PushSDK目录下的AndroidManifest.xml中的${applicationId}替换成为自己项目的包名。

<manifest……>
<uses-sdk android:minSdkVersion="8"></uses-sdk>
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
  <uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
  <uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" />
  <uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />
  <uses-permission android:name="android.permission.RESTART_PACKAGES" />
  <uses-permission android:name="android.permission.GET_TASKS" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
- <application>
- <service android:name="com.taobao.accs.ChannelService" android:exported="true" android:process=":channel">
- <intent-filter>
  <action android:name="com.taobao.accs.intent.action.SERVICE" />
  </intent-filter>
- <intent-filter>
  <action android:name="com.taobao.accs.intent.action.ELECTION" />
  </intent-filter>
  </service>
- <service android:name="com.taobao.accs.data.MsgDistributeService" android:exported="true">
- <intent-filter>
  <action android:name="com.taobao.accs.intent.action.RECEIVE" />
  </intent-filter>
  </service>
- <receiver android:name="com.taobao.accs.EventReceiver" android:process=":channel">
- <intent-filter>
  <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
- <intent-filter>
  <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
  </intent-filter>
- <intent-filter>
  <action android:name="android.intent.action.PACKAGE_REMOVED" />
  <data android:scheme="package" />
  </intent-filter>
- <intent-filter>
  <action android:name="android.intent.action.USER_PRESENT" />
  </intent-filter>
  </receiver>
- <receiver android:name="com.taobao.accs.ServiceReceiver" android:process=":channel">
- <intent-filter>
  <action android:name="com.taobao.accs.intent.action.COMMAND" />
  </intent-filter>
- <intent-filter>
  <action android:name="com.taobao.accs.intent.action.START_FROM_AGOO" />
  </intent-filter>
  </receiver>
  <service android:name="com.taobao.accs.ChannelService$KernelService" android:process=":channel" />
- <service android:name="org.android.agoo.accs.AgooService" android:exported="true">
- <intent-filter>
  <action android:name="com.taobao.accs.intent.action.RECEIVE" />
  </intent-filter>
  </service>
- <service android:name="com.umeng.message.UmengIntentService" android:exported="true">
- <intent-filter>
  <action android:name="org.agoo.android.intent.action.RECEIVE" />
  </intent-filter>
  </service>
- <receiver android:name="com.taobao.agoo.AgooCommondReceiver" android:exported="true">
- <intent-filter>
  <action android:name="${applicationId}.intent.action.COMMAND" />
  </intent-filter>
- <intent-filter>
  <action android:name="android.intent.action.PACKAGE_REMOVED" />
  <data android:scheme="package" />
  </intent-filter>
  </receiver>
  <receiver android:name="com.umeng.message.NotificationProxyBroadcastReceiver" android:exported="false" />
- <service android:name="com.umeng.message.UmengMessageCallbackHandlerService" android:exported="false">
- <intent-filter>
  <action android:name="com.umeng.messge.registercallback.action" />
  </intent-filter>
- <intent-filter>
  <action android:name="com.umeng.message.enablecallback.action" />
  </intent-filter>
- <intent-filter>
  <action android:name="com.umeng.message.disablecallback.action" />
  </intent-filter>
- <intent-filter>
  <action android:name="com.umeng.message.message.handler.action" />
  </intent-filter>
  </service>
  <service android:name="com.umeng.message.UmengDownloadResourceService" android:exported="false" />
- <service android:name="com.umeng.message.UmengMessageIntentReceiverService" android:exported="true" android:process=":channel">
- <intent-filter>
  <action android:name="org.android.agoo.client.MessageReceiverService" />
  </intent-filter>
  </service>
- <provider android:name="com.umeng.message.provider.MessageProvider" android:authorities="${applicationId}.umeng.message" android:exported="false">
  <grant-uri-permission android:pathPattern=".*" />
  </provider>
  </application>
…… <activity ……/> <meta-data android:value="xxx" android:name="UMENG_APPKEY"/><meta-data android:value="xxx" android:name="UMENG_CHANNEL"/><meta-data android:value="xxx" android:name="UMENG_MESSAGE_SECRET"/> </application> </manifest>

4.添加代码,主入口文件调用

在工程的Application类的 onCreate() 方法中注册推送服务,无论推送是否开启都需要调用此方法:

PushAgent mPushAgent = PushAgent.getInstance(this);//mPushAgent.setDebugMode(false);//如果app对外正式发布设为false
//注册推送服务,每次调用register方法都会回调该接口
mPushAgent.register(new IUmengRegisterCallback() {

    @Override
    public void onSuccess(String deviceToken) {
        //注册成功会返回device token
    }

    @Override
    public void onFailure(String s, String s1) {

    }
});mPushAgent.setNotificationClickHandler(UmengNotificationClickHandler);//自定义点击事件监听
//mPushAgent.setMessageHandler(UHandler);//自定义消息处理,设置后不会收到默认通知
//mPushAgent.setPushIntentServiceClass(MyPushIntentService);//完全自定义通知设置,设置后不会收到默认通知
如需手动获取device token,可以调用mPushAgent.getRegistrationId()方法(需在注册成功后调用);

免打扰模式,如23:00-7:00:mPushAgent.setNoDisturbMode(23,0,7,0);//关闭免打扰模式设为0,0,0,0

在所有的Activity 的onCreate 方法或在应用的BaseActivityonCreate方法中添加:

PushAgent.getInstance(context).onAppStart();
到此基本功能已经完成……

三.友盟分享 登录

1.首先需要友盟官网 http://mobile.umeng.com/social注册并且添加新应用,获得Appkey

2.导入SDK,Android 社会化组件SDK v5.1.3(2016-6-6)http://dev.umeng.com/social/android/sdk-download

对应jar包名字对应如下:新浪精简版,不再需要.so文件

平台 完整版(老版) 精简版(新版)
main umeng_social_sdk.jar umeng_social_apiv6.0.0.jar 
umeng_social_netv6.0.0.jar 
umeng_social_viewv6.0.0.jar
qq mta-sdk-1.6.2.jar 
open_sdk_r5756_lite.jar 
SocialSDK_QQZone_3.jar
SocialSDK_umengqq.jar 
SocialSDK_QQZone_3.jar
sina SocialSDK_Sina.jar 
weiboSDKCore_3.1.4.jar 
libweibosdkcore.so
SocialSDK_umengsina.jar
微信 SocialSDK_WeiXin_1.jar 
SocialSDK_WeiXin_2.jar
SocialSDK_umengwx.jar 
SocialSDK_WeiXin_2.jar

3.配置xml

<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.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" />
<!--友盟分享编辑页-->         <activity             android:name="com.umeng.socialize.view.ShareActivity"             android:configChanges="orientation|keyboard"             android:launchMode="singleTask"             android:noHistory="true"                          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" />                 <!-- 100424468,如果你使用的公用账号,则不需要修改;否则修改成你在QQ开放平台申请的 APP ID-->                 <data android:scheme="tencent100424468" />              </intent-filter>         </activity>         <activity android:name="com.tencent.connect.common.AssistActivity"             android:theme="@android:style/Theme.Translucent.NoTitleBar"             android:screenOrientation="portrait">         </activity>          <!-- 微信wechat callback-->         <activity             android:name="xx.xx.xx.wxapi.WXEntryActivity" <!--xx.xx.xx必须与应用包名相同-->             android:configChanges="keyboardHidden|orientation|screenSize"             android:exported="true"             android:screenOrientation="portrait"             android:theme="@android:style/Theme.Translucent.NoTitleBar" />         <!-- tencent -->
<!-- 微博 -->    <activity android:name=".WBShareActivity" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait" >         <intent-filter>             <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />             <category android:name="android.intent.category.DEFAULT" />         </intent-filter>     </activity>     <activity         android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser"         android:configChanges="keyboardHidden|orientation"         android:windowSoftInputMode="adjustResize"         android:exported="false" >     </activity>        <service android:name="com.sina.weibo.sdk.net.DownloadService"         android:exported="false"></service>
    <!--###################添加UmengAppkey###################### -->         <meta-data             android:name="UMENG_APPKEY"             android:value="xxxxx" >         </meta-data>
4.添加代码,主入口文件调用

public class App extends Application{
{

    PlatformConfig.setWeixin("wx967daebe835fbeac", "5bb696d9ccd75a38c8a0bfe0675559b3");//微信
    PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad");//新浪微博
    PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");//QQ空间
    Config.REDIRECT_URL = "在新浪后台的回调地址"
}
}
			UMImage image = new UMImage(this, "imgURL");
			 new ShareAction(this).setDisplayList(SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE)
             .withTitle("标题")
             .withText("内容")
             .withMedia(image)
             .withTargetUrl("http://www.xx.com")
             .setCallback(umShareListener)
              //.withShareBoardDirection(view, Gravity.TOP|Gravity.LEFT)
             .open();//打开分享
	private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
//            Log.d("plat","platform"+platform);
            if(platform.name().equals("WEIXIN_FAVORITE")){
                Toast.makeText(this,platform + " 收藏成功啦",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(this,platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
            if(t!=null){
//                Log.d("throw","throw:"+t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };
友盟登录 UMShareAPI mShareAPI = UMShareAPI.get( this );
			SHARE_MEDIA platform=SHARE_MEDIA.WEIXIN;//微信登录
			if(!mShareAPI.isInstall(this, platform)){
				//ToastUtils.setToast(this, false, "还没有安装");
				return;
			}
			/**添加begin invoke umeng api**/
	        mShareAPI.doOauthVerify(this, platform, umAuthListener);
	        /**删除begin invoke umeng api**/
//	        mShareAPI.deleteOauth(this, platform, umdelAuthListener);
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
        	Log.e("auth callback","-----------------getting data:"+data);
            Toast.makeText(getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();
            if(data!=null&&data.containsKey("access_token")&&data.containsKey("openid")&&data.containsKey("unionid")){
            	String access_token=data.get("access_token").trim();//...
            	mShareAPI.getPlatformInfo(this, platform, umAuthListener);//获取用户信息           
            }
            if(data!=null&&data.containsKey("nickname")&&data.containsKey("headimgurl")){
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
        }
    };

注意以下---------------------------------------------------------------------------------------------------------------------------------------------------------

import com.umeng.socialize.weixin.view.WXCallbackActivity;
/**
 * Created by ntop on 15/9/4.
 */
public class WXEntryActivity extends WXCallbackActivity {

}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);//有分享或登录时调用
    }

到此基本功能已经完成……

来源http://blog.csdn.net/a704755096/article/details/46341865

展开阅读全文

没有更多推荐了,返回首页