androidApp接入友盟运行数据统计

友盟注册Key

在项目build.gradle下增加:mavenCentral()

repositories {
        maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public/'
        }
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'       
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public/'
        }
        google()
        jcenter()
        mavenCentral()
    }
}

在工程build.gradle下添加依赖:

compile 'com.umeng.sdk:common:latest.integration'
compile 'com.umeng.sdk:analytics:latest.integration'

在proguard-rules下增加混淆:

# 友盟sdk 混淆配置
-keep class com.umeng.commonsdk.** {*;}

创建MyApp,对友盟功能代码进行初始化:

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        /**
         * 初始化common库
         * 参数1:上下文,不能为空
         * 参数2:设备类型,UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子,默认为手机
         * 参数3:Push推送业务的secret
         */
        UMConfigure.setLogEnabled(BuildConfig.DEBUG);//Log开关,在debug情况下输出
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE,null);

        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);

    }

}

并在AndroidManifest.xml中:

android:name=".MyApp"

创建BaseActivity,让所有的Activity都继承BaseActivity

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}

AndroidManifest.xml写入如下配置数据:

  <meta-data
      android:name="UMENG_APPKEY"
      android:value="在友盟申请的key" />
  <meta-data
      android:name="UMENG_CHANNEL"
      android:value="yingyongbao" />

增加权限:

<!-- 必须的权限 -->
<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.INTERNET" />

你可能感兴趣的:(android)