Method Not Working Inside Javascript Interface Android - android

I am loading an Webview url inside my Javascript interface In Android , but they are not working;
How to To handle IT
#SuppressLint({ "SetJavaScriptEnabled", "NewApi" }) public class MainActivity extends ActionBarActivity {
WebView Browser;
String website ="http://192.168.1.4/Soft/Bigcats/Ba/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Browser=(WebView) findViewById(R.id.webView1) ;
class MyJavaScriptInterface
{
#JavascriptInterface
#SuppressWarnings("unused")
public void Danyial(String Data)
{
Browser.loadUrl("http://enjoybaba.com");
}
}
Browser.addJavascriptInterface(new MyJavaScriptInterface(), "api");
Browser.loadUrl(website);
Browser.setWebViewClient(new WebViewClient());
}
}

JavaScript runs in another thread, not UI so you may need to run your code on the UI thread for it to work.
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
Browser.loadUrl("http://enjoybaba.com");
}
});

You have Browser.setWebViewClient(new WebViewClient()); after Browser.loadUrl(website);. Try to setWebViewClient before loadUrl.
I would do this a little bit differently. Take a look.
Declare JsHelper.
public interface JsHelper {
public void loadNewUrl();
}
Javascript Interface calls JsHelper
public class JsApi {
JsHelper mJsHelper;
public JsApi(JsHelper jsHelper) {
mJsHelper= mJsHelper;
}
#JavascriptInterface
public void Danyial() {
mJsHelper.loadNewUrl();
}
}
MainActivity implements JsInterface
public class MainActivity extends ActionBarActivity implements JsHelper {
WebView Browser;
String website ="http://192.168.1.4/Soft/Bigcats/Ba/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Browser=(WebView) findViewById(R.id.webView1) ;
Browser.addJavascriptInterface(new JsApi (this), "api");
Browser.setWebViewClient(new WebViewClient());
Browser.loadUrl(website);
}
#Override
public void loadNewUrl(){
Browser.loadUrl("http://enjoybaba.com");
}
}

Related

I'm trying to open firebase website in my app but it requests for external browser

I am trying to open the firebase website inside my app using webview. But the app requests for external browser while opening that activity.
public class FirebaseActivity extends AppCompatActivity {
private WebView firebase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firebase);
firebase = findViewById(R.id.firebase);
this.firebase.getSettings().setDomStorageEnabled(true);
this.firebase.getSettings().setJavaScriptEnabled(true);
firebase.loadUrl("https://console.firebase.google.com/");
}
}
Setting WebViewClient should resolve the issue.
public class FirebaseActivity extends AppCompatActivity {
private WebView firebase;
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firebase);
firebase = findViewById(R.id.firebase);
firebase.setWebViewClient(new MyWebViewClient());
firebase.loadUrl("https://console.firebase.google.com/");
firebase.requestFocus();
}
}

The source code to refresh WebView periodically on Android Studio 3.1.1 doesn't work

I get this answer from StackOverflow old question-answer. But when I put it on Android Studio, it comes with an error. How to solve it? Please, take a look at this screenshot and this piece of code:
public class MainActivity extends AppCompatActivity {
public WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) findViewById(R.id.webView);
WebSettings webSettings= webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://obs6.com/");
webView.setWebViewClient(new WebViewClient());
Toast.makeText(LogThirdPager.this, "Hello", Toast.LENGTH_SHORT).show();
reload();
}
public void reload() {
final Handler handler = new Handler() {
#Override
public void publish(LogRecord record) {
}
#Override
public void flush() {
}
#Override
public void close() throws SecurityException {
}
};
webView.postDelayed(new Runnable() {
Toast.makeText(LogThirdPager.this, "Hello", Toast.LENGTH_SHORT).show();
reload();
},5000);
}
}
Change LogThirdPager.this in two lines to MainActivity.this. LogThirdPager.this cannot be referenced from MainActivity.

How I can get a listner implementation in Android Activity

I have an activity (AuthenticationActivity) on Android with an interface. Another class implements this interface. I need to parse an interface object for AuthenticationActivity so when one event happens, another class performs an action.
public class MyWebViewActivity extends Activity {
private WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new OAuthWebViewClient());
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("https://www....");
}
private class OAuthWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(App.callbackUrl)) {
String urls[] = url.split("=");
// listener.onComplete(urls[1]); // HERE I NEED FOR LISTNER
return true;
}
return false;
}
}
public interface OAuthDialogListener {
public abstract void onComplete(String accessToken);
public abstract void onError(String error);
}
}
I have in other class:
OAuthDialogListener listener = new OAuthDialogListener() {
#Override
public void onComplete(String code) {
getAccessToken(code);
}
#Override
public void onError(String error) {
mListener.onFail("Authorization failed");
}
};
ann this class call:
Intent intent = new Intent(this, MyWebViewActivity.class);
startActivity(intent);
how the MyWebViewActivity clas uses listener?
inside OAuthWebViewClient class initialize the OAuthDialogListener and them implement it with MyWebViewActivity
try
public class MyWebViewActivity extends Activity implements OAuthWebViewClient {

Android Webview doen't work correctly

This site works well in android browsers but it doesn't work in android webview.
I set internet permission and did all things.
Who can teach me?
public class MainActivity extends AppCompatActivity {
WebView web_view;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OneSignal.startInit(this).init();
setContentView(R.layout.activity_main);
web_view = (WebView)findViewById(R.id.webView);
getSupportActionBar().hide();
web_view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
web_view.getSettings().setDomStorageEnabled(true);
web_view.getSettings().setJavaScriptEnabled(true);
web_view.setWebViewClient(mWebViewClient);
web_view.addJavascriptInterface(new MyJavaScriptInterface(), "android");
web_view.loadUrl("http://182.72.55.182/pms/employee");
}
WebViewClient mWebViewClient = new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.android.onUrlChange(window.location.href);");
};
};
class MyJavaScriptInterface {
#JavascriptInterface
public void onUrlChange(String url) {
Log.d("hydrated", "onUrlChange" + url);
}
}
}
make sure that your regarding website becoming live in everywhere!
webview doesn't display any local host website
Try to put WebViewClient part inside the onCreate.

DroidGap: how get webpage?

My source:
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
}
#Override
public boolean dispatchTouchEvent(MotionEvent e) {
super.dispatchTouchEvent(e);
// Now what page ??
return true;
}
How to get the current web page?
Create one external javascript file, write following code in it:
&ltscript>
document.getElementByTagName("body").addEventListener('touchstart',touchPagePressed);// you can use 'onclick' also
function touchPagePressed()
{
alert(touchPagePressed');
var sPath=window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
alert(sPage);
MyAndroid.performClick(sPage);
}
&lt/script>
include that external js file in every html page.
after this, write following code in your onCreate method:
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new Object()
{
public void performClick(String currentPageName)
{
// Deal with a click on the body tag
System.out.println("PageName- "+currentPageName);
}
},"MyAndroid");
}
}

Categories

Resources