Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop Flash after leaving a WebView?

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) {
        }
    }
}

}
like image 997
Fenderf4i Avatar asked Jun 01 '11 13:06

Fenderf4i


3 Answers

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

like image 188
speedplane Avatar answered Oct 12 '22 23:10

speedplane


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

webview.stopLoading(); 
webview.loadUrl("");
webview.reload();
webview = null;
like image 34
Pawan Yadav Avatar answered Oct 12 '22 22:10

Pawan Yadav


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

like image 36
arsalank2 Avatar answered Oct 13 '22 00:10

arsalank2