I have an application using Chrome custom tabs
to open some links, I need to have event each second during all the time the user stay on Chrome, or know how many time he stay on Chrome. For me the only way to do it is to use a Service
. Is it possible to do it differently?
Create your YourBroadCastReceiver class as follows
public class YourBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Called every 60 seconds","called");
}
}
After starting your custom tab successfully create Alarm PendingIntent that will trigger YourBroadCastReceiver once every 60 sec.
// 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);
after closing your custom tab never forget to cancel your PendingIntent
PendingIntent.getBroadcast(
context, _pendingIntentId, alarmIntent, 0).cancel();
For implementation of chrome custom tabs I've followed this tutorial, github link.
My solution basically rely on boolean and System.currentTimeMillis().
Step - 1 : Declare two class global variables,
private boolean isCustomTabsLaunched = false;
private long customTabsEnterTime;
Step - 2 : Set values for above to variables when 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);
}
});
Step - 3 : Calculate stay time in onResume method.
@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);
}
In order to make code more robust you may like to store boolean isCustomTabsLaunched and long customTabsEnterTime in preferences or database so in any case these two params get destroyed as your activity may get destroy in background if user stay for long time in chrome custom tab.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With