我有一个使用WebView
的android应用。按下后退按钮时出现错误,称为错误缓存未命中。我具有互联网许可,并且还禁用了缓存,但是仍然遇到相同的错误。
public class MyChooserHtmlCheck extends Activity {
/**
* Called when the activity is first created.
*/
private String TAG;
WebView web;
private ProgressDialog progressDialog;
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;
String post_detail, post_url;
public ValueCallback<Uri[]> uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;
private boolean isChatLoad = false;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (requestCode == REQUEST_SELECT_FILE) {
if (uploadMessage == null)
return;
uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
uploadMessage = null;
}
} else if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage)
return;
// Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment
// Use RESULT_OK only if you're implementing WebView inside an Activity
Uri result = intent == null || resultCode != MyChooserHtmlCheck.RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
} else
Toast.makeText(getApplicationContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TAG = MyChooserHtmlCheck.this.getClass().getSimpleName();
createIds();
Toast.makeText(getApplication(), "Please wait Loading...", Toast.LENGTH_LONG).show();
clearCookies(MyChooserHtmlCheck.this);
getDetail();
}
private void createIds() {
web = (WebView) findViewById(R.id.webview);
progressDialog = new ProgressDialog(this, R.style.MyTheme);
progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
}
private void getDetail() {
Intent intent = getIntent();
String ChatCall = "";
if (intent.hasExtra("NotificationMessage")) {
post_detail = "";
ChatCall = intent.getStringExtra("chatMessage");
isChatLoad = true;
post_detail = intent.getStringExtra(Itag.post_login);
post_url = intent.getStringExtra(Itag.post_login_url);
} else {
post_detail = intent.getStringExtra(Itag.post_login);
post_url = intent.getStringExtra(Itag.post_login_url);
}
LoadwebView(post_detail, post_url);
}
// Load View
private void LoadwebView(String post_detail, String post_Url) {
web = new WebView(this);
web.getSettings().setJavaScriptEnabled(true);
// web.loadUrl("http://www.script-tutorials.com/demos/199/index.html");
WebViewClient wb = new WebViewClient();
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
web.getSettings().setAllowFileAccess(true);
web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setSupportZoom(false);
web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
progressDialog.setTitle("Silver LifeStyle");
progressDialog.setMessage("Please wait...");
progressDialog.show();
web.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();
Log.e(TAG, hr.getExtra() + "Type == " + hr.getType());
return false;
}
});
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("url", "url : " + url);
if (!(MyChooserHtmlCheck.this).isFinishing()) {
if (!progressDialog.isShowing()) {
progressDialog.setTitle("Silver LifeStyle");
progressDialog.setMessage("Please Wait...");
progressDialog.show();
}
}
// if (url.equalsIgnoreCase("https://consignchain.net/optisoft/logout.php")) {
if (url.equalsIgnoreCase("https://titankart.com/optisoft/logout.php")) {
SavedData.saveLogin(false);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
finish();
return true;
} else if (url != null && url.startsWith("https://") && !url.equalsIgnoreCase("https://titankart.com/optisoft/logout.php")) {
Log.e("Prince New", "" + url);
SavedData.saveLogin(true);
web.loadUrl(url);
return true;
} else {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
SavedData.saveLogin(false);
//1
Intent i = new Intent(MyChooserHtmlCheck.this, LoginActivity.class);
startActivity(i);
finish();
return false;
}
}
@Override
public void onPageFinished(WebView view, String url) {
Log.e("on Finish", "Url : " + url);
if (isChatLoad) {
web.loadUrl("https://titankart.com/optisoft/inbox.php?");
isChatLoad = false;
}
if (url.equalsIgnoreCase("https://titankart.com/api.php")) {
Toast.makeText(MyChooserHtmlCheck.this, "Invalid Credential ....", Toast.LENGTH_LONG).show();
SavedData.saveLogin(false);
//2
Intent i = new Intent(MyChooserHtmlCheck.this, LoginActivity.class);
startActivity(i);
finish();
}
if (url.equalsIgnoreCase("https://titankart.com/api.php")) {
Toast.makeText(MyChooserHtmlCheck.this, "Invalid Credential ....", Toast.LENGTH_LONG).show();
SavedData.saveLogin(false);
//3
Intent i = new Intent(MyChooserHtmlCheck.this, LoginActivity.class);
startActivity(i);
finish();
}
if (url.equalsIgnoreCase("https://titankart.com/index8.php")) {
Toast.makeText(MyChooserHtmlCheck.this, "Invalid Credential ....", Toast.LENGTH_LONG).show();
SavedData.saveLogin(false);
//4
Intent i = new Intent(MyChooserHtmlCheck.this, LoginActivity.class);
startActivity(i);
finish();
}
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
});
web.postUrl(post_Url, EncodingUtils.getBytes(this.post_detail, "base64"));
setChrome();
}
// Manage Dialog and Attachment
private void setChrome() {
web.setWebChromeClient(new WebChromeClient() {
//The undocumented magic method override
//Eclipse will swear at you if you try to put @Override here
// For Android 3.0+
protected void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
}
// For Lollipop 5.0+ Devices
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
uploadMessage = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try {
startActivityForResult(intent, REQUEST_SELECT_FILE);
} catch (ActivityNotFoundException e) {
uploadMessage = null;
Toast.makeText(getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
//For Android 4.1 only
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
}
protected void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
});
setContentView(web);
}
public static void clearCookies(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
Log.e("clear cache", "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
} else {
Log.e("clear cache", "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
cookieSyncMngr.startSync();
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncMngr.stopSync();
cookieSyncMngr.sync();
}
}
//flipscreen not loading again
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
下面是激活互联网许可的清单文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="business.supply.yourguide.consignchain.comstock.Titankart">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:name="business.supply.yourguide.consignchain.comstock.Titankart.app.AppController"
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:roundIcon="@drawable/app_icon"
android:supportsRtl="true"
android:theme="@style/AppThemeFullscreen">
<activity android:name="business.supply.yourguide.consignchain.comstock.Titankart.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="business.supply.yourguide.consignchain.comstock.Titankart.LoginActivity"
android:screenOrientation="portrait"></activity>
<!-- <activity
android:name="business.supply.yourguide.consignchain.comstock.consignchain.MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"></activity>
-->
<activity
android:name="business.supply.yourguide.consignchain.comstock.Titankart.RegisterActvity"
android:screenOrientation="portrait"></activity>
<activity
android:name="business.supply.yourguide.consignchain.comstock.Titankart.VerificationDetail"
android:screenOrientation="portrait"></activity>
<activity
android:name="business.supply.yourguide.consignchain.comstock.Titankart.MyChooserWeb"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@style/Theme.AppCompat.NoActionBar"></activity>
<!--Notification Services-->
<service android:name="business.supply.yourguide.consignchain.comstock.Titankart.Firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name="business.supply.yourguide.consignchain.comstock.Titankart.Firebase.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<receiver
android:name="business.supply.yourguide.consignchain.comstock.Titankart.Firebase.FirebaseDataReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/icon" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!--End zNotification Services-->
</application>
</manifest>