【问题标题】:andorid daydream service webview (context requires the flag_activity_new_task flag)android daydream service webview(上下文需要flag_activity_new_task标志)
【发布时间】:2014-01-07 16:06:54
【问题描述】:

我已经实现了 android daydream 服务,我在其中播放了来自 asserts 的视频, 我在那里放了一个按钮和 onclick 侦听器我想打开一个带有链接的 webview,一切正常,但我无法处理 webview,因为当我单击按钮时出现此消息

不幸的是应用程序已停止

日志中出现的错误信息是

AndroidRuntime(827):致命异常: android.util.AndroidRuntimeException:从 Activity 上下文外部调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?

我已经在 xml 文件中创建了 webview

我调用 webview 的代码是:

final WebView webView = (WebView)this.findViewById(R.id.webView1);

   button.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
           // Perform action on click   
           vv.stopPlayback();
           vv.setVisibility(View.GONE);//for media player

           webView.setVisibility(View.VISIBLE);
           webView.loadUrl("http://www.google.com");
       }
   });

【问题讨论】:

    标签: android webview android-webview android-service android-context


    【解决方案1】:

    经过大量搜索,我找到了我的问题的答案,这是对我最有帮助的链接

    link

    基本上我只是把这个类代码放在我的 android dreamservice 中

        private class LinkLauncher extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
            return true;
        }
    

    并使用 webview 如下

               WebView webview = (WebView) findViewById(R.id.webView1);
               webview.setVisibility(View.VISIBLE);
               webview.setWebViewClient(new LinkLauncher());
    
               WebSettings webSettings = webview.getSettings();
               webSettings.setJavaScriptEnabled(true);
    
               webview.loadUrl("http://www.google.com");
    

    我写的答案可能对其他人有帮助。 谢谢

    【讨论】:

    • 您甚至不需要实现自定义 WebViewClient。只需在 WebView 上设置一个空的 WebViewClient 就可以解决问题(是的,这很令人惊讶 :))。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2019-07-17
    相关资源
    最近更新 更多