Is there a permission or config that needs to be used if a site uses SAML in an android webview?
I'm getting the error:
opensaml::FatalProfileException at (http://example.com/SAML2/POST)
Error from identity provider: Status:
urn:oasis:names:tc:SAML:2.0:status:Responder Sub-Status
urn:oasis:names:tc:SAML:2.0:status:AuthnFailed
My permissions look like:
My activity code looks like:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context c = this.getApplicationContext();
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.setFocusable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
//webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
webView.loadUrl("http://example.com");
}
If http://example.com is the IDP and your mobile application is starting the SAML authentication. Your mobile phone must provide a SAML AuthenRequest. I cant se that you do this in your code.
It is quite uncommon to do SAML authentication from a mobile phone. Generally Oauth is the better choice here.
I did this based on https://github.com/maybeaustin/WebView-Developer-Browser :
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import com.webviewbrowser.R;
public class MainActivity extends Activity {
private long pageStartTime = 0;
private WebView wv;
private ImageButton newActivityBtn;
private EditText et;
private TextView pageLoadTime;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wv = (WebView) findViewById(R.id.wv);
newActivityBtn = (ImageButton) findViewById(R.id.new_activity);
pageLoadTime = (TextView) findViewById(R.id.page_load_time);
et = (EditText) findViewById(R.id.et);
// setup edit text
et.setSelected(false);
if (getIntent().getStringExtra("url") != null) {
et.setText(getIntent().getStringExtra("url"));
} else {
et.setText("");
}
// setup wv
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
}
wv.setWebChromeClient(new WebChromeClient());
wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
WebSettings settings = wv.getSettings();
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setJavaScriptEnabled(true);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setAppCacheEnabled(false);
settings.setDomStorageEnabled(true);
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
et.setText(url);
pageStartTime = System.currentTimeMillis();
pageLoadTime.setText("0ms");
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (pageStartTime == 0) {
} else {
long loadTime = (System.currentTimeMillis() - pageStartTime);
pageLoadTime.setText(String.format("%sms", loadTime));
System.out.println(String.format("page load time: %sms", loadTime));
}
}
});
handleLoadUrl();
// setup events
newActivityBtn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
newActivityBtn.setColorFilter(getResources().getColor(android.R.color.holo_blue_dark));
return false;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
newActivityBtn.setColorFilter(null);
return false;
}
return false;
}
});
newActivityBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.putExtra("url", et.getText().toString());
startActivity(intent);
}
});
et.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
handleLoadUrl();
return true;
} else {
return false;
}
}
});
}
private void handleLoadUrl() {
String url = et.getText().toString();
if (url.startsWith("http://")) {
} else if (url.startsWith("https://")) {
} else {
url = String.format("http://%s", url);
}
wv.loadUrl(url);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
}
Related
I want to save instant of my fragment and then restore back on reopening the tab. when switching from one tab to other.
here is my fragment code:
package com.mhm.universityofwah.fragments;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.mhm.universityofwah.R;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import jp.co.recruit_lifestyle.android.widget.WaveSwipeRefreshLayout;
public class WebFragment extends Fragment implements WaveSwipeRefreshLayout.OnRefreshListener {
private WaveSwipeRefreshLayout swipeRefreshLayout;
private WebView mWebView;
private Activity context;
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setRetainInstance(true);
View view = inflater.inflate(R.layout.fragment_avicenna, container, false);
mWebView = (WebView) view.findViewById(R.id.webview);
swipeRefreshLayout = (WaveSwipeRefreshLayout) view.findViewById(R.id.swipeContainer);
swipeRefreshLayout.setOnRefreshListener((WaveSwipeRefreshLayout.OnRefreshListener) WebFragment.this);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
swipeRefreshLayout.setRefreshing(true);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
});
BottomNavigationView bottomNavigationView=view.findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(navListner);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.setWebViewClient(new Callback());
if(savedInstanceState==null){
loadWebsite();
}
return view;
}
private BottomNavigationView.OnNavigationItemSelectedListener navListner=
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_refresh:
mWebView.reload();
break;
case R.id.nav_back:
if(mWebView.canGoBack()){
mWebView.goBack();
}
break;
case R.id.nav_forward:
if(mWebView.canGoForward()){
mWebView.goForward();
}
break;
}
return true;
}
};
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void loadWebsite() {
ConnectivityManager cm = (ConnectivityManager) Objects.requireNonNull(getActivity()).getSystemService(Context.CONNECTIVITY_SERVICE);
assert cm != null;
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
mWebView.loadUrl("http://uow.edu.pk");
} else {
Toast.makeText(Objects.requireNonNull(getActivity()).getApplicationContext(), "Please Check Network Connection!", Toast.LENGTH_SHORT).show();
swipeRefreshLayout.setRefreshing(false);
}
}
public class Callback extends WebViewClient {
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(Objects.requireNonNull(getActivity()).getApplicationContext(), "Please Check Network Connection!", Toast.LENGTH_SHORT).show();
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".pdf")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}else if (url.contains("mailto:")) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}else if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}else {
view.loadUrl(url);
return true;
}
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
swipeRefreshLayout.setRefreshing(true);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
}
#Override
public void onRefresh() {
mWebView.reload();
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
}
}
#Override
public void onSaveInstanceState(#NotNull Bundle outState )
{
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
public boolean onKey(View v, int keyCode, KeyEvent event ) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
Objects.requireNonNull(getActivity()).finish();
}
return true;
}
}
// here your code
return false;
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
this.context = (Activity) context;
}
}
I have 4 tabs but when i switch from one tab to other and come back to first one, it always reload. i tried every thing but my problem is not solved. Please help me to get this thing done.
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setRetainInstance(true);
if(view == null){
view = inflater.inflate(R.layout.fragment_avicenna, container, false);
mWebView = (WebView) view.findViewById(R.id.webview);
swipeRefreshLayout = (WaveSwipeRefreshLayout) view.findViewById(R.id.swipeContainer);
swipeRefreshLayout.setOnRefreshListener((WaveSwipeRefreshLayout.OnRefreshListener) WebFragment.this);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
swipeRefreshLayout.setRefreshing(true);
}
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
}
});
BottomNavigationView bottomNavigationView=view.findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(navListner);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccess(true);
webSettings.setAppCacheEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.setWebViewClient(new Callback());
if(savedInstanceState==null){
loadWebsite();
}
}
return view;
}
please make view a global variable and add null check when your onCreate view recalls it recreates the view so add check that if view != null then only return already initialized view instead of creating again
I have created a webview app. On pressing volume keys, my app crashes. This is the error in logcat
W/ViewRootImpl[MainActivity]: Cancelling event due to no window focus:
KeyEvent { action=ACTION_UP, keyCode=KEYCODE_VOLUME_DOWN,
scanCode=114, metaState=0, flags=0x28, repeatCount=0,
eventTime=196583031, downTime=196582864, deviceId=9, source=0x101 }
Below is my MainActivity.java file:
package app.freeairdrop.io;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class MainActivity extends Activity{
private ProgressBar progressBar;
private WebView webView;
private SwipeRefreshLayout mySwipeRefreshLayout;
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClientDemo());
webView.setWebChromeClient(new WebChromeClientDemo());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
webView.loadUrl("http://freeairdrop.io/");
mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
mySwipeRefreshLayout.setRefreshing(false);
}
}
);
}
private class WebViewClientDemo extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
if (uri.getHost() != null && (url.startsWith("https://freeairdrop.io/") || url.startsWith("https://www.freeairdrop.io/"))) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else {
finish();
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
// This method is used to detect back button
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
Remove
finish();
from here :
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else {
finish();
}
add focus to webview using:
<WebView android:id="#+id/webview"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"/>
Or
webview.requestFocus(View.FOCUS_DOWN|View.FOCUS_UP);
Remove call to finish() from here,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else {
finish(); // Remove this, onBackPressed() should handle it.
}
return super.onKeyDown(keyCode, event);
}
This is my second activity
can anyone help me to add a progress text while loading the website, after the loading it should stop, help me to place the code in right place ,because i don't know much about java, i am just studying
package com.example.jaisonjoseph.newsclient;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.support.design.widget.NavigationView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
public class Manorama extends Activity {
Toolbar toolbar=null;
public WebView mwebView;
public static boolean isNetworkStatusAvialable (Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null)
{
NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
if(netInfos != null)
if(netInfos.isConnected())
return true;
}
return false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manorama);
if(isNetworkStatusAvialable (getApplicationContext())) {
WebView view=(WebView) findViewById(R.id.webView2);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new WebViewClient());
view.loadUrl("http://manoramaonline.com");
} else {
WebView view1=(WebView) findViewById(R.id.webView2);
view1.getSettings().setJavaScriptEnabled(true);
view1.setWebViewClient(new WebViewClient());
view1.loadUrl("file:///android_asset/index.html");
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction()==KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
WebView webView = (WebView) findViewById(R.id.webView2);
if (webView != null) {
if (webView.canGoBack()) {
webView.goBack();
} else {
finish();
}
return true;
}
}
}
return super.onKeyDown(keyCode, event);
}
private class MyWebViewClient extends WebViewClient {
ProgressBar bar=(ProgressBar)findViewById(R.id.progressBar);
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
bar.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
bar.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
super.onPageFinished(view, url);
}
}
}
You should set WebViewClient for listening page loading started and finished. You can make that like:
Custom WebViewClient:
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#Override
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) {
progress.setVisibility(View.VISIBLE);
webView.setVisibility(View.GONE);
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
super.onPageFinished(view, url);
}
}
Set above client to your webView:
webView.setWebViewClient(new MyWebViewClient());
You can also add an TextView below your ProgressBar and set visibility of that. Good luck.
I have a WebView based app that shows a WordPress website. I have in my website a few YouTube videos, and the problem is YouTube videos does not have full screen button. I looked for an answer to this and didn't find anything on the web. Can someone help me? how can I make the YouTube videos have the option to play in full screen from my WebView?
Here is my MainActivity code:
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.delitestudio.pushnotifications.PushNotifications;
import com.loopj.android.http.*;
import cz.msebera.android.httpclient.*;
import cz.msebera.android.httpclient.impl.entity.StrictContentLengthStrategy;
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final WebChromeClient webChromeClient = new WebChromeClient();
private static final WebViewClient webViewClient = new WebViewClient();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url;
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey("fromPush")) {
url = "http://www.paotiptipon.co.il/%d7%9b%d7%a0%d7%99%d7%a1%d7%aa-%d7%94%d7%95%d7%a8%d7%99%d7%9d-2/%d7%a4%d7%95%d7%a1%d7%98%d7%99%d7%9d/";
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl(url);
} else {
url = "http://paotiptipon.co.il/";
}
WebView myWebView = (WebView) this.findViewById(R.id.webView);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebChromeClient(webChromeClient);
myWebView.setWebViewClient(webViewClient);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setAllowContentAccess(true);
myWebView.getSettings().setDatabaseEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.loadUrl(url);
myWebView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
WebView webView = (WebView) v;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack())
webView.goBack();
break;
}
}
return false;
}
});
notificationsButtonClicked();
newNotificationButtonClicked();
calendarButtonClicked();
final PushNotifications pn = new PushNotifications(this);
if (pn.getToken() == null || pn.isExpired()) {
pn.refreshToken("http://www.paotiptipon.co.il/pnfw/register/",
"35299935473");
}
// Bundle extras = getIntent().getExtras();
// if (extras != null) {
// int id = extras.getInt(PushNotifications.ID);
// String title = extras.getString(PushNotifications.TITLE);
// System.out.println(id + title);
// }
}
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
mIntentReceiver.abortBroadcast();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("התראה חדשה!");
alertDialog.setMessage("יש התראה חדשה במרכז ההתראות! לחצו על אישור כדי לראות אותה!");
// Setting OK Button
alertDialog.setPositiveButton("אישור",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String url = "http://www.paotiptipon.co.il/%d7%9b%d7%a0%d7%99%d7%a1%d7%aa-%d7%94%d7%95%d7%a8%d7%99%d7%9d-2/%d7%a4%d7%95%d7%a1%d7%98%d7%99%d7%9d/";
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl(url);
}
});
// Setting CANCEL Button
alertDialog.setNegativeButton("ביטול",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialog.show();
}
};
private void notificationsButtonClicked() {
Button notifications = (Button) findViewById(R.id.notifications);
notifications.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.paotiptipon.co.il/%d7%9b%d7%a0%d7%99%d7%a1%d7%aa-%d7%94%d7%95%d7%a8%d7%99%d7%9d-2/%d7%a4%d7%95%d7%a1%d7%98%d7%99%d7%9d/";
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl(url);
}
});
}
private void newNotificationButtonClicked() {
Button newNotification = (Button) findViewById(R.id.newNotification);
newNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.paotiptipon.co.il/wp-admin/post-new.php";
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl(url);
}
});
}
private void calendarButtonClicked() {
Button calendar = (Button) findViewById(R.id.calendar);
calendar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.paotiptipon.co.il/events/";
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl(url);
}
});
}
#Override
protected void onResume() {
super.onResume();
registerReceiver(mIntentReceiver, PushNotifications.getIntentFilter(this));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mIntentReceiver);
}
}
first of all thanks for those of you who tried to help.
I found an answer to my question, and I would like to share it here so if anyone needs this it will be here.
I used this link:
Playing HTML5 video on fullscreen in android webview
I just downloaded the example project from there and implemented the code there in my project. The man who wrote those classes is awesome and wrote a really good tutorial on this and it fixed my problem. :)
I tried to add a back button to my android webview but i've got this error:
The method onBackPressed() of type new WebViewClient(){} must override or implement a supertype method
This is my Java file:
package sherdle.donald.duck.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPageLoader extends Activity
{
WebView webview;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public void onBackPressed() {
// do something on back.
return;
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://example.com");
}
}
Thanks for your help.
According to the doc, there is no onBackPressed method in the WebViewClient class. Did you want to Override it on your activity ?
try smthing like this:
package sherdle.donald.duck.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPageLoader extends Activity
{
WebView webView;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://example.com");
}
#Override
public void onBackPressed (){
if(webView.canGoBack()) webView.goBack();
else super.onBackPressed();
}
}
You have to override the onkeyDown() method in your Activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Try this
package sherdle.donald.duck.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPageLoader extends Activity
{
WebView webview;
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://example.com");
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true)
webview.goBack();
else finish();
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
}
And this method should be override in Activty.
WebView webView = (WebView) findViewById(R.id.webview);
Change it to:
webView = (WebView) findViewById(R.id.webview);
and then use the second answer - works like a charm!