java Android:后退按钮跳过webView
我是一个新手开发建设一个活的壁纸。我有一个设置屏幕,可以打开webview来浏览我的网页,其中包含指向各种视频文件的链接:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/webview"
安卓:layout_width="fill_parent"
安卓: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:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
>
<LinearLayout
安卓:orientation="vertical"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
>
<VideoView
安卓:id="@+id/myvideoview"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
一切都很好。用户单击“设置”按钮后,将显示该网页。当用户单击链接时,它会下载预览视频并跳到视频播放器。但是,当用户使用“上一步”按钮时,它们会一直返回到“设置”窗格。这就是它最终的工作方式:
settings > browser > video > [back] > settings
这就是我期望它的工作方式:
settings > browser > video > [back] > browser
为什么浏览器会被“后退”按钮跳过
# 1 楼答案
“如果”我理解你的问题,你可以尝试为你的网络视图添加一个
onKeyDown()
,如下所示: