11

I have an app that I've put together to stream flash video in a webview when a user clicks a button.

It does this fine, but after backing out or losing focus, it looks like it continues to use data for a while until I assume when the system shuts the activity down. If I manually kill out of the activity screen, data use stops almost immediately. Just backing out and it can keep going for a while.

Can someone help me out with my code, I would really appreciate it!

import java.lang.reflect.Method;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class Video extends Activity {


    private WebView webview;


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.video);


    webview = (WebView) findViewById(R.id.webview);

// resumeTimers() to account for the webview refcount bug (hopefully)
    webview.resumeTimers();
    WebSettings webSettings = webview.getSettings();
    webview.getSettings().setJavaScriptEnabled(true);
    webSettings.setPluginsEnabled(true);
    webview.setVerticalScrollBarEnabled (false);
    webview.setHorizontalScrollBarEnabled (false);

    webview.loadUrl("http://www.nasa.gov/multimedia/nasatv/nasatv_android_flash.html");
}


@Override
protected void onPause() {
pauseBrowser();
super.onPause();
}

@Override
protected void onResume() {
resumeBrowser();
super.onResume();
}



private void pauseBrowser() {

// pause flash and javascript etc
callHiddenWebViewMethod(webview, "onPause");
webview.pauseTimers();
}

private void resumeBrowser() {

// resume flash and javascript etc
callHiddenWebViewMethod(webview, "onResume");
webview.resumeTimers();
}

private void callHiddenWebViewMethod(final WebView wv, final String name){
    if( webview != null ){
        try {
            Method method = WebView.class.getMethod(name);
            method.invoke(webview);
        } catch (final Exception e) {
        }
    }
}

}
anticafe
  • 6,816
  • 9
  • 43
  • 74
Fenderf4i
  • 115
  • 1
  • 9

6 Answers6

13

I'm a bit confused by the question, but I think you're saying that flash video keeps on playing even after the activity is closed. I ran into a similar issue. The following worked for me:

@Override
protected void onDestroy() {
    super.onDestroy();
    final WebView webview = (WebView)findViewById(R.id.webPlayer);
    // Calling .clearView does not stop the flash player must load new data
    webview.loadData("", "text/html", "utf-8");
}

I posted this same solution here: android WebView stop Flash plugin onPause

Community
  • 1
  • 1
speedplane
  • 15,673
  • 16
  • 86
  • 138
9

This approach works me. I call it on back button press.

webview.stopLoading(); 
webview.loadUrl("");
webview.reload();
webview = null;
Pierre GM
  • 19,809
  • 3
  • 56
  • 67
Pawan Yadav
  • 1,772
  • 19
  • 17
4

Calling webvew.destroy() in onDestroy() worked for me.

arsalank2
  • 1,683
  • 1
  • 16
  • 25
2

If you are as lucky as I am, even this won't be enough to end a youtube streaming! The sound is still playing no matter what!

@Override
protected void onDestroy() {
    super.onDestroy();

    // Stopping a webview and all of the background processes (flash,
    // javascript, etc) is a very big mess.
    // The following steps are to counter most of the issues seen on
    // internals still going on after the webview is destroyed.

    mWebView.stopLoading();
    mWebView.loadData("", "text/html", "utf-8");
    mWebView.reload();

    mWebView.setWebChromeClient(null);
    mWebView.setWebViewClient(null);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.webviewRelativeLayout);
    layout.removeView(mWebView);
    mWebView.removeAllViews();
    mWebView.clearHistory();
    mWebView.destroy();
    mWebView = null;
}
slash33
  • 899
  • 1
  • 11
  • 17
2

I combined some of the answers here, and this worked for me:

@Override
protected void onDestroy()
{
    super.onDestroy();

    webView.stopLoading();
    webView.loadData("", "text/html", "utf-8");
    webView.destroy();
}
electronix384128
  • 6,625
  • 11
  • 45
  • 67
1

The webViwe destory approach did not work for me - it worked well on the first close of the webview, but subsequent calls to the webview caused a crash.

I ended up doing this:

_webView.loadData("", "text/html", "utf-8");

from my activity finish() method.

Israel Roth
  • 441
  • 3
  • 9
  • so the solution is public void finish() { super.finish(); Log.d(TAG, "finish activity youtube"); //fix youtube continue sound after finish web.loadData("", "text/html", "utf-8"); it's ok ??? My device have no problem, but I got complaints from users , so tell me please is it ok like this – idan Jun 05 '13 at 19:15
  • exactly, but make sure you call the super.finish() AFTER you call the loadData – Israel Roth Aug 19 '13 at 20:58