0

我正在测试 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://stackoverflow.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(){
}

}

谢谢!

4

1 回答 1

1

CustomTabs 忽略mayLaunchUrl()从后台发送的信息。这有两个原因:

  1. 避免浪费带宽的简单措施(很容易检查来自同一会话的自定义选项卡不是前台,只是还没有这样做)

  2. 通过当前加载的页面mayLaunchUrl()不保留Window.sessionStorage(),这对于转换来说不是问题App -> CustomTab,但是对于在单个 CustomTab 中导航,这可能会打破用户对导航模式的假设,例如:

站点A -> 站点B -> 站点A

(第二次访问SiteA看不到会话中的内容)。教mayLaunchUrl()选正义sessionStorage()是复杂的,目前还没有计划。

于 2016-05-25T10:46:39.677 回答