Dic*_*ben 17 android chrome-custom-tabs
我有一个应用程序Chrome custom tabs用来打开一些链接,我需要在用户留在Chrome上的每一秒都有事件,或者知道他在Chrome上停留了多少时间.对我来说,唯一的方法是使用a Service.有可能以不同的方式做到这一点吗?
为了实现 chrome 自定义选项卡,我遵循了本教程 github链接。
我的解决方案基本上依赖于boolean和System.currentTimeMillis()。
步骤 - 1:声明两个类全局变量,
private boolean isCustomTabsLaunched = false;
private long customTabsEnterTime;
Run Code Online (Sandbox Code Playgroud)
步骤 - 2:在 launchUrl 时将上述值设置为变量。
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "FloatingActionButton");
// Launch Chrome Custom Tabs on click
customTabsIntent.launchUrl(CustomTabsActivity.this, Uri.parse(URL));
isCustomTabsLaunched = true;
customTabsEnterTime = System.currentTimeMillis();
Log.d(TAG, "customTabsEnterTime = " + customTabsEnterTime);
}
});
Run Code Online (Sandbox Code Playgroud)
步骤 - 3:计算 onResume 方法中的停留时间。
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
if (isCustomTabsLaunched) {
isCustomTabsLaunched = false;
calculateStayTime();
}
}
private void calculateStayTime() {
long customTabsExitTime = System.currentTimeMillis();
Log.d(TAG, "customTabsExitTime = " + customTabsExitTime);
long stayTime = (customTabsExitTime - customTabsEnterTime) / 1000; //convert in seconds
Log.d(TAG, "stayTime = " + stayTime);
}
Run Code Online (Sandbox Code Playgroud)
为了使代码更健壮,您可能希望在首选项或数据库中存储 boolean isCustomTabsLaunched 和 long customTabsEnterTime ,因此在任何情况下,这两个参数都会被破坏,因为如果用户长时间停留在 chrome 自定义选项卡中,您的活动可能会在后台被破坏。
| 归档时间: |
|
| 查看次数: |
1040 次 |
| 最近记录: |