【发布时间】:2016-03-28 17:55:37
【问题描述】:
我想在应用完成 URL 加载之前显示启动画面。
经过搜索,找到了解决办法——Splash screen while loading a url in a webview in android app
但我无法在我当前的代码上实现它。我在尝试按 id 获取启动图像视图时收到错误消息。
这是我的代码:
public class MyAppWebViewClient extends WebViewClient {
private final Context context;
public MyAppWebViewClient(Context context) {
this.context = context;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("?dl=1")) {
Uri source = Uri.parse(url);
// Make a new request pointing to the download url
DownloadManager.Request request = new DownloadManager.Request(source);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(url);
String fileName = URLUtil.guessFileName(url, null, fileExtension);
// save the file in the "Downloads" folder of SDCARD
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
return true;
}
if (Uri.parse(url).getHost().contains("www.example.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
view.loadUrl("file:///android_asset/offline.html");
}
@Override
public void onPageFinished(WebView view, String url) {
//hide loading image
myImageView = (ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);
//show webview
view.findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
}
}
和activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.MainActivity">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<ImageView
android:id="@+id/activity_splash_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/splash_logo"
android:visibility="visible"
/>
</RelativeLayout>
这一行出现的问题:
myImageView = (ImageView) findViewById(R.id.activity_splash_logo).setVisibility(View.GONE);
mainactivity.java
package com.myapp;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 0;
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient(MainActivity.this));
mWebView.loadUrl("http://www.example.com/");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Request required permission
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_EXTERNAL_STORAGE);
}
// Add user agent suffix
this.mWebView.getSettings().setUserAgentString(
this.mWebView.getSettings().getUserAgentString()
+ " "
+ BuildConfig.APPLICATION_ID
+ "/"
+ BuildConfig.VERSION_CODE
);
}
// Prevent the back-button from closing the app
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
【问题讨论】:
-
您链接到的帖子在
Activity中包含代码,您可以在其中调用findViewById。您的代码在WebViewClient内,因此您不能调用findViewById,因为这不是该类的方法。