标签:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import com.example.exmanalysis.umeng.analytics.FragmentStack; import com.example.exmanalysis.umeng.analytics.FragmentTabs; import com.example.exmanalysis.umeng.analytics.WebviewAnalytic; import com.umeng.analytics.MobclickAgent; import com.umeng.analytics.MobclickAgent.EScenarioType; import com.umeng.analytics.social.UMPlatformData; import com.umeng.analytics.social.UMPlatformData.GENDER; import com.umeng.analytics.social.UMPlatformData.UMedia; public class UmengActivity extends Activity { private final static String TAG = "UmengActivity"; private Context mContext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_umeng); mContext = this; MobclickAgent.setDebugMode(true); // SDK在统计Fragment时,需要关闭Activity自带的页面统计, // 然后在每个页面中重新集成页面统计的代码(包括调用了 onResume 和 onPause 的Activity)。 MobclickAgent.openActivityDurationTrack(false); // MobclickAgent.setAutoLocation(true); // MobclickAgent.setSessionContinueMillis(1000); // MobclickAgent.startWithConfigure( // new UMAnalyticsConfig(mContext, "4f83c5d852701564c0000011", "Umeng", EScenarioType.E_UM_NORMAL)); MobclickAgent.setScenarioType(mContext, EScenarioType.E_UM_NORMAL); } @Override public void onResume() { super.onResume(); MobclickAgent.onPageStart(TAG); MobclickAgent.onResume(mContext); } @Override public void onPause() { super.onPause(); MobclickAgent.onPageEnd(TAG); MobclickAgent.onPause(mContext); } public void onButtonClick(View view) { int id = view.getId(); switch (id) { case R.id.umeng_example_analytics_event_cc: List<String> keyPath = new ArrayList<String>(); keyPath.add("one"); keyPath.add("two"); keyPath.add("tree"); MobclickAgent.onEvent(mContext, keyPath, 20, "label"); break; case R.id.umeng_example_analytics_event: MobclickAgent.onEvent(mContext, "click"); MobclickAgent.onEvent(mContext, "click", "button"); break; case R.id.umeng_example_analytics_ekv: Map<String, String> map_ekv = new HashMap<String, String>(); map_ekv.put("type", "popular"); map_ekv.put("artist", "JJLin"); MobclickAgent.onEvent(mContext, "music", map_ekv); break; case R.id.umeng_example_analytics_duration: Map<String, String> map_value = new HashMap<String, String>(); map_value.put("type", "popular"); map_value.put("artist", "JJLin"); MobclickAgent.onEventValue(this, "music", map_value, 12000); break; case R.id.umeng_example_analytics_make_crash: "123".substring(10); break; case R.id.umeng_example_analytics_js_analytic: startActivity(new Intent(this, WebviewAnalytic.class)); break; case R.id.umeng_example_analytics_fragment_stack: startActivity(new Intent(this, FragmentStack.class)); break; case R.id.umeng_example_analytics_fragment_tabs: startActivity(new Intent(this, FragmentTabs.class)); break; case R.id.umeng_example_analytics_social: UMPlatformData platform = new UMPlatformData(UMedia.SINA_WEIBO, "user_id"); platform.setGender(GENDER.MALE); // optional platform.setWeiboId("weiboId"); // optional MobclickAgent.onSocialEvent(this, platform); break; case R.id.umeng_example_analytics_signin: MobclickAgent.onProfileSignIn("example_id"); break; case R.id.umeng_example_analytics_signoff: MobclickAgent.onProfileSignOff(); break; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { Hook(); return true; } return super.onKeyDown(keyCode, event); } // /对于好多应用,会在程序中杀死 进程,这样会导致我们统计不到此时Activity结束的信息, // /对于这种情况需要调用 'MobclickAgent.onKillProcess( Context )' // /方法,保存一些页面调用的数据。正常的应用是不需要调用此方法的。 private void Hook() { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setPositiveButton("退出应用", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { MobclickAgent.onKillProcess(mContext); int pid = android.os.Process.myPid(); android.os.Process.killProcess(pid); } }); builder.setNeutralButton("后退一下", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { finish(); } }); builder.setNegativeButton("点错了", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }); builder.show(); } }
import android.app.Activity; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.baidu.mobstat.NativeCrashHandler; import com.baidu.mobstat.SendStrategyEnum; import com.baidu.mobstat.StatService; import com.example.exmanalysis.baidu.AnotherDemoActivity1; import com.example.exmanalysis.baidu.Conf; import com.example.exmanalysis.baidu.DemoActivity2; import com.example.exmanalysis.baidu.DemoActivity3; import com.example.exmanalysis.baidu.WebViewActivity; import com.example.exmanalysis.baidu.appfragment.AppFragmentDemoActivity; import com.example.exmanalysis.baidu.supportv4fragment.MainFragmentActivity; public class BaiduActivity extends Activity { private Button btnPrev; private Button btnNext; private Button btnWebview; private Button btnException; private Button btnNativeException; private Button btnSetTV; private Button btnEvent; private Button btnEventDuration; private Button btnEventStart; private Button btnEventEnd; private Button btnFragmentPage; private Button btnAppFragmentPage; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.baidu_activity); boolean isWear = getPackageManager().hasSystemFeature("android.hardware.type.watch"); Log.e("TEST", "isWear: " + isWear); Log.e("TEAT", "manufacturer: " + Build.MANUFACTURER); // 设置AppKey // StatService.setAppKey("a9e2ad84a2"); // appkey必须在mtj网站上注册生成,该设置建议在AndroidManifest.xml中填写,代码设置容易丢失 /* * 设置渠道的推荐方法。该方法同setAppChannel(String), 如果第三个参数设置为true(防止渠道代码设置会丢失的情况),将会保存该渠道,每次设置都会更新保存的渠道, * 如果之前的版本使用了该函数设置渠道,而后来的版本需要AndroidManifest.xml设置渠道,那么需要将第二个参数设置为空字符串,并且第三个参数设置为false即可。 * appChannel是应用的发布渠道,不需要在mtj网站上注册,直接填写就可以 该参数也可以设置在AndroidManifest.xml中 */ // StatService.setAppChannel(this, "RepleceWithYourChannel", true); // 测试时,可以使用1秒钟session过期,这样不断的间隔1S启动退出会产生大量日志。 StatService.setSessionTimeOut(30); // setOn也可以在AndroidManifest.xml文件中填写,BaiduMobAd_EXCEPTION_LOG,打开崩溃错误收集,默认是关闭的 StatService.setOn(this, StatService.EXCEPTION_LOG); /* * 设置启动时日志发送延时的秒数<br/> 单位为秒,大小为0s到30s之间<br/> 注:请在StatService.setSendLogStrategy之前调用,否则设置不起作用 * * 如果设置的是发送策略是启动时发送,那么这个参数就会在发送前检查您设置的这个参数,表示延迟多少S发送。<br/> 这个参数的设置暂时只支持代码加入, * 在您的首个启动的Activity中的onCreate函数中使用就可以。<br/> */ StatService.setLogSenderDelayed(0); /* * 用于设置日志发送策略<br /> 嵌入位置:Activity的onCreate()函数中 <br /> * * 调用方式:StatService.setSendLogStrategy(this,SendStrategyEnum. SET_TIME_INTERVAL, 1, false); 第二个参数可选: * SendStrategyEnum.APP_START SendStrategyEnum.ONCE_A_DAY SendStrategyEnum.SET_TIME_INTERVAL 第三个参数: * 这个参数在第二个参数选择SendStrategyEnum.SET_TIME_INTERVAL时生效、 取值。为1-24之间的整数,即1<=rtime_interval<=24,以小时为单位 第四个参数: * 表示是否仅支持wifi下日志发送,若为true,表示仅在wifi环境下发送日志;若为false,表示可以在任何联网环境下发送日志 */ StatService.setSendLogStrategy(this, SendStrategyEnum.SET_TIME_INTERVAL, 1, false); // 调试百度统计SDK的Log开关,可以在Eclipse中看到sdk打印的日志,发布时去除调用,或者设置为false StatService.setDebugOn(true); String sdkVersion = StatService.getSdkVersion(); TextView sdkVersionTxtv = (TextView) findViewById(R.id.tv_sdk_version); if (sdkVersion != null) { sdkVersionTxtv.setText("sdk version is: " + sdkVersion); } btnPrev = (Button) findViewById(R.id.layout1_btn1); btnNext = (Button) findViewById(R.id.layout1_btn2); btnWebview = (Button) findViewById(R.id.layout1_btn_web_view); btnException = (Button) findViewById(R.id.layout1_btn_excep); btnNativeException = (Button) findViewById(R.id.layout1_btn_native_excep); btnNativeException.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { NativeCrashHandler.doNativeCrash(); } }); btnSetTV = (Button) findViewById(R.id.layout1_btn_set_TV); btnSetTV.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { StatService.setForTv(BaiduActivity.this, true); } }); btnEvent = (Button) findViewById(R.id.layout1_btn_event); btnEventDuration = (Button) findViewById(R.id.layout1_btn_event_duration); btnEventStart = (Button) findViewById(R.id.layout1_btn_event_start); btnEventEnd = (Button) findViewById(R.id.layout1_btn_event_end); btnFragmentPage = (Button) findViewById(R.id.layout1_fragment); btnAppFragmentPage = (Button) findViewById(R.id.layout1_app_fragment); findViewById(R.id.btn_another_process).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(BaiduActivity.this, AnotherDemoActivity1.class); startActivity(intent); } }); btnPrev.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.setClass(BaiduActivity.this, DemoActivity3.class); BaiduActivity.this.startActivity(intent); } }); btnNext.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.setClass(BaiduActivity.this, DemoActivity2.class); BaiduActivity.this.startActivity(intent); } }); btnWebview.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(BaiduActivity.this, WebViewActivity.class); BaiduActivity.this.startActivity(intent); } }); btnException.setOnClickListener(new OnClickListener() { public void onClick(View v) { Thread t = new Thread() { @Override public void run() { if (!MyApplication.SHIELD_EXCEPTION) { Log.w(Conf.TAG, 10 / 0 + ""); } } }; t.start(); } }); btnEvent.setOnClickListener(new OnClickListener() { public void onClick(View v) { StatService.onEvent(BaiduActivity.this.getApplicationContext(), "registered id", "pass", 1); } }); /** * 自定义事件的第一种方法,写入某个事件的持续时长 */ btnEventDuration.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 事件id("registered id")的事件pass,其时长持续100毫秒 StatService.onEventDuration(BaiduActivity.this, "registered id", "pass", 100); } }); /* * 自定义事件的第二种方法,自己定义该事件的起始时间和结束时间 */ btnEventStart.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 事件id("registered id")的事件pass,其时长持续10毫秒 StatService.onEventStart(BaiduActivity.this, "registered id", "pass"); // 必须和onEventEnd共用才行 } }); /* * 自定义事件的第二种方法,自己定义该事件的起始时间和结束时间 */ btnEventEnd.setOnClickListener(new OnClickListener() { public void onClick(View v) { // 事件id("registered id")的事件pass,其时长持续10毫秒 StatService.onEventEnd(BaiduActivity.this, "registered id", "pass"); // 必须和onEventStart共用才行 } }); btnFragmentPage.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent in = new Intent(); in.setClass(BaiduActivity.this, MainFragmentActivity.class); startActivity(in); } }); btnAppFragmentPage.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { Intent in = new Intent(); in.setClass(BaiduActivity.this, AppFragmentDemoActivity.class); startActivity(in); } } }); } public void onResume() { super.onResume(); //页面起始(每个Activity中都需要添加,如果有继承的父Activity中已经添加了该调用,那么子Activity中务必不能添加) //不能与StatService.onPageStart一级onPageEnd函数交叉使用 StatService.onResume(this); } public void onPause() { super.onPause(); //页面结束(每个Activity中都需要添加,如果有继承的父Activity中已经添加了该调用,那么子Activity中务必不能添加) //不能与StatService.onPageStart一级onPageEnd函数交叉使用 StatService.onPause(this); } }
import java.util.HashMap; import java.util.Map; import java.util.Properties; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.os.Bundle; import android.os.Environment; import android.os.MemoryFile; import android.util.Log; import android.view.View; import android.widget.Button; import com.tencent.stat.StatAppMonitor; import com.tencent.stat.StatConfig; import com.tencent.stat.StatNativeCrashReport; import com.tencent.stat.StatService; import com.tencent.stat.StatServiceImpl; public class TencentActivity extends Activity { private final static String TAG = "TencentActivity"; private Context ctx = null; private Button btn_customevent_args_count = null; private Button btn_customevent_kv_count = null; private Button btn_customevent_args_duration_begin = null; private Button btn_customevent_args_duration_end = null; private Button btn_customevent_kv_duration_begin = null; private Button btn_customevent_kv_duration_end = null; private Button btn_report_error = null; private Button btn_report_exception = null; private Button btn_catch_unhandled_exception = null; private Button btn_init_nativeCrash = null; private Button btn_catch_native_crash = null; private Button btn_monitor_events = null; public void onActivityOpen() { // 你的函数 Properties prop = new Properties(); prop.setProperty("aty", "抢票活动"); // 活动页面 prop.setProperty("gid", "潜在付费用户"); // 用户组名称 prop.setProperty("attr", "1"); // 用户属性(年龄、性别等) prop.setProperty("act_type", "1"); // 行为类型(最近30天启动过、最近30天使用时长超过、最近30天是否有过升级行为等) prop.setProperty("act_val", "10"); // 行为取值 StatService.trackCustomKVEvent(this, "mta_tag_activity_open", prop); } public void onActivityClick2() { // 你的函数 Properties prop = new Properties(); prop.setProperty("aty", "抢票活动"); // 活动页面 prop.setProperty("btn", "报名"); // 按钮 prop.setProperty("gid", "潜在付费用户"); // 用户组名称 prop.setProperty("attr ", "1"); // 用户属性(年龄、性别等) prop.setProperty("act_type ", "1"); // 行为类型(最近30天启动过、最近30天使用时长超过、最近30天是否有过升级行为等) prop.setProperty("act_val ", "10"); // 行为取值 StatService.trackCustomKVEvent(this, "mta_tag_activity_click", prop); } public void onUserPay() { // 你的函数 Properties prop = new Properties(); prop.setProperty("scene", "通关"); // 付费场景 prop.setProperty("amount", "350"); // 付费金额 prop.setProperty("way", "手机支付"); // 付费方式(可选择上报) StatService.trackCustomKVEvent(null, "mta_tag_user_pay", prop); } MemoryFile memoryFile = null; private View.OnClickListener l = new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "ctx==" + ctx); int id = v.getId(); switch (id) { case R.id.btn_customevent_args_count: StatService.trackCustomEvent(ctx, "trackCustomEvent", "args"); StatService.trackCustomEvent(ctx, "id1", "args"); StatService.trackCustomEvent(ctx, "id2", ""); StatService.trackCustomEvent(ctx, "trackCustomEvent", null); StatServiceImpl.trackBackground(getApplicationContext(), 10, null); break; case R.id.btn_customevent_kv_count: Properties prop = new Properties(); prop.setProperty("key", "value"); prop.setProperty("key2", "value2"); StatService.trackCustomKVEvent(ctx, "trackCustomKVEvent", prop); prop = new Properties(); prop.setProperty("a", "b"); StatService.trackCustomKVEvent(ctx, "4", prop); StatService.trackCustomKVEvent(ctx, "trackCustomKVEvent", prop); prop = new Properties(); prop.setProperty("num", "3434"); StatService.trackCustomKVEvent(ctx, "trackCustomKVEvent", prop); break; case R.id.btn_customevent_args_duration_begin: StatService.trackCustomBeginEvent(ctx, "trackCustomEvent", "loadConfigFile"); break; case R.id.btn_customevent_args_duration_end: StatService.trackCustomEndEvent(ctx, "trackCustomEvent", "loadConfigFile"); break; case R.id.btn_customevent_kv_duration_begin: Properties properties = new Properties(); properties.setProperty("load", "config"); StatService.trackCustomBeginKVEvent(ctx, "trackCustomEvent", properties); break; case R.id.btn_customevent_kv_duration_end: properties = new Properties(); properties.setProperty("load", "config"); StatService.trackCustomEndKVEvent(ctx, "trackCustomEvent", properties); break; case R.id.btn_report_error: StatService.reportError(ctx, "I hate error."); StatService.commitEvents(getApplicationContext(), -1); break; case R.id.btn_report_exception: try { String myNull = null; int length = myNull.length(); } catch (NullPointerException ex) { StatService.reportException(ctx, ex); } break; case R.id.btn_monitor_events: // 新建监控接口对象 StatAppMonitor monitor = new StatAppMonitor("ping:www.qq.com"); // 接口开始执行 String ip = "www.qq.com"; Runtime run = Runtime.getRuntime(); java.lang.Process proc = null; try { String str = "ping -c 3 -i 0.2 -W 1 " + ip; long starttime = System.currentTimeMillis(); proc = run.exec(str); int retCode = proc.waitFor(); long difftime = System.currentTimeMillis() - starttime; // 设置接口耗时 monitor.setMillisecondsConsume(difftime); // 设置接口返回码 monitor.setReturnCode(retCode); // 设置请求包大小,若有的话 monitor.setReqSize(1000); // 设置响应包大小,若有的话 monitor.setRespSize(2000); // 设置抽样率,默认为1,表示100%。如果是50%,则填2(100/50),如果是25%,则填4(100/25),以此类推。 // monitor.setSampling(2); if (retCode == 0) { Log.d(TAG, "ping连接成功"); // 标记为成功 monitor.setResultType(StatAppMonitor.SUCCESS_RESULT_TYPE); } else { Log.d(TAG, "ping测试失败"); // 标记为逻辑失败,可能由网络未连接等原因引起的,但对于业务来说不是致命的,是可容忍的 monitor.setResultType(StatAppMonitor.LOGIC_FAILURE_RESULT_TYPE); } } catch (Exception e) { Log.d(TAG, e.getMessage()); // 接口调用出现异常,致命的,标识为失败 monitor.setResultType(StatAppMonitor.FAILURE_RESULT_TYPE); } finally { proc.destroy(); } // 上报接口监控的信息 StatService.reportAppMonitorStat(ctx, monitor); Map<String, Integer> map = new HashMap<String, Integer>(); map.put("www.qq.com", 80); map.put("pingma.qq.com", 80); StatService.testSpeed(ctx, map); break; case R.id.btn_catch_unhandled_exception: int i = 1 / 0; break; case R.id.btn_init_nativeCrash: String tombfilepath = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/my"; StatConfig.initNativeCrashReport(ctx, null); break; case R.id.btn_catch_native_crash: StatNativeCrashReport.doNativeCrashTest(); break; default: break; } } }; String getCurProcessName(Context context) { int pid = android.os.Process.myPid(); ActivityManager mActivityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager .getRunningAppProcesses()) { if (appProcess.pid == pid) { return appProcess.processName; } } return null; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tencent); ctx = this; // 高级功能:在线配置更新 // String onOrOff = StatConfig.getCustomProperty("switch", "off"); // Log.d(TAG, "switch" + onOrOff); // if(onOrOff.equalsIgnoreCase("on")){ // // 打开某项功能 // }else{ // // 禁用某项功能 // } // String appid = "互联的appid"; // String appkey = "Aqc" + appid; // StatConfig.setAutoExceptionCaught(false); // 禁止捕获app未处理的异常 // StatConfig.setEnableSmartReporting(true); // 禁止WIFI网络实时上报 // StatConfig.setSendPeriodMinutes(24 * 60); // PERIOD间隔周期,24小时 // StatConfig.setStatSendStrategy(StatReportStrategy.PERIOD); // // PERIOD上报策略 // try { // StatService.startStatService(this, appkey, // com.tencent.stat.common.StatConstants.VERSION); // } catch (MtaSDkException e) { // Log.e("DEBUG", "MTA init Failed."); // } initActivity(); } private void initActivity() { setTitle("常用统计分析"); btn_customevent_args_count = (Button) findViewById(R.id.btn_customevent_args_count); btn_customevent_kv_count = (Button) findViewById(R.id.btn_customevent_kv_count); btn_customevent_args_duration_begin = (Button) findViewById(R.id.btn_customevent_args_duration_begin); btn_customevent_args_duration_end = (Button) findViewById(R.id.btn_customevent_args_duration_end); btn_customevent_kv_duration_begin = (Button) findViewById(R.id.btn_customevent_kv_duration_begin); btn_customevent_kv_duration_end = (Button) findViewById(R.id.btn_customevent_kv_duration_end); btn_report_error = (Button) findViewById(R.id.btn_report_error); btn_report_exception = (Button) findViewById(R.id.btn_report_exception); btn_catch_unhandled_exception = (Button) findViewById(R.id.btn_catch_unhandled_exception); btn_catch_native_crash = (Button) findViewById(R.id.btn_catch_native_crash); btn_init_nativeCrash = (Button) findViewById(R.id.btn_init_nativeCrash); btn_monitor_events = (Button) findViewById(R.id.btn_monitor_events); btn_customevent_args_count.setOnClickListener(l); btn_customevent_kv_count.setOnClickListener(l); btn_customevent_args_duration_begin.setOnClickListener(l); btn_customevent_args_duration_end.setOnClickListener(l); btn_customevent_kv_duration_begin.setOnClickListener(l); btn_customevent_kv_duration_end.setOnClickListener(l); btn_report_error.setOnClickListener(l); btn_report_exception.setOnClickListener(l); btn_catch_unhandled_exception.setOnClickListener(l); btn_init_nativeCrash.setOnClickListener(l); btn_catch_native_crash.setOnClickListener(l); btn_monitor_events.setOnClickListener(l); } @Override protected void onResume() { super.onResume(); StatService.onResume(this); } @Override protected void onPause() { super.onPause(); StatService.onPause(this); } }
标签:
原文地址:http://blog.csdn.net/aqi00/article/details/51719051