I have to call a javascript method on OK clicked from an alert created in java plugin. Actually the alert is displayed on click of the notification from the status bar.
// Method to collect info from pending intent on notification click
public void launchNotification()
{
Intent intent = getIntent();
if(intent!=null)
{
if(intent.getExtras().getBoolean("fromNotification"))
{
final String notificationType = intent.getStringExtra("notificationType");
final String twoWeeksNotificationType = intent.getStringExtra("2WeeksNotificationType");
final String tasksNotificationType = intent.getStringExtra("tasksNotificationType");
if(notificationType != null || twoWeeksNotificationType != null || tasksNotificationType != null)
{
if(notificationType != null && notificationType.equals("dailyReviewAvailable"))
{
NotificationManager nMgr = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel("DailyReview", 0);
Handler handler = new Handler();
handler.postDelayed(
new Runnable() {
public void run() {
showAlertForNotifications(notificationType);
}
}, 1000L);
}
intent.putExtra("fromNotification",false);
}
else
finish();
}
}
// Method for launching alert after notification is clickedpublic void
showAlertForNotifications(String notificationIdStr) {
if(notificationIdStr.equals("dailyReviewAvailable")) {
Log.d("Notification", "Daily Review Start");
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Actions Hero");
adb.setMessage("Would you like to send your daily review?");
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
showDailyReview();
}
});
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
// Action for 'Cancel' Button
dialog.cancel();
finish();
}
});
//adb.setIcon(R.drawable.icon);
adb.show();
}
else {
//do later
}
}
// Method to call javascript function on OK click of alert button in java
public void showDailyReview() {
Log.v("showDailyReview", "Before");
// This javascript function cannot be invoked super.sendJavascript("javascript:app.runningDailyReview('DailyReview')");
Log.v("showDailyReview", "After");
finish();
}
How should I invoke the javascript method on clicking OK on the alert shown ??
Thanks..
Related
I have a WinForm application that using cefsharp web browser. From what I learnt about cefsharp, the events MouseDown, Click cannot trigger in WinForm. So I decided to use javascript to send a message to the console, and trigger ChromiumWebBrowser.ConsoleMessage event instead. But that did not work well. Did I mess up the javascript code or execute the code wrongly?
Here are the codes, please noted that UI.cs is a Form object. A panel object pn_Browser already created in the design view. What I tried to do is, click any element and send "success" to the console. Then the Background show the received message if the message in console.log is "success".
Background.cs
public ChromiumWebBrowser browser;
public Background()
{
browser = new ChromiumWebBrowser("https://www.google.com");
browser.ConsoleMessage += browser_ConsoleMessage;
browser.FrameLoadEnd += browser_FrameLoadEnd;
}
public void browser_ConsoleMessage(object sender, ConsoleMessageEventArgs e)
{
if (e.Message == "success")
{
System.Windows.Forms.MessageBox.Show("Received message.");
}
}
public void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
Chromium.ExecuteScriptAsync("document.addEventListener(\"click\", function() {alert('Detect a click.');});");
}
UI.cs
public UI()
{
InitializeComponent();
Background background = new Background();
pn_Browser.Controls.Add(background.browser);
background.browser.JavascriptMessageReceived += browser_JavascriptMessageReceived;
background.browser.ExecuteScriptAsyncWhenPageLoaded(
"function test() {console.log(\"success\")}" +
"document.addEventListener(\"click\", test);"
);
}
delegate void JavascriptReceivedCallback(object sender, JavascriptMessageReceivedEventArgs e);
private void browser_JavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e)
{
if (InvokeRequired)
{
JavascriptReceivedCallback callback = new JavascriptReceivedCallback(Browser_JavascriptMessageReceived);
Invoke(callback, new object[] { sender, e };
}
else
{
MessageBox.Show(e.Message.ToString());
}
}
I use GeckoWebBrowser component for get some webpages. But I don't know when page JavaScripts fully completed.
For detecting page full load I use event DocumentCompleted.
private GeckoWebBrowser wb = new GeckoWebBrowser();
private bool doc_loaded = false;
wb.DocumentCompleted += Browser_DocumentCompleted;
doc_loaded = false;
wb.Navigate(url);
Wait_Load();
private void Browser_DocumentCompleted(object sender, EventArgs e)
{
doc_loaded = true;
}
private void Wait_Load()
{
while (doc_loaded == false)
{
Application.DoEvents();
Application.RaiseIdle(new EventArgs());
}
}
How detect that all JavaScripts completed in GeckoWebBrowser?
I Have a Alert like this
new AlertDialog.Builder(context)
.setTitle("Exit Confirmation")
.setMessage("Are you sure you want to Exit?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
I want to change Button text of this alert.
Suppose android.R.string.yes is "I Do" and android.R.string.no is "I Don't"
Now I want to set comic.tft to it.
Please help...
For the font, after alert.show(), you can use:
Button btnPositive = alert.getButton(Dialog.BUTTON_POSITIVE);
btnPositive.setTypeFace(myTypeFace);
You can use something like this:
getResources().getString(R.string.about)
in string.xml
<string name="about">I do</string>
<string name="aboutno">I don't</string>
Code:
new AlertDialog.Builder(context)
.setTitle("Exit Confirmation")
.setMessage("Are you sure you want to Exit?")
.setPositiveButton(getResources().getString(R.string.about), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(getResources().getString(R.string.aboutno), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
I have created an android application with simple webview function. In my html page which webview loads, I am using following script.
var message="!!YOU CANNOT COPY ANY TEXT OR IMAGE FROM THIS APP!";
function clickIE4()
{
if (event.button==2)
{
alert(message);
return false;
}
}
function clickNS4(e)
{
if (document.layers||document.getElementById&&!document.all)
{
if (e.which==2||e.which==3)
{
alert(message);
return false;
}
}
}
if (document.layers)
{
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById)
{
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("alert(message);return false")
//for disable select option
//document.onselectstart = new Function("alert(message);return false");
When I open this in android webview application, it does not show the alert message. It works fine when I open it in normal browser.
You need to create WebChromeClient and set to WebiView. Also override onJsAlert method.
WebChromeClient webChromeClient = new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
})
.create()
.show();
return true;
}
}
webView.setWebChromeClient(webChromeClient);
I have a webview in my android app. I want to show a banner only if the web page is viewed on browser. I created an interface and it tests fine but because the web page doesn't recognize the function the other javascript on the page doesn't load. Here is my Code:
Interface Class
import android.content.Context;
import android.webkit.JavascriptInterface;
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public Boolean getUser() {
return true;
}
}
Activity Class
public class MainActivity extends Activity {
ProgressBar progressBar;
WebView webview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView)findViewById(R.id.webview);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
webview.addJavascriptInterface(new WebAppInterface(this),"Android");
this.webview.getSettings().setUserAgentString(
this.webview.getSettings().getUserAgentString()
+ " "
+ getString(R.string.user_agent_suffix)
);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.VISIBLE);
}
}
});
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl("http://www.getonfleek.com");
ParsePush.subscribeInBackground("test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
}
HTML
</script>
<script type="text/javascript">
$(document).ready(function(){
var user = Android.getUser();
if(user == null){
//do something on web
}else{
//do something in app
}
});
</script>
Check to make sure the interface exists before trying to call it.
<script type="text/javascript">
$(document).ready(function(){
var user = null;
if(typeof Android !== 'undefined'){
user = Android.getUser();
//do something in app
}else{
//do something on web
}
});
</script>