WebView is a class which is an extention of View class. Using Webview, we can display webpages in the mobile browser.
To add webview to your application, declare internet permission in manifest file and add WebView to xml.
To load webpage in WebView, use loadUrl
WebView wb=(WebView)findViewById(R.id.webView1);
wb.loadUrl("http://www.mobengineers.com");
You must enable javascript in your webview to load javascript in the page.
To bind interface between Javascript code and android code, then use
webview.addJavascriptInterface(new WebAppInterface(this), "Android");
sample webview example here
package com.example.demo1;
public class MainActivity extends Activity {
public class WebAppInterface { Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wb=(WebView)findViewById(R.id.webView1);
wb.loadUrl("http://www.mobengineers.com");
WebSettings ws=wb.getSettings();
ws.setJavaScriptEnabled(true);
// wb.addJavascriptInterface(new WebAppInterface(this), "Android");
wb.setWebViewClient(new WebViewClient());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.getnicejobs.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
To add webview to your application, declare internet permission in manifest file and add WebView to xml.
To load webpage in WebView, use loadUrl
WebView wb=(WebView)findViewById(R.id.webView1);
wb.loadUrl("http://www.mobengineers.com");
You must enable javascript in your webview to load javascript in the page.
To bind interface between Javascript code and android code, then use
webview.addJavascriptInterface(new WebAppInterface(this), "Android");
To store user data in the browser, we have to use cookies, so that the browser data it does not share.
android.webkit.CookieSyncManager class to manage all cookies.
To use this, 1st create one instance,
cookieSyncManager.createInstance(context);
setup sync call
to start
cookieSyncManager.getInstance().startSync();
to stop
cookieSyncManager.getInstance().stopSync();
to get sync instantly,
cookieSyncManager.getInstance().Sync();
package com.example.demo1;
public class MainActivity extends Activity {
public class WebAppInterface { Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wb=(WebView)findViewById(R.id.webView1);
wb.loadUrl("http://www.mobengineers.com");
WebSettings ws=wb.getSettings();
ws.setJavaScriptEnabled(true);
// wb.addJavascriptInterface(new WebAppInterface(this), "Android");
wb.setWebViewClient(new WebViewClient());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.getnicejobs.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
0 coment�rios:
Post a Comment