我想显示一个启动画面,直到应用完成加载URL。
搜索后,我发现一个解决方案 - Splash screen while loading a url in a webview in android app无法通过编号获取ImageView
但我无法实现它在我当前的代码。尝试按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',因为那不是该类的一个方法。 –