背景
package com.example.neurofinance;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.TargetApi;
import android.content.Context;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.View。
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest。
import android.webkit.WebSettings。
import android.webkit.WebView。
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public class WebAppInterface {
Context mContext。
/**實體化介面并設定背景關系 */
WebAppInterface(Context c) {
mContext = c;
}
/** 顯示網頁上的祝酒詞 */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext,toast,Toast.LENGTH_SHORT).show()。
}
}
@Override
public boolean onCreateOptionsMenu(Menu) {
return true;
}
@Override; }
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(s savedInstanceState)。
setContentView(R.layout.activity_main)。
this.getWindow().getDecorView().setSystemUiVisibility(
View.SYST_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN)。)
WebView webView = findViewById(R.id.webView)。
webView.addJavascriptInterface(new WebAppInterface(this), "Android") 。
webView.getSettings().setDomStorageEnabled(true)。
webView.getSettings().setJavaScriptEnabled(true)。
webView.getSettings().setUseWideViewPort(true);//setting wide view。
webView.getSettings().setLoadWithOverviewMode(true);//setting default zoomed out view。
webView.setInitialScale(1)。
webView.getSettings().setBuiltInZoomControls(true);//設定縮放控制。
webView.loadUrl("https://neurofinance.rf.gd/")。
WebViewClient webViewClient = new WebViewClient() {
@SuppressWarnings("deprecation") @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url)。
return true。
}
@Override; }
public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@TargetApi(Build.VERSION_CODES.N) @Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
view.loadUrl(request.getUrl().toString())。
return true。
}
};
webView.setWebViewClient(webViewClient)。
if (Build.VERSION.SDK_INT >= 21)
webView.getSettings().setMixedContentMode( WebSettings.MIXED_CONTENT_ALWAYS_ALLOW ) 。
}
- AndroidManifest.xml 。
<?xml version="1.0" encoding="utf-8"? >
<manifest xmlns:android="http://schemas.android.com/apk/res/android"/span>
package="com.example.neurofinance"/span>>
<uses-permission android:name="android.permission.INTERNET"/span>/>
<application
android:allowBackup="true"。
android:icon="@mipmap/ic_launcher"。
android:label="@string/app_name"。
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"。
android:theme="@style/AppTheme"/span>>
<activity android:name=" .MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"/span>>
<intent-filter>/span>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/span> />
</intent-filter>
</activity>/span>
</application>/span>
</manifest>
- activity_main.xml 。
<?xml version="1.0" encoding="utf-8"? >
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
xmlns:app="http://schemas.android.com/apk/res-auto"。
xmlns:tools="http://schemas.android.com/tools"。
android:layout_width="match_parent"。
android:layout_height="match_parent"。
tools:context=".MainActivity"/span>>
<WebView。
android:id="@ id/webView"。
android:layout_width="match_parent"。
android:layout_height="match_parent"。
/>
</androidx.constraintlayout.widget.ConstraintLayout>
uj5u.com熱心網友回復:
WebView不支持網站用來顯示通知的API,而瀏覽器則支持該API。這就是為什么你在瀏覽器中看到通知,但在WebView中卻看不到。 請參閱本頁,了解支持通知 API 的環境串列。
如果您的 WebView 顯示一個由您控制的網站,您可以修改該網站以使用一些其他機制來顯示通知(通過一些 java 介面)。
uj5u.com熱心網友回復:
這對你是否有用?
https://github.com/ashraf-alsamman/android-webview-app-with-push-notification
https://medium.com/shibinco/creating-a-webview-android-app-with-push-notification-7fd48541a913
希望你能從這些例子中的一個使其發揮作用
。uj5u.com熱心網友回復:
我不確定我能提供什么幫助,但你可能只讓代碼支持網站,而不是實際的應用程式。我自己是個初學者,所以我知道的不多,但我想這是這里的問題。
uj5u.com熱心網友回復:
我沒有足夠的 "聲譽 "來發表評論,但這對你是否有用呢?
https://github.com/ashraf-alsamman/android-webview-app-with-push-notification
https://medium.com/shibinco/creating-a-webview-android-app-with-push-notification-7fd48541a913
希望你能從這些例子中的一個使其作業
。轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/313065.html
標籤:
上一篇:如何通過一個變數查詢一個鍵?

