Chrome 自定义标签会在浏览时预加载网页吗?
Posted
技术标签:
【中文标题】Chrome 自定义标签会在浏览时预加载网页吗?【英文标题】:Chrome Custom Tabs pre-load webpages while browsing? 【发布时间】:2016-09-21 20:23:14 【问题描述】:我正在测试 Chrome 自定义标签。它应该预加载用户可能在后台点击的网页。这是通过调用
session.mayLaunchUrl(uri, null, null);
当 MainActivity 处于活动状态时,网页会很好地预加载,并且在启动 URL 时,网页会按预期快速加载。 但是,我想在用户已经浏览时自动向用户提供其他网页(因此活动在后台)。然后,预加载网页似乎不再加快加载过程,即使正在加载新提供的网页,这也会缓慢发生。
我编写了一个小应用程序来演示这种行为。手动预加载和启动 URL 效果很好(因为活动是活动的,我想),自动在循环中提供新网页很慢(因为活动不活动并且 mayLaunchUrl 方法不能按预期工作)。
是否可以在用户已经浏览时使用预加载机制?如果是,如何?
我添加了 MainActivity 代码,如下示例:
public class MainActivity extends AppCompatActivity
private CustomTabsSession mCustomTabsSession;
private CustomTabsClient mClient;
private CustomTabsServiceConnection mConnection;
private EditText urlET;
private String TAG = "MainActivity";
private ArrayList<String> urlList;
private Thread cycleThread;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("MainActivity", "onCreate");
urlList = new ArrayList<>();
urlList.add("http://www.google.com");
urlList.add("https://github.com");
urlList.add("http://***.com");
urlList.add("http://www.heise.de");
// pre launch the chrome browser, bind services etc
warmup();
urlET = (EditText) findViewById(R.id.urlID);
// pre load a webpage manually
Button prepareBt = (Button) findViewById(R.id.prepareBt);
assert prepareBt != null;
prepareBt.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
mayLaunch(null);
);
//launch webpage manually
Button launchBt = (Button) findViewById(R.id.launchBt);
assert launchBt != null;
launchBt.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
launch(null);
);
//start a loop that serves webpages every 10 seconds
Button cycleBt = (Button) findViewById(R.id.cycleBt);
assert cycleBt != null;
cycleBt.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if(cycleThread !=null)
cycleThread.interrupt();
cycleThread = new Thread(cycle);
cycleThread.start();
);
private Runnable cycle = new Runnable()
@Override
public void run()
int i = 0;
mayLaunch(Uri.parse(urlList.get(i)));
try
Thread.sleep(5000);
while (true)
try
Log.d(TAG, "launch: "+urlList.get(i));
launch(Uri.parse(urlList.get(i)));
i++;
if(i>=urlList.size())
i=0;
Thread.sleep(5000);
Log.d(TAG, "prepare: "+urlList.get(i));
mayLaunch(Uri.parse(urlList.get(i)));
Thread.sleep(5000);
catch (InterruptedException e)
e.printStackTrace();
break;
catch (InterruptedException e)
e.printStackTrace();
;
private void mayLaunch(Uri uri)
if(uri ==null)
uri = Uri.parse(urlET.getText().toString());
CustomTabsSession session = getSession();
session.mayLaunchUrl(uri, null, null);
private void launch(Uri uri)
if(uri ==null)
uri = Uri.parse(urlET.getText().toString());
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder(getSession())
.setToolbarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))
.setShowTitle(true)
.setStartAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
.setExitAnimations(this, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
.build();
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
customTabsIntent.launchUrl(this,uri);
public CustomTabsSession getSession()
if (mClient == null)
mCustomTabsSession = null;
else if (mCustomTabsSession == null)
mCustomTabsSession = mClient.newSession(null);
Log.d(TAG, "getSession: created new session");
return mCustomTabsSession;
private void warmup()
if (mClient != null) return;
String packageName = "com.android.chrome";
if (packageName == null) return;
mConnection = new CustomTabsServiceConnection()
@Override
public void onCustomTabsServiceConnected(ComponentName componentName, CustomTabsClient customTabsClient)
mClient = customTabsClient;
mClient.warmup(0L);
@Override
public void onServiceDisconnected(ComponentName name)
mClient = null;
mCustomTabsSession = null;
;
CustomTabsClient.bindCustomTabsService(this, packageName, mConnection);
private void coolDown()
if (mConnection == null) return;
unbindService(mConnection);
mClient = null;
mCustomTabsSession = null;
mConnection = null;
public void onDestroy()
Log.v("MainActivity", "onDestroy");
super.onDestroy();
coolDown();
@Override
public void onBackPressed()
谢谢!
【问题讨论】:
【参考方案1】:CustomTabs 忽略从后台发送的mayLaunchUrl()
。这有两个原因:
避免浪费带宽的简单措施(很容易检查来自同一会话的自定义选项卡不是前台,只是还没有这样做)
通过mayLaunchUrl()
加载的页面当前不保留Window.sessionStorage()
,这对于App -> CustomTab
转换来说不是问题,但是对于在单个CustomTab 中导航,这可能会打破用户对导航模式的假设,例如:
站点A -> 站点B -> 站点A
(第二次访问SiteA
看不到会话中的内容)。教mayLaunchUrl()
选择正确的sessionStorage()
很复杂,目前还没有这方面的计划。
【讨论】:
感谢您的解释。那么您知道我可以采取的任何解决方法(在相当大的努力范围内)吗? 如果您控制循环浏览的页面,它们可以发出 甚至以上是关于Chrome 自定义标签会在浏览时预加载网页吗?的主要内容,如果未能解决你的问题,请参考以下文章
从 Chrome 中的 css 自定义样式按钮中删除蓝色边框
javascript Chrome扩展程序将自定义javascript文件加载到标签的DOM中。
如何让firefox和chrome支持css自定义鼠标样式?IE中用“cursor:url()”属性可以实现,FF、Chrome怎么弄?