gpt4 book ai didi

javascript - 安卓 WebView : Can't go back to previous page

转载 作者:搜寻专家 更新时间:2023-11-01 09:29:31 25 4
gpt4 key购买 nike

(我是新手)我从一个人那里买了一个 android 应用程序源,我用我的链接替换了他的链接,所以现在 webview 指向我的网站。一切正常,但后退按钮有问题。如果我按下它,应用程序将关闭(更像是最小化,应用程序会继续在后台运行)而不是返回到上一页。我已经尝试了在谷歌上找到的多种解决方案,但它们都在做同样的事情。我买的那个人没有回复我的消息,所以我在这里问,也许你们会帮助我。

我会提供任何其他需要的信息。我不想成为应用程序开发人员,所以如果你们能用更简单的术语解释解决方案,我将不胜感激。

主要 Activity .java(我希望这是需要修改的代码)

/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package com.myapp.music;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.onesignal.OneSignal;

import org.apache.cordova.*;

public class MainActivity extends CordovaActivity
{
boolean doubleBackToExitPressedOnce = false;
private InterstitialAd mInterstitialAd;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}


ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
loadUrl(launchUrl);
}

else {

Toast.makeText(this,"Device is not connected to the internet",Toast.LENGTH_LONG).show();

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Error :(");
builder.setMessage("No internet connection").setCancelable(false)

.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alert = builder.create();
alert.show();


}


//OneSignal Push Notification
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();

//Interstitial - To enable the Admob Ads, Uncomment the below code

/* prepareAd();

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", " Interstitial AD not loaded");
}

}
}, 30000); //Ads will be shown after 30 seconds */

UiChangeListener();

}

public void UiChangeListener()
{
final View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
}
});

}

public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}

this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();

new Handler().postDelayed(new Runnable() {

@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}

public void prepareAd(){

mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(""); //replace with your Admob ID
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}

最佳答案

在 onBackButtonPressed 中使用这个

public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}

if(webView.canGoBack()){
webView.goBack();
}

this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();

new Handler().postDelayed(new Runnable() {

@Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}

关于javascript - 安卓 WebView : Can't go back to previous page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48240253/

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