Android 侦听chrome自定义选项卡进度事件

Android 侦听chrome自定义选项卡进度事件,android,chrome-custom-tabs,Android,Chrome Custom Tabs,我有一个使用Chrome自定义选项卡的应用程序要打开一些链接,我需要在用户停留在Chrome上的所有时间内每秒都有事件,或者知道用户停留在Chrome上的时间。对我来说,唯一的办法就是使用服务。有可能做得不同吗 对于chrome自定义选项卡的实现,我遵循了教程github 我的解决方案基本上依赖于布尔值和System.currentTimeMillis() 步骤-1:声明两个类全局变量 private boolean isCustomTabsLaunched = false; p

我有一个使用
Chrome自定义选项卡的应用程序
要打开一些链接,我需要在用户停留在Chrome上的所有时间内每秒都有事件,或者知道用户停留在Chrome上的时间。对我来说,唯一的办法就是使用
服务。有可能做得不同吗

对于chrome自定义选项卡的实现,我遵循了教程github

我的解决方案基本上依赖于布尔值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);
    }

为了使代码更加健壮,您可能希望在首选项或数据库中存储boolean isCustomTabsLaunched和long customTabsEnterTime,因此在任何情况下,这两个参数都会被销毁,因为如果用户在chrome custom tab中停留很长时间,您的活动可能会在后台被销毁。

创建您的YourBroadCastReceiver类,如下所示

public class YourBroadCastReceiver extends BroadcastReceiver {

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

}
启动自定义选项卡后,成功创建每60秒触发一次BroadcastReceiver的报警挂起内容

    // 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.getBroadcast(
       context, _pendingIntentId, alarmIntent, 0).cancel();

听起来好像您想要侦听在关闭自定义选项卡时触发的事件。这是正确的吗?是的,如果我在回调中有用户会话的总时间,它可能会起作用,但最好是每X次有一个事件。感谢您的回答@Chitrang,您的解决方案的问题是如果您在chrome导航期间终止活动,
onResume()
将永远不会被调用,您将丢失数据:/n您将如何终止活动?或者您的意思是杀死应用程序,在这种情况下,没有解决方案会起作用。