gpt4 book ai didi

android - Webview 应用程序在第一次安装应用程序时工作,但之后只显示空白屏幕

转载 作者:行者123 更新时间:2023-11-29 02:19:26 25 4
gpt4 key购买 nike

第一次安装应用程序时,一切正常,但关闭应用程序并再次打开后,只有白屏。网站未加载。

MainActivity.java文件

package app.freeairdrop.io;
import ....

public class MainActivity extends Activity{
private ProgressBar progressBar;
private WebView webView;
private SwipeRefreshLayout mySwipeRefreshLayout;
private boolean doubleBackToExitPressedOnce;

@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClientDemo());
webView.setWebChromeClient(new WebChromeClientDemo());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);


webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
// webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
mySwipeRefreshLayout =
(SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);


mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
mySwipeRefreshLayout.setRefreshing(false);
}
}
);



private class WebViewClientDemo extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
if (uri.getHost() != null && (url.startsWith("https://freeairdrop.io/") || url.startsWith("https://www.freeairdrop.io/"))) {
return false;
}

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
}


private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else {
}
return super.onKeyDown(keyCode, event);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@Override
// This method is used to detect back button
public void onBackPressed() {
if (this.webView.canGoBack()) {
this.webView.goBack();
return;
}

else {
// Let the system handle the back button
super.onBackPressed();
}
}


@Override
protected void onStart() {
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();

if (getIntent().getExtras() != null) {
webView.loadUrl(String.valueOf(appLinkData));

} else if (getIntent().getExtras() == null){
webView.loadUrl("https://freeairdrop.io/");

}else {
webView.loadUrl("https://freeairdrop.io/");
}
super.onStart();


}

@Override
protected void onResume() {
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();

if (getIntent().getExtras() != null) {
webView.loadUrl(String.valueOf(appLinkData));

} else if (getIntent().getExtras() == null){
webView.loadUrl("https://freeairdrop.io/");

}else {
webView.loadUrl("https://freeairdrop.io/");
}
super.onResume();
}

}

AndroidMamifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="app.freeairdrop.io">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
android:appCategory="productivity"
android:hardwareAccelerated="true"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name=".ApplicationClass"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name="app.freeairdrop.io.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:scheme="https"
android:host="freeairdrop.io" />
</intent-filter>


</activity>
</application>


</manifest>

每当我使用网站链接访问应用程序时,使用深度链接它每次都能正常工作,即如果有人在 whatsapp 上获得指向我网站的链接并且他单击它并使用我的应用程序打开该链接,它会完美打开。就像如果我们在 whatsapp 上获得视频链接,我们就可以打开 youtube 应用程序。

最佳答案

getIntent().getExtras() 可能不为空但具有非 URL 值。在使用 URLUtil 将值传递给 webview 之前,再执行一个额外的步骤来检查该值

修改 block onResumeonStart

String gotoUrl = String.valueOf(appLinkData);

if(URLUtil.isValidUrl(gotoUrl))
webView.loadUrl(gotoUrl);
else webView.loadUrl("https://freeairdrop.io/");

关于android - Webview 应用程序在第一次安装应用程序时工作,但之后只显示空白屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57341027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com