如何统计每个Activity的PV,其实思路很简单,首先定义一个基类Activity,每一个Activity都继承自这个基类,并在这个基类onStart()和onStop()方法中进行上报,继而就可以统计到每个页面的PV、页面留存时间,同时还可以在基类中做一些优化设置。代码如下
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 取消系统设置文字大小时对App的影响
configContextConfiguration(this);
}
private void configContextConfiguration(Context context) {
try {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.fontScale = 1f;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
} catch (Throwable e) {
//ignore
}
}
@Override
protected void onStart() {
super.onStart();
FlurryAgent.onStartSession(this);
}
@Override
protected void onStop() {
super.onStop();
FlurryAgent.onEndSession(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (!isFinishing() && !mStateSaved) {
onBackPressed();
}
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// do nothing
return true;
}
return super.onKeyDown(keyCode, event);
}
}