友盟

友盟

  • 主界面
    • activity Gradle
    • 当前项目Gradle
    • 自定义类继承自Application

主界面

实现通知、推送、统计

package com.example.contron;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "123321";
    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(MainActivity.this,"成功了",Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(MainActivity.this, "失败了"+t.getMessage(), Toast.LENGTH_SHORT).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(MainActivity.this,"取消                                          了",Toast.LENGTH_LONG).show();

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        PushAgent.getInstance(this).onAppStart();
//        ACCESS_COARSE_LOCATION
//        ACCESS_FINE_LOCATION"
//        CHANGE_WIFI_STATE" />
        if(Build.VERSION.SDK_INT>=23){
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.CHANGE_WIFI_STATE};
            ActivityCompat.requestPermissions(this,mPermissionList,123);
        }

        UMConfigure.init(this,"5d809592570df30507000cbe"
                ,"umeng",UMConfigure.DEVICE_TYPE_PHONE,"664d24096a68c1f686b75f2be81a1236");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");

        UMConfigure.init(this,UMConfigure.DEVICE_TYPE_PHONE,"664d24096a68c1f686b75f2be81a1236");

        // 初始化SDK
        UMConfigure.init(this, "5d809592570df30507000cbe", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, null);
        /**
         * 注意: 即使您已经在AndroidManifest.xml中配置过appkey和channel值,也需要在App代码中调
         * 用初始化接口(如需要使用AndroidManifest.xml中配置好的appkey和channel值,
         * UMConfigure.init调用中appkey和channel参数请置为null)。
         */
        /**
         * 注意:如果您已经在AndroidManifest.xml中配置过appkey和channel值,可以调用此版本初始化函数。
         */
        /**
         * 设置组件化的Log开关
         * 参数: boolean 默认为false,如需查看LOG设置为true
         */
        UMConfigure.setLogEnabled(true);

        // 选用MANUAL页面采集模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.LEGACY_AUTO);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {

    }


    public void click(View view) {
        UMImage image = new UMImage(this, R.drawable.umeng_socialize_copyurl);//本地文件
//        new ShareAction(this).withText("hello").withMedia(image).share();
        new ShareAction(MainActivity.this).withText("hello").setDisplayList(SHARE_MEDIA.QQ)
                .withMedia(image)
                .setCallback(shareListener).open();
    }

    // Fragment页面onResume函数重载
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainScreen"); //统计页面("MainScreen"为页面名称,可自定义)
        MobclickAgent.onResume(this); // 不能遗漏

    }

    // Fragment页面onResume函数重载
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainScreen");
        MobclickAgent.onPause(this); // 不能遗漏
    }
}

activity Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.1"


    defaultConfig {
        flavorDimensions "default"//添加这句胡 不然报错
        applicationId "com.example.contron"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
        Alpha {//自己打包测试使用
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: Alpha]
        }
        Tencent {//投放应用宝市场
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: Tencent]
        }
        Baidu {//投放百度市场
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: Baidu]
        }
        Qihoo {//投放360市场
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: Qihoo]
        }
        Alibaba {//投放阿里巴巴
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: Alibaba]
        }
        Vivo {//投放vivo市场
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: Vivo]
        }
        Oppo {//投放oppo市场
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: Oppo]
        }
        Xiaomi {//投放小米市场
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: Xiaomi]
        }
        Huawei {//投放华为应用市场
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: Huawei]
        }

        productFlavors.all { flavor ->
            flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }

    }

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])

        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.2.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

        //PushSDK必须依赖基础组件库,所以需要加入对应依赖
//    implementation 'com.umeng.umsdk:common:1.5.4'
//PushSDK必须依赖utdid库,所以需要加入对应依赖
//    implementation 'com.umeng.umsdk:utdid:1.1.5.3'
//PushSDK
        implementation 'com.umeng.umsdk:push:6.0.1'
//    implementation 'com.umeng.sdk:common:1.5.0'
        implementation 'com.umeng.sdk:utdid:1.1.5.3'
//        implementation 'com.umeng.sdk:common:1.5.1'
//        implementation 'com.umeng.sdk:analytics:7.5.0'

        implementation  'com.umeng.umsdk:analytics:8.0.0'
//        implementation  'com.umeng.umsdk:common:2.0.0'
    }
}

当前项目Gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://dl.bintray.com/umsdk/release' }
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0'
        
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url 'https://dl.bintray.com/umsdk/release' }
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
allprojects {
    repositories {
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

自定义类继承自Application

package com.example.contron;

import android.app.Application;
import android.util.Log;

import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;

public class MyApplication extends Application {

    private static final String TAG = "123321";
    @Override
    public void onCreate() {
        super.onCreate();

        // 在此处调用基础组件包提供的初始化函数 相应信息可在应用管理 -> 应用信息 中找到 http://message.umeng.com/list/apps
// 参数一:当前上下文context;
// 参数二:应用申请的Appkey(需替换);
// 参数三:渠道名称;
// 参数四:设备类型,必须参数,传参数为UMConfigure.DEVICE_TYPE_PHONE则表示手机;传参数为UMConfigure.DEVICE_TYPE_BOX则表示盒子;默认为手机;
// 参数五:Push推送业务的secret 填充Umeng Message Secret对应信息(需替换)
        UMConfigure.init(this, "5d809592570df30507000cbe", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "664d24096a68c1f686b75f2be81a1236");
        //获取消息推送代理示例
        PushAgent mPushAgent = PushAgent.getInstance(this);
//注册推送服务,每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回deviceToken deviceToken是推送消息的唯一标志
                Log.i(TAG,"注册成功:deviceToken:-------->  " + deviceToken);
            }

            @Override
            public void onFailure(String s, String s1) {
                Log.e(TAG,"注册失败:-------->  " + "s:" + s + ",s1:" + s1);
            }
        });

    }


}

你可能感兴趣的:(安卓第三个月)