简体   繁体   中英

andorid daydream service webview (context requires the flag_activity_new_task flag)

I have implemented the android daydream service in which I have played a video from asserts , I have put a button there and onclick listener I want to open a webview with a link, every things goes right but I can't able to deal with the webview as when I clicks the button this message appear

Unfortunatley App has stopped

and the error message that appears in log is

AndroidRuntime(827): FATAL EXCEPTION: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity con text requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

I have created the webview in the xml file

my code for calling webview is:

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");
       }
   });

After a lot of search i found answer of my question here is the link that helps me most

link

basically i just put this class code in my 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;
    }

and uses the webview as follow

           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");

i write answer that might help anyone else. thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM