问题描述
我的网站在 wordpress 中,使用谷歌登录,在桌面上运行良好。但是android WebView 应用程序有问题。
下面是我的代码:
AndroidManifest.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"
android:installLocation="auto"
package="com.xyz.webviewapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!--
You can easily change the main theme. Just modify application.android:theme attribute.
There are 10 themes you can use:
Theme.WebViewApp.Blue
Theme.WebViewApp.brown
Theme.WebViewApp.Gray
Theme.WebViewApp.Green
Theme.WebViewApp.Lime
Theme.WebViewApp.Orange
Theme.WebViewApp.Purple
Theme.WebViewApp.Red
Theme.WebViewApp.teal
Theme.WebViewApp.Violet
-->
<application
android:name=".WebViewAppApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.WebViewApp.Blue"
android:hardwareAccelerated="true"
android:allowBackup="true"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
tools:ignore="AllowBackup,UnusedAttribute">
CookieManager.getInstance().setAcceptCookie(true);
<activity
android:name=".activity.SplashActivity"
android:theme="@style/Theme.WebViewApp.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name"
android:launchMode="standard"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.broWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="@string/app_deep_link_host" />
<data android:pathPattern="@string/app_deep_link_path" />
</intent-filter>
</activity>
<service
android:name=".fcm.WebViewAppFirebaseMessagingService"
android:stopWithTask="false"
tools:ignore="ExportedService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<Meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</Meta-data>
</provider>
<Meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_notification" />
<Meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/global_color_accent" />
<Meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/fcm_default_notification_channel_id" />
<Meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/admob_app_id" />
<Meta-data
android:name="onesignal_app_id"
android:value="@string/onesignal_app_id"
tools:replace="android:value" />
<Meta-data
android:name="onesignal_google_project_number"
android:value="str:REMOTE"
tools:replace="android:value" />
<Meta-data
android:name="com.onesignal.Notificationopened.DEFAULT"
android:value="disABLE" />
</application>
</manifest>
activity -> mainactivity.java
package com.robotemplates.webviewapp.activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
import com.robotemplates.kozuza.Kozuza;
import com.robotemplates.webviewapp.R;
import com.robotemplates.webviewapp.WebViewAppConfig;
import com.robotemplates.webviewapp.ads.AdMobGdprHelper;
import com.robotemplates.webviewapp.ads.AdMobInterstitialHelper;
import com.robotemplates.webviewapp.fragment.MainFragment;
import com.robotemplates.webviewapp.listener.DrawerStateListener;
import com.robotemplates.webviewapp.listener.LoadUrlListener;
import com.robotemplates.webviewapp.utility.IntentUtility;
import com.robotemplates.webviewapp.utility.ratingUtility;
public class MainActivity extends AppCompatActivity implements LoadUrlListener,DrawerStateListener {
public static final String EXTRA_URL = "url";
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;
private ActionBarDrawerToggle mDrawerToggle;
private String mUrl;
private AdMobInterstitialHelper mAdMobInterstitialHelper = new AdMobInterstitialHelper();
public static Intent newIntent(Context context) {
Intent intent = new Intent(context,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
public static Intent newIntent(Context context,String url) {
Intent intent = new Intent(context,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(EXTRA_URL,url);
return intent;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// handle intent extras
Bundle extras = getIntent().getExtras();
if (extras != null) {
handleExtras(extras);
}
// handle intent data
String data = getIntent().getDataString();
if (data != null) {
handleData(data);
}
// setup action bar and drawer
setupActionBar();
setupDrawer(savedInstanceState);
// bind data
setupView();
// gdpr
AdMobGdprHelper adMobGdprHelper = new AdMobGdprHelper(this,getString(R.string.admob_publisher_id),WebViewAppConfig.GDPR_PRIVACY_POLICY_URL);
adMobGdprHelper.requestConsent();
// admob
mAdMobInterstitialHelper.setupAd(this);
// check rate app prompt
ratingUtility.checkRateAppPrompt(this);
// kozuza
Kozuza.process(this);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent intent) {
super.onActivityResult(requestCode,resultCode,intent);
// forward activity result to fragment
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container_drawer_content);
if (fragment != null) {
fragment.onActivityResult(requestCode,intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// action bar menu
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onoptionsItemSelected(@NonNull MenuItem item) {
// open or close the drawer if home button is pressed
if (mDrawerToggle.onoptionsItemSelected(item)) {
return true;
}
// action bar menu behavior
if (item.getItemId() == android.R.id.home) {
Intent intent = MainActivity.newIntent(this);
startActivity(intent);
return true;
}
return super.onoptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(@NonNull Configuration newConfiguration) {
super.onConfigurationChanged(newConfiguration);
mDrawerToggle.onConfigurationChanged(newConfiguration);
}
@Override
public void onBackpressed() {
if (isDraweropen()) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
if (WebViewAppConfig.EXIT_CONFIRMATION) {
Snackbar
.make(findViewById(R.id.main_coordinator_layout),R.string.main_exit_snackbar,Snackbar.LENGTH_LONG)
.setAction(R.string.main_exit_confirm,view -> MainActivity.super.onBackpressed())
.show();
} else {
super.onBackpressed();
}
}
}
@Override
public void onLoadUrl(String url) {
boolean showInAppReviewDialog = ratingUtility.checkInAppReviewDialog(this);
if (!showInAppReviewDialog) {
mAdMobInterstitialHelper.checkAd(this);
}
}
@Override
public boolean isDraweropen() {
return mDrawerLayout.isDrawerOpen(GravityCompat.START);
}
@Override
public void onBackButtonpressed() {
onBackpressed();
}
public void showInterstitialAd() {
mAdMobInterstitialHelper.forceAd(this);
}
private void setupActionBar() {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar bar = getSupportActionBar();
bar.setdisplayUselogoEnabled(false);
bar.setdisplayShowTitleEnabled(true);
bar.setdisplayShowHomeEnabled(true);
bar.setdisplayHomeAsUpEnabled(WebViewAppConfig.NAVIGATION_DRAWER);
bar.setHomeButtonEnabled(WebViewAppConfig.NAVIGATION_DRAWER);
if (!WebViewAppConfig.ACTION_BAR) bar.hide();
}
private void setupDrawer(Bundle savedInstanceState) {
// reference
mDrawerLayout = findViewById(R.id.main_drawer_layout);
mNavigationView = findViewById(R.id.main_drawer_navigation);
// add menu items
MenuItem firstItem = setupMenu(mNavigationView.getMenu());
// menu icon tint
if (!WebViewAppConfig.NAVIGATION_DRAWER_ICON_TINT) {
mNavigationView.setItemIconTintList(null);
}
// navigation listener
mNavigationView.setNavigationItemSelectedListener(item -> {
// check in-app review dialog
boolean showInAppReviewDialog = ratingUtility.checkInAppReviewDialog(this);
if (!showInAppReviewDialog) {
// check interstitial ad
mAdMobInterstitialHelper.checkAd(this);
}
// select drawer item
selectDrawerItem(item);
return true;
});
// drawer toggle
mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.drawer_open,R.string.drawer_close) {
@Override
public void onDrawerClosed(View view) {
supportInvalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.addDrawerListener(mDrawerToggle);
// disable navigation drawer
if (!WebViewAppConfig.NAVIGATION_DRAWER) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,GravityCompat.START);
}
// show initial fragment
if (savedInstanceState == null) {
if (mUrl == null) {
selectDrawerItem(firstItem);
} else {
selectDrawerItem(mUrl);
}
}
}
private MenuItem setupMenu(Menu menu) {
// title list
String[] titles = getResources().getStringArray(R.array.navigation_title_list);
// url list
String[] urls = getResources().getStringArray(R.array.navigation_url_list);
// icon list
TypedArray iconTypedArray = getResources().obtainTypedArray(R.array.navigation_icon_list);
Integer[] icons = new Integer[iconTypedArray.length()];
for (int i = 0; i < iconTypedArray.length(); i++) {
icons[i] = iconTypedArray.getResourceId(i,-1);
}
iconTypedArray.recycle();
// clear menu
menu.clear();
// add menu items
Menu parent = menu;
MenuItem firstItem = null;
for (int i = 0; i < titles.length; i++) {
if (urls[i].equals("")) {
// category
parent = menu.addSubMenu(Menu.NONE,i,titles[i]);
} else {
// item
MenuItem item = parent.add(Menu.NONE,titles[i]);
if (icons[i] != -1) item.setIcon(icons[i]);
if (firstItem == null) firstItem = item;
}
}
return firstItem;
}
private void selectDrawerItem(MenuItem item) {
int position = item.getItemId();
String[] urlList = getResources().getStringArray(R.array.navigation_url_list);
String[] shareList = getResources().getStringArray(R.array.navigation_share_list);
String url = urlList[position];
if (IntentUtility.startIntentActivity(this,url)) return; // check for intent url
Fragment fragment = MainFragment.newInstance(url,shareList[position]);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container_drawer_content,fragment).commitAllowingStateLoss();
item.setCheckable(true);
mNavigationView.setCheckedItem(position);
getSupportActionBar().setTitle(item.getTitle());
mDrawerLayout.closeDrawers();
}
private void selectDrawerItem(String url) {
if (IntentUtility.startIntentActivity(this,"");
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container_drawer_content,fragment).commitAllowingStateLoss();
mNavigationView.setCheckedItem(-1);
getSupportActionBar().setTitle(getString(R.string.app_name));
mDrawerLayout.closeDrawers();
}
private void handleExtras(Bundle extras) {
if (extras.containsKey(EXTRA_URL)) {
mUrl = extras.getString(EXTRA_URL);
}
}
private void handleData(String data) {
mUrl = data;
}
private void setupView() {
// reference
NavigationView navigationView = findViewById(R.id.main_drawer_navigation);
// inflate navigation header
if (navigationView.getHeaderView(0) == null) {
View headerView = getLayoutInflater().inflate(R.layout.navigation_header,navigationView,false);
navigationView.addHeaderView(headerView);
}
// navigation header content
if (navigationView.getHeaderView(0) != null) {
// reference
View headerView = navigationView.getHeaderView(0);
// header background
if (WebViewAppConfig.NAVIGATION_DRAWER_HEADER_IMAGE) {
headerView.setBackgroundResource(R.drawable.navigation_header_bg);
}
}
}
}
WebViewAppApplication.java
package com.robotemplates.webviewapp;
import android.content.Context;
import androidx.multidex.MultiDex;
import com.google.android.gms.ads.MobileAds;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.onesignal.Onesignal;
import com.robotemplates.kozuza.BaseApplication;
import com.robotemplates.kozuza.Kozuza;
import com.robotemplates.webviewapp.ads.AdMobUtility;
import com.robotemplates.webviewapp.fcm.OnesignalNotificationopenedHandler;
import com.robotemplates.webviewapp.utility.Preferences;
import org.alfonz.utility.Logcat;
public class WebViewAppApplication extends BaseApplication {
@Override
public void onCreate() {
super.onCreate();
// init logcat
Logcat.init(WebViewAppConfig.LOGS,"STUDENTPORTAL");
// init analytics
FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(!WebViewAppConfig.DEV_ENVIRONMENT);
// init AdMob
MobileAds.initialize(this);
MobileAds.setRequestConfiguration(AdMobUtility.createRequestConfiguration());
// init Onesignal
initOnesignal(getString(R.string.onesignal_app_id));
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
@Override
public String getPurchaseCode() {
return WebViewAppConfig.PURCHASE_CODE;
}
@Override
public String getProduct() {
return Kozuza.PRODUCT_WEBVIEWAPP;
}
private void initOnesignal(String onesignalAppId) {
if (!onesignalAppId.equals("")) {
Onesignal.initWithContext(this);
Onesignal.setAppId(onesignalAppId);
Onesignal.setNotificationopenedHandler(new OnesignalNotificationopenedHandler());
Onesignal.addSubscriptionObserver(stateChanges -> {
if (stateChanges.getTo().isSubscribed()) {
String userId = stateChanges.getTo().getUserId();
saveOnesignalUserId(userId);
}
});
saveOnesignalUserId(Onesignal.getDeviceState().getUserId());
}
}
private void saveOnesignalUserId(String userId) {
if (userId != null) {
Logcat.d("userId = " + userId);
Preferences preferences = new Preferences();
preferences.setonesignalUserId(userId);
}
}
}
WebViewAppConfig.java
package com.robotemplates.webviewapp;
import com.robotemplates.webviewapp.view.PullToRefreshMode;
import static com.robotemplates.webviewapp.view.PullToRefreshMode.*;
public class WebViewAppConfig {
// Envato purchase code
public static final String PURCHASE_CODE = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
// true for showing action bar
public static final boolean ACTION_BAR = false;
// true for showing html title rather than navigation title in the action bar
public static final boolean ACTION_BAR_HTML_TITLE = false;
// true for enabling navigation drawer menu
public static final boolean NAVIGATION_DRAWER = false;
// true for enabling background image in the header of the navigation drawer menu,// otherwise accent color will be used,// background image is stored in navigation_header_bg.png
public static final boolean NAVIGATION_DRAWER_HEADER_IMAGE = false;
// true for enabling icon tint in the navigation drawer menu,// note that only transparent PNG icons can be tinted,// tint color is defined in @color/navigation_icon_tint
public static final boolean NAVIGATION_DRAWER_ICON_TINT = false;
// true for enabling exit confirmation when back button is pressed
public static final boolean EXIT_CONFIRMATION = true;
// true for enabling geolocation
public static final boolean GEOLOCATION = true;
// true for enabling progress placeholder when loading a first page
public static final boolean PROGRESS_PLACEHOLDER = true;
// true for enabling javascript api
public static final boolean JAVA_SCRIPT_API = true;
// pull-to-refresh gesture for refreshing the content,// set ENABLED to enable the gesture,set disABLED to disable the gesture,// set PROGRESS to disable the gesture and show only progress indicator
public static final PullToRefreshMode PULL_TO_REFRESH = ENABLED;
// frequency of showing in-app review dialog,// review dialog will be shown after each x url loadings or clicks on navigation drawer menu,// set 0 if you do not want to show in-app review dialog
public static final int INAPP_REVIEW_DIALOG_FREQUENCY = 30;
// frequency of showing rate my app prompt,// prompt will be shown after each x launches of the app,// set 0 if you do not want to show rate my app prompt
public static final int RATE_APP_PROMPT_FREQUENCY = 0;
// duration of showing rate my app prompt in milliseconds
public static final int RATE_APP_PROMPT_DURATION = 10000;
// custom user agent string for the webview,// leave this constant empty if you do not want to set custom user agent string
public static final String WEBVIEW_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/41.0.2228.0 Safari/537.36";
// frequency of showing AdMob interstitial ad,// ad will be shown after each x url loadings or clicks on navigation drawer menu
public static final int ADMOB_INTERSTITIAL_FREQUENCY = 50000000;
// URL link to your privacy policy for GDPR consent form,// leave this constant empty if you do not want to show GDPR consent form
public static final String GDPR_PRIVACY_POLICY_URL = "https://link.to/privacy-policy.html";
// true for opening webview links in external web browser rather than directly in the webview
public static final boolean OPEN_LINKS_IN_EXTERNAL_broWSER = false;
// rules for opening links in external browser,// if URL link contains the string,it will be opened in external browser,// these rules have higher priority than OPEN_LINKS_IN_EXTERNAL_broWSER option
public static final String[] LINKS_OPENED_IN_EXTERNAL_broWSER = {
"target=blank","target=external","play.google.com/store","youtube.com/watch","facebook.com/sharer","twitter.com/share","t.me"
};
// rules for opening links in internal webview,it will be loaded in internal webview,// these rules have higher priority than OPEN_LINKS_IN_EXTERNAL_broWSER option
public static final String[] LINKS_OPENED_IN_INTERNAL_WEBVIEW = {
"target=webview","target=internal"
};
// list of file extensions or expressions for download,// if webview URL matches with this regular expression,the file will be downloaded via download manager,// leave this array empty if you do not want to use download manager
public static final String[] DOWNLOAD_FILE_TYPES = {
".*zip$",".*rar$",".*pdf$",".*doc$",".*xls$",".*mp3$",".*wma$",".*ogg$",".*m4a$",".*wav$",".*avi$",".*mov$",".*mp4$",".*mpg$",".*3gp$",".*drive.google.com.*file.*",".*dropBox.com/s/.*"
};
// debug logs,value is set via build config in build.gradle
public static final boolean LOGS = BuildConfig.LOGS;
// development environment,value is set via build config in build.gradle
public static final boolean DEV_ENVIRONMENT = BuildConfig.DEV_ENVIRONMENT;
// AdMob test ads,value is set via build config in build.gradle
public static final boolean TEST_ADS = BuildConfig.TEST_ADS;
}
当我们打开应用程序时,它会显示用谷歌登录,当你点击它时,它会带你到谷歌登录用户名和密码页面..一旦你输入正确的详细信息并按登录,它就会显示空白的白屏...... 请帮忙..
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)