我有一個應用程序使用Chrome custom tabs
來打開一些鏈接,我需要在用戶每次呆在Chrome上時每秒都有活動,或知道他在Chrome上呆多少次。對我來說,唯一的方法就是使用Service
。有可能以不同的方式做到這一點嗎?聽chrome自定義選項卡進度事件
17
A
回答
1
對於鉻自定義選項卡的實現我已遵循this教程,github link。
我的解決方案基本上依賴於布爾型和System.currentTimeMillis()。
步驟 - 1:聲明兩個類全局變量,
private boolean isCustomTabsLaunched = false;
private long customTabsEnterTime;
步驟 - 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);
}
});
步驟 - 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因此在任何情況下,這兩個參數被摧毀,如同您的活動可能會破壞在後臺用戶久留在鉻自定義選項卡中的時間。
2
創建YourBroadCastReceiver類,如下所示
public class YourBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Called every 60 seconds","called");
}
}
開始您的自定義選項卡成功地創建報警的PendingIntent將觸發YourBroadCastReceiver每隔60秒後。
// 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. WebView與Chrome自定義選項卡
- 2. 從Chrome自定義選項卡回撥
- 3. Chrome自定義選項卡Oauth2流程
- 4. 關閉chrome自定義選項卡
- 5. Chrome自定義選項卡不工作
- 6. Chrome新選項卡事件
- 7. 聆聽自定義事件
- 8. 添加一個收聽者到Chrome自定義選項卡關閉按鈕
- 9. Jquery自定義選項卡
- 10. Chrome選項卡和進程
- 11. 自定義選項卡形狀的ASP.NET選項卡控件
- 12. Chrome自定義選項卡。設置多個工具欄項目
- 13. 紙張選項卡事件監聽器更改選定?
- 14. CloseableTabs的自定義選項卡控件
- 15. Chrome自定義選項卡不會在重定向時關閉
- 16. Chrome自定義選項卡不回到應用程序
- 17. Chrome自定義選項卡,與Oauth2深層鏈接
- 18. Chrome自定義選項卡和科爾多瓦
- 19. 在Chrome自定義選項卡中清除瀏覽數據
- 20. 關閉Chrome自定義選項卡的回調
- 21. Chrome自定義選項卡 - android - 用戶代理
- 22. 在Chrome自定義選項卡中檢測到內存泄漏
- 23. Chrome自定義選項卡隱藏地址欄
- 24. jQuery Mobile選項卡 - 選項卡選定的事件
- 25. 自定義Backstage視圖選項卡像標準選項卡FileNew
- 26. 在窗體上移動自定義選項卡/子選項卡
- 27. Actionscript3正在監聽自定義事件
- 28. TypeScript自定義事件偵聽器
- 29. GWT聽外部自定義JavaScript事件
- 30. React自定義事件偵聽器
這聽起來像你想監聽當自定義選項卡關閉時被觸發的事件。它是否正確? – ade
是的,如果我在回調中有用戶會話的總時間,但最好是每X次都有一個事件。 – Dichoben