监听Chrome自定义标签页进度事件

17

我有一个应用程序,使用 Chrome 自定义标签页打开一些链接。我需要在用户停留在 Chrome 的整个时间内每秒钟触发一个事件,或者知道他在 Chrome 上停留了多长时间。对我来说,唯一的方法是使用一个服务(Service)。是否有其他方法可以实现这个功能?


听起来您想要监听一个事件,当自定义选项卡关闭时触发。是这样吗? - ade
如果在回调中有用户会话的总时间,那么它可能有效,但最好是每隔一段时间就触发一个事件。 - Dichoben
2个回答

2
创建您的YourBroadCastReceiver类,如下所示。
public class YourBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("Called every 60 seconds","called");
    }

}

成功启动您的自定义选项卡后,请创建一个Alarm PendingIntent,以便每60秒触发YourBroadCastReceiver。

    // Retrieve a PendingIntent that will perform a broadcast

    Intent repeatingIntent = new Intent(context,
            YourBroadCastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
           context, _pendingIntentId, alarmIntent, 0);

    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // Set the alarm to start at 10:00 AM
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    manager.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), 60 * 1000, // repeat for every 60 seconds
            pendingIntent);

在关闭自定义选项卡后,不要忘记取消您的PendingIntent。
PendingIntent.getBroadcast(
       context, _pendingIntentId, alarmIntent, 0).cancel();

1
我将为您翻译以下内容:

为了实现chrome自定义标签,我遵循了this教程,github link

我的解决方案基本上依赖于布尔值System.currentTimeMillis()

步骤 - 1:声明两个类全局变量,

    private boolean isCustomTabsLaunched = false;
    private long customTabsEnterTime;

步骤 - 2:在启动URL时将上述值设置为变量。

    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);
        }
    });

步骤 - 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);
    }

为了使代码更加健壮,您可能希望将布尔值isCustomTabsLaunched和长整型customTabsEnterTime存储在首选项或数据库中,以便在任何情况下这两个参数都会被销毁,因为如果用户在Chrome自定义选项卡中停留时间很长,您的活动可能会在后台被销毁。

1
谢谢您的回答@Chitrang,您的解决方案存在问题,如果在Chrome导航期间杀死活动,则不会调用onResume(),您将丢失数据 :/ - Dichoben
你是想结束这个Activity吗?如果是想结束整个应用程序,那么没有任何解决方案可以实现。 - Chitrang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接