Web开放式意图://协议

时间:2016-02-24 17:33:41

标签: android android-intent webview

当我将url加载到WebView并尝试打开链接时,其中一些会显示错误页面,如:

  

net :: ERR_UNKNOWN_URL_SCHEME意图://maps.yandex.ru?utm_medium = tab ...

如何打开此链接,或者至少检查是否安装了相应的应用并运行它?

我试图通过实现自定义WebViewClient来覆盖网址加载:

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (!url.startsWith("http")) {
            getActivity().startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url)));
            return true;
        }
        return false;
    }

但在这种情况下应用程序崩溃与ActivityNotFoundException。

02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=intent://maps.yandex.ru?utm_medium=tab-maps&text=магазин&utm_source=serp&yandexuid=2258091361456330110 }
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at android.app.Activity.startActivityForResult(Activity.java:3930)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at android.app.Activity.startActivityForResult(Activity.java:3890)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at android.app.Activity.startActivity(Activity.java:4213)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at android.app.Activity.startActivity(Activity.java:4181)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at ru.uxapps.voicesearch.FWeb$CustomWebViewClient.shouldOverrideUrlLoading(FWeb.java:70)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at com.android.webview.chromium.WebViewContentsClientAdapter.shouldOverrideUrlLoading(WebViewContentsClientAdapter.java:325)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(AwContentsClientBridge.java:266)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:37)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at android.os.Looper.loop(Looper.java:148)
02-24 20:58:51.886 31255-31255/ru.uxapps.voicesearch W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
02-24 20:58:51.887 31255-31255/ru.uxapps.voicesearch W/System.err:     at java.lang.reflect.Method.invoke(Native Method)

即使安装了必需的应用,应用也会崩溃。但如果在Chrome中打开此链接,则会显示maps.yandex.ru网站,或者如果已安装,则会打开应用。

是否有常见解决方案,适用于所有intent://链接? 谢谢你的建议。

1 个答案:

答案 0 :(得分:7)

我找到了解决方案。 This questionthis documentation帮助我了解情况。

结果,我写了一个遵循这个逻辑的链接处理程序:

  1. 在同一WebView中打开http和https
  2. 尝试处理已知方案(电话:等)
  3. 如果找不到已知活动,则解析intent:scheme并尝试运行相应的应用程序
  4. 如果未安装所需的应用,请尝试将提供的后备网址加载到当前的WebView
  5. 如果没有提供后备网址,请重定向到市场并要求安装所需的应用
  6. 代码是:

     from multiprocessing import Pool
    
     pool = Pool(NUMBER_OF_CORES)
     pool.map()
    
     def f(args):
         files, list1, list2 = args
         dict_one =  defaultdict(set)
         for line in file_text:
             for x in list1:
                 for xx in list2:
                     if x in line and xx in line:
                         dict_one[x].add(xx) 
    
    if __name__ == '__main__':
        p = Pool(NUMBER_OF_CORES)
        chunk_size= len(files) //NUMBER_OF_CORES
        print(p.map(f, [(files[i*chunk_size:(i+1)*chunk_size], list1, list2) for i in range(NUMBER_OF_CORES)]))
    

    也许它需要一些清理,但它可能会有所帮助。请告诉我,如果您知道更简单的方法,我仍然在寻找最佳解决方案。