1、配置AndroidManifest.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.INTERNET" />

2 在应用程序的 Application类的onCreate方法中调用Test类中的sendMessage方法,在方法中传入本应用的Appkey。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.Manifest.permission;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log; public class Test {
private static final Pattern pattern = Pattern.compile("UTDID\">([^<]+)"); /**
* * send message To Server * * @param context * android Context. * @param*
* appkey * umeng appkey
*
* @throws UnsupportedEncodingException
*/
@SuppressWarnings("deprecation")
public void sendMessage(Context context, String appkey)
throws UnsupportedEncodingException {
StringBuilder sber = new StringBuilder();
sber.append("https://ar.umeng.com/stat.htm?");
if (!TextUtils.isEmpty(appkey)) {
sber.append("ak=").append(appkey);
} String devicename = Build.MODEL; if (!TextUtils.isEmpty(devicename)) {
sber.append("&device_name=").append(
URLEncoder.encode(devicename, "UTF-8"));
} TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
PackageManager pm = context.getPackageManager(); if (pm.checkPermission(permission.READ_PHONE_STATE,
context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
String imei = tm.getDeviceId(); if (!TextUtils.isEmpty(imei)) {
sber.append("&imei=").append(URLEncoder.encode(imei,"UTF-8"));
}
} if (pm.checkPermission(permission.ACCESS_WIFI_STATE,
context.getPackageName()) == PackageManager.PERMISSION_GRANTED) { WifiManager wifi = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String mac = info.getMacAddress(); // String mac = tm.getDeviceId();
if (!TextUtils.isEmpty(mac)) {
sber.append("&mac=").append(URLEncoder.encode(mac,"UTF-8"));
}
} String androidId = Secure.getString(context.getContentResolver(),
Secure.ANDROID_ID);
if (!TextUtils.isEmpty(androidId)) {
sber.append("&android_id=").append(URLEncoder.encode(androidId,"UTF-8"));
} String utdid = getUTDid(context);
if (!TextUtils.isEmpty(utdid)) {
sber.append("&utdid=").append(URLEncoder.encode(utdid,"UTF-8"));
} HttpURLConnection conn = null;
try { conn = (HttpURLConnection) new URL(sber.toString())
.openConnection(); conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
System.setProperty("http.keepAlive", "false");
}
int code = conn.getResponseCode();
if (code == 200) {
InputStream inputStream = conn.getInputStream(); // do something
} else {
Log.e("test", "status code: " + code);
}
} catch (Exception e) {
System.out.print(e.getMessage());
} finally {
if (conn != null) {
conn.disconnect();
}
}
} /**
* * get UTDID * * @param context * @return
*/
private String getUTDid(Context context) {
try {
Class<?> utdevice = Class.forName("com.ut.device.UTDevice");
Method reMethod = utdevice.getMethod("getUtdid", Context.class);
return (String) reMethod.invoke(null, context);
} catch (Exception e) {
return readNativeFile(context);
}
} //get mac /**
* * read native file method * * @param context * @return
*/
private String readNativeFile(Context context) {
File f = getFile(context);
if (f == null || !f.exists()) {
return null;
}
try {
FileInputStream fis = new FileInputStream(f);
try {
return parseId(readStreamToString(fis));
} finally {
safeClose(fis);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* * java pattern parse native utdid * * @param content * @return
*/
private String parseId(String content) {
if (content == null) {
return null;
}
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
return matcher.group(1);
}
return null;
} /** * read native file * * @param context * @return */ private File getFile(Context context) {
PackageManager pm = context.getPackageManager();
if (pm.checkPermission(permission.WRITE_EXTERNAL_STORAGE,
context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
return null;
}
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sdCardDir = Environment.getExternalStorageDirectory();
try {
return new File(sdCardDir.getCanonicalPath(),
".UTSystemConfig/Global/Alvin2.xml");
} catch (Exception ignore) {
}
}
return null;
} /**
* * convert inputstream to String * * @param input * @return * @throws
* IOException
* */
private String readStreamToString(InputStream input) throws IOException {
InputStreamReader reader = new InputStreamReader(input);
char[] buffer = new char[1024];
int n = 0;
StringWriter writer = new StringWriter();
while (-1 != (n = reader.read(buffer))) {
writer.write(buffer, 0, n); }
return writer.toString();
} /**
* * close io stream * * @param is
*/
private void safeClose(InputStream is) {
if (is != null) {
try {
is.close();
} catch (Exception e) { }
}
}
}

  

umeng 渠道统计 android的更多相关文章

  1. umeng 渠道统计ios

    1. 使用前提:需要在工程中添加依赖框架:AdSupport.framework 和 Security.framework 2.在AppDelegate.m文件中的-(BOOL)application ...

  2. App渠道统计方法全面解析 总有一种适合你

    一.App渠道统计对于App推广运营的重要性 (理解App渠道统计重要性的老司机,请直接移步到第二部分) App服务的竞争重点已经由功能竞争转向市场和运营的竞争,而App的推广与运营离不开App渠道统 ...

  3. openinstall渠道统计工具介绍

    大家好,今天给大家介绍一下如何使用openinstall 来实现APP 渠道统计,做运营推广的朋友应该对渠道统计并不陌生,之前一般都是让技术的同事打渠道包方式进行渠道统计,而且只有安卓才能打渠道包.o ...

  4. 免打包:简单、灵活、便捷的APP渠道统计方法

    相信做过APP运营推广的小伙伴们应该对APP渠道统计并不陌生吧.APP推广运营人员需要根据数据来评估渠道推广的效果,找到最适合自家APP的渠道,有针对性的投放,不断完善推广策略,这样才能更加精准.有效 ...

  5. 【python调用windows CLI】调用adb统计Android app的流量消耗

    主要记录python如何调用windows CLI 手机连接PC,adb devices可以看到手机sn 通过adb 获取指定app的processID UID 读取Android  /proc/ne ...

  6. jacoco统计Android手工测试覆盖率并自动上报服务器

    改进了几个点 1. 不用借助Instrumentation启动,正常启动即可: 2. 测试代码不用push到主分支,主分支代码拉到本地后用git apply patch方式合并覆盖率代码: 3. 测试 ...

  7. 实现统计 android手机 CPU使用率

    # -*- coding:utf-8 -*- ''' Created on Sep 10, 2018 @author: SaShuangYiBing ''' import subprocess imp ...

  8. android分渠道打包,监测日活量统计(基于友盟SDK)

    客服说要看App日活,让加个统计功能. (我们技术部已经混到客服部都能直接提需求的地步) 首先接入友盟统计的SDK,在项目外层的build.gradle中添加依赖'https://dl.bintray ...

  9. Android应用发布后的统计——百度移动统计的应用

    一个App发布到各个渠道之后,我们需要采集不同渠道的一些信息,比如app在运行过程中产生的一些异常信息,app在各个android版本的分布,以及各个app版本的分布,各渠道的用户数,用户忠诚度等等信 ...

随机推荐

  1. Android 软件开发之如何使用Eclipse Debug调试程序详解及Eclipse常用快捷键(转)

    1.在程序中添加一个断点如果所示:在Eclipse中添加了一个程序断点 在Eclipse中一共有三种添加断点的方法 第一种: 在红框区域右键出现菜单后点击第一项 Toggle Breakpoint 将 ...

  2. Android自动化测试之Monkeyrunner学习笔记(一)

    Android自动化测试之Monkeyrunner学习笔记(一) 因项目需要,开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括Monkey.Monkeyr ...

  3. mysql的collation

    mysql的collation大致的意思就是字符序.首先字符本来是不分大小的,那么对字符的>, = , < 操作就需要有个字符序的规则.collation做的就是这个事情,你可以对表进行字 ...

  4. CSS魔法堂:说说Float那个被埋没的志向

    前言  定位系统中第一难理解就是Normal flow,而第二就非Float莫属了,而Float难理解的原因有俩,1. 一开头我们就用错了:2. 它跟Normal flow靠得太近了.本文尝试理清Fl ...

  5. 【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)

    如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...

  6. PHP类和对象等代码说明

    1.定义和创建类和对象: 定义类要使用class关键字.例如:class 类名{//属性和方法} 创建对象使用new关键字.例如: $p1 = new 类名;,可以基于一个类创建多个对象. 2. 类属 ...

  7. WinForm打印

    WinForm打印要用打印控件: PageSetupDialog:打印设置对话框 PrintDialog:打印对话框 PrintDocument:要打印的对象,非常重要 PrintPreviewCon ...

  8. android 开发:shape和selector和layer-list的(详细说明)

    目录(?)[+] Shape 简介 使用的方法 属性 Selector 简介 使用的方法 layer-list 简介 例子 最后   <shape>和<selector>在An ...

  9. 火狐浏览器+Firebug+FirePath测试Xpath

    前言 抓取网页数据时使用HtmlAgilityPack分析,需要通过xpath定位页面元素.如果有个xpath的生成和验证工具就事半功倍了,火狐浏览器插件FirePath配合Firebug就能完美实现 ...

  10. 两台Linux主机之间文件的复制

    使用scp命令可以实现两台Linux主机之间的文件复制,基本格式是: scp [可选参数] file_source file_target 1. 复制文件 命令格式: scp local_file r ...