Android:后退按钮跳过webView

时间:2015-03-17 18:37:53

标签: java android

我是一名构建动态壁纸的新手开发者。我有一个设置屏幕,可以打开webview浏览我的网页,该网页包含各种视频文件的链接:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

这由以下人员控制:

    public class intMarket extends Activity {
        public static final String SHARED_PREFS_NAME="videowallpapersettings";

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

            WebView myWebView = (WebView) findViewById(R.id.webview);
            myWebView.setWebViewClient(new WebViewClient());
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

            String tUrl = getString(R.string.urlMarket);
            myWebView.loadUrl(tUrl);
        }

        public class  WebAppInterface {
            Context mContext;

            /** Instantiate the interface and set the context */
            WebAppInterface(Context c) {
                mContext = c;
            }

            /** previewVideo(pUrl) - Downloads a video from a url and plays it in the app. */
            @JavascriptInterface
            public void previewVideo(String pUrl) {
                final DownloadTask downloadTask = new DownloadTask(intMarket.this);
                downloadTask.execute(pUrl);
            }
        }
    }


    // usually, subclasses of AsyncTask are declared inside the activity class.
    // that way, you can easily modify the UI thread from here
    private class DownloadTask extends AsyncTask<String, Integer, String> {
        private Context context;
        private PowerManager.WakeLock mWakeLock;

        public DownloadTask(Context context) {
            this.context = context;
        }

        @Override
        protected String doInBackground(String... sUrl) {
            InputStream input = null;
            OutputStream output = null;
            ...
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            if (result != null)
                Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show();
            else {
                File sdCard = Environment.getExternalStorageDirectory();
                String rootDirectoryName = getString(R.string.rootDirectoryName);
                File directory = new File(sdCard.getAbsolutePath() + "/" + rootDirectoryName + "/Previews");
                String path = directory + "/preview.mp4";

                setContentView(R.layout.videopreview);
                VideoView myVideoView = (VideoView) findViewById(R.id.myvideoview);
                myVideoView.setVideoPath(path);
                myVideoView.setMediaController(new MediaController(getBaseContext()));
                myVideoView.requestFocus();
                myVideoView.start();
            }
        }
    }

当用户点击缩略图时,会触发另一个下载和播放视频的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
            <VideoView
                android:id="@+id/myvideoview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
        </LinearLayout>
    </LinearLayout>

一切都很完美。用户单击设置按钮后将显示该网页。当用户点击链接时,它会下载预览视频并跳转到视频播放器。但是,当用户使用后退按钮时,它们会一直返回到设置窗格。这是它最终工作的方式:

 settings > browser > video > [back] > settings

这就是我期望它发挥作用的方式:

 settings > browser > video > [back] > browser

为什么浏览器会跳过后退按钮?

1 个答案:

答案 0 :(得分:0)

&#34;如果&#34;我了解您的问题,您可以尝试为您的网络视图添加onKeyDown(),如下所示:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }