Umeng Channel Stats Android

Source: Internet
Author: User

1. Configure androidmanifest.xml, add permissions

<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 in the OnCreate method of the application class of the application , call the SendMessage method in the Test class and pass in the Appkey of the application in the method.

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 Send Message (context context, String Appkey) throws Unsupportedencodingexception {St                  Ringbuilder 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 (C Ontext.                  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. Getsystemservi                          CE (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 (Androidi                  D, "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 = =) {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_m                          ounted)) {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 {Inputstreamreade                  R reader = new InputStreamReader (input);                  char[] buffer = new char[1024]; InchT 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 Channel Stats Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.