I would like to load an website via WebView. When it loads via web browser,there is no problem. But when I try to load via WebView, Nothing loads. A white blank background shows instead.
Here is MainActivity class(Here, it is Main class):
package com.xamarin.gcmexample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import android.telephony.TelephonyManager;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;
import java.util.logging.Logger;
public class Main extends AppCompatActivity {
WebView mWebView;
Logger LOGGER;
private static Main mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
LOGGER = Logger.getLogger(RegistrationIntentService.class.getName());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "Android");
mWebView.setWebViewClient(new MyWebViewClient());
mContext =Main.this;
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String id=telephonyManager.getDeviceId();
int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mContext);
if (resultCode == ConnectionResult.SUCCESS){
mWebView.loadUrl("https://pavementworks.corelynx.com?imei="+id);
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}catch(Exception ex)
{ LOGGER.info(" mWebView:"+ex.getMessage());
return;
}
}
@Override
protected void onResume() {
super.onResume();
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mContext =Main.this;
LOGGER = Logger.getLogger(RegistrationIntentService.class.getName());
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "Android");
mWebView.setWebViewClient(new MyWebViewClient());
int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mContext);
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String id=telephonyManager.getDeviceId();
if (resultCode == ConnectionResult.SUCCESS){
mWebView.loadUrl("https://pavementworks.corelynx.com?imei="+id);
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
}
class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//You can add some custom functionality here
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//You can add some custom functionality here
}
@Override
public void onReceivedError (WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
//You can add some custom functionality here
}
}
class MyJavaScriptInterface {
Activity activity;
MyJavaScriptInterface(Activity activity) {
this.activity = activity;
}
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(activity, toast, Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public void closeActivity() {
activity.finish();
}
}
Here is my Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:fitsSystemWindows="true"
tools:context="com.xamarin.gcmexample.Main">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
Am I doing any wrong? Please give suggestion.