如何销毁WebView Activity和WebView中的视频?

发布于 2024-11-30 19:13:15 字数 1331 浏览 0 评论 0原文

我在webview中播放了一个swf视频,当我按back_key时,声音仍然存在..我不知道如何销毁它...请帮助我,代码如下:

public class Video extends Activity {
    private WebView webview = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        Intent intent = getIntent();
        String url = intent.getExtras().getString("Url");
        Log.i("URL OF VIDEO IS", url);
        setTitle("Video");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.webpageview);
        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setPluginsEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl(url);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //How can I destroy the Activity? I tried finish() it, but it doesn't works.
            webview.removeAllViews();
            return super.onKeyDown(keyCode, event);
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}

I played a swf video in a webview, when I press back_key, the sound still exists.. I don't know how to destroy it...plz help me, code is as following:

public class Video extends Activity {
    private WebView webview = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        Intent intent = getIntent();
        String url = intent.getExtras().getString("Url");
        Log.i("URL OF VIDEO IS", url);
        setTitle("Video");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.webpageview);
        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setPluginsEnabled(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl(url);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //How can I destroy the Activity? I tried finish() it, but it doesn't works.
            webview.removeAllViews();
            return super.onKeyDown(keyCode, event);
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-12-07 19:13:15

在您使用之前,我们认为您至少需要了解这一点。

LINK1

LINK2

编辑

始终使用应用程序上下文实例化 webview。

webView = new WebView(getApplicationContext());

如果你想要 xml 格式的 webview。扩展它

    public MyWebView(Context context, AttributeSet attrs) {
    super(getApplicationContext(), attrs);
}

并像这样使用它

<com.myapp.MyWebView ...

,并在您不再需要视图的所有地方调用它。

webView.destroy();    

因为有时候Activity.onDestroy、Activity.onStop可能不会被调用。

before you use we view you need to be aware of atleast this.

LINK1

and

LINK2

EDIT

always instantiate webview using application context.

webView = new WebView(getApplicationContext());

if you want webview in xml. extend it

    public MyWebView(Context context, AttributeSet attrs) {
    super(getApplicationContext(), attrs);
}

and use it like this

<com.myapp.MyWebView ...

and call this in all the places where you don't want the view any more.

webView.destroy();    

because there are times when Activity.onDestroy, Activity.onStop may not be called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文