How to call javascript functions from blackberry native? - blackberry

I am developing an app where i need to call some methods from blackberry native to javascript.
when i click on back key down event , i want to trigger the onBackKeyDown() method, which is declared in javascript.
Main.java
protected boolean keyDown(int keycode, int time) {
// TODO Auto-generated method stub
if(Keypad.key(keycode) == Keypad.KEY_ESCAPE)
{
// onBackKeyDown();
// i want to call the following method which is declared in main.js file
Dialog.alert("this is back button");
return true;
}
return super.keyDown(keycode, time);
}
main.js
function onBackKeyDown() {
try {
if ($.mobile.activePage.is("#Page1")) {
$.mobile.changePage("#page5");
} else if ($.mobile.activePage.is("#page2")) {
$.mobile.changePage("#main");
} else if ($.mobile.activePage.is("#page3")) {
$.mobile.changePage("#main");
} else if ($.mobile.activePage.is("#main")) {
navigator.app.exitApp();
}
} catch(e) {
alert("Exception:ConsoleLog.log:" + e);
}
}
As i am having idea that by using "extendScriptEngine" , the methods declared in javascript are invoked in native. But here how to invoke the methods in javascript which are in native as per my above code... can anyone please help me with this...

You don't show this code, but I have to assume that your app has some Screen that contains some kind of browser field, which is displaying HTML content.
I can't tell you for sure without seeing that code, but what I would recommend is to use net.rim.device.api.browser.field2.BrowserField (Browser Field 2), if your app only needs to support OS 5.0 and higher.
If you have to support less than OS 5.0, I'm not sure how to do that.
Anyway, with this 5.0+ BrowserField, you can do this:
BrowserFieldConfig config = new BrowserFieldConfig();
config.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE); // should be the default
// Browser basic initialization
BrowserField _browserField = new BrowserField(config);
and then
protected boolean keyDown(int keycode, int time)
{
if(Keypad.key(keycode) == Keypad.KEY_ESCAPE)
{
// i want to call the following method which is declared in main.js file
_browserField.executeScript("onBackKeyDown()");
Dialog.alert("this is back button");
return true;
}
return super.keyDown(keycode, time);
}

Related

Move Focus is not working properly on List Field

I am working on ListView section, in this, the user can search the content by name and directly move at the first element of List via pressing a keyboard button. Like, if you press button B from (right vertical manager) it will scroll the list and move focus to first record of B.
The code is working fine in simulator but it's not working on Touch device - I have BB 9380 Curve.
here is the code for :
LabelField a = new LabelField("A" , FOCUSABLE)
{
protected void paint(Graphics graphics)
{
graphics.setColor(0xC4C4C4);
super.paint(graphics);
}
protected boolean navigationClick(int status, int time)
{
//fieldChangeNotify(1);
injectKey(Characters.LATIN_CAPITAL_LETTER_A);
injectKey(Characters.LATIN_CAPITAL_LETTER_A);
return true;
}
};
private void injectKey(char key)
{
try
{
searchList.setFocus();
KeyEvent inject = new KeyEvent(KeyEvent.KEY_DOWN, key, 0);
inject.post();
/*inject.post();*/
} catch (Exception e) {
Log.d("In injectKey :: :: :: "+e.toString());
MessageScreen.msgDialog("In Inject Key "+e.toString());
}
}
Alternate Solution
I would recommend a different strategy for this. Instead of trying to simulate key press events, I would define one method that handles a keypress of a certain letter, or a touch click on that same letter's LabelField.
Source: blackberry.com
So, you can have code that handles key presses by using
protected boolean keyChar( char character, int status, int time )
{
// you might only want to do this for the FIRST letter entered,
// but it sounds like you already have the keypress handling
// the way you want it ...
if( CharacterUtilities.isLetter(character) )
{
selectLetter(character);
return true;
}
return super.keyChar( character, status, time );
}
and then also handle touch events:
LabelField a = new LabelField("A" , FOCUSABLE)
{
protected void paint(Graphics graphics)
{
graphics.setColor(0xC4C4C4);
super.paint(graphics);
}
protected boolean navigationClick(int status, int time)
{
char letter = getText().charAt(0);
selectLetter(letter);
return true;
}
};
then, simply define a method that takes in one character, and scrolls to the start of that part of the list:
private void selectLetter(char letter);
Key Injection
If you really, really want to simulate key presses, though, you might try changing the code so that it injects two events: key down, and then key up (you're currently injecting two key down events). This might be causing problems.
injectKey(Characters.LATIN_CAPITAL_LETTER_A, true);
injectKey(Characters.LATIN_CAPITAL_LETTER_A, false);
with
private void injectKey(char key, boolean down)
{
try
{
searchList.setFocus();
int event = down ? KeyEvent.KEY_DOWN : KeyEvent.KEY_UP;
KeyEvent inject = new KeyEvent(event, key, 0);
inject.post();
} catch (Exception e) { /** code removed for clarity **/
}
}
Additional Note
For UIs, I like to trigger events on the key up, or unclick events. I think this makes a better experience for the user. So, you could replace keyChar() with keyUp() and navigationClick() with navigationUnclick() if you want to do this.

How to listen for a keyboard event in dart programming

I'm new to google dart and been trying to learn it for a day now. I'm pretty novice to programming in general and I'm trying to read the documentation; however, I feel a bit overwhelmed.
I would like to know the most proper method of creating a interaction for spacebar here key. When one would push spacebar, it would toggle between function void startwatch() , void resetwatch()
I believe this is the correct documentation page also documentation for keyboardEventController
void main() {
}
void startwatch() {
mywatch.start();
var oneSecond = new Duration(milliseconds:1);
var timer = new Timer.repeating(oneSecond, updateTime);
}
void resetwatch() {
mywatch.reset();
counter = '00:00:00';
}
Any further information needed I'll try to respond immediately. Thnk you so much for your help.
To listen to keyboard events and toggle between startwatch() and resetwatch():
void main() {
var started = false;
window.onKeyUp.listen((KeyboardEvent e) {
print('pressed a key');
if (e.keyCode == KeyCode.SPACE) {
print('pressed space');
if (started) {
resetwatch();
} else {
startwatch();
}
started = !started; // A quick way to switch between true and false.
}
});
}
window is an instance of Window class. It's automatically provided for you.
There's also a handy class called KeyEvent, which attempts to eliminate cross-browser inconsistencies. These inconsistencies are usually related to special keys.

Blackberry button click handler

I want to run some Java code when the user clicks on this ToolbarButtonField in my BlackBerry app. I have the following code which is not working. Please tell me where I am wrong.
butHome = new ToolbarButtonField(new StringProvider("Home"));
butHome.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Clicked...");
}
});
You can use:
ToolbarButtonField#invoke
Performs an action when this
ToolbarButtonField is clicked on if
Command has been set. A click is
defined as the following sequence of
touch events: TouchEvent.DOWN,
TouchEvent.CLICK, TouchEvent.UNCLICK
and TouchEvent.UP.
You're going to have to use that in conjuction with the Command framework. If that's not desirable, override ToolbarButtonField#touchEvent for a TouchEvent.UNCLICK event to execute the desired code.
public boolean touchEvent(TouchEvent message) {
if ( message.geEvent() == TouchEvent.UNCLICK ) {
// do what I want.
}
}
Try this:
butHome = new ToolbarButtonField(new StringProvider("Home")) {
protected boolean navigationClick(int status, int time) {
System.out.println("Clicked...");
return true;
}
});

Turn on Flash as Light on Blackberry

I am new to BlackBerry application development and trying to make a simple application to turn my flash light on as a torch. I know there are several applications that do this already, but I would like to try do it on my own.
I have installed eclipse and all the necesary add on to get my development environment running. I have also successfully create the stock standard hello world application.
I am however struggling to find out how to do this. I have been reading through the API documentation and started playing with FlashControl, VideoControl and SnapshotControl.
These however don't seem to expose methods to do this.
I know through the video camera I am able to go to options and turn the flash light on and this is exactly what i'm trying to mimic.
The code i have used so far which seems to just set the camera flash to force on is:
Player p = javax.microedition.media.Manager.createPlayer("capture://video");
p.realize();
p.start();
FlashControl flashControl = (FlashControl) p.getControl("javax.microedition.amms.control.camera.FlashControl");
flashControl.setMode(FlashControl.FORCE);
the problem relevant to the flash control has been resolved by me
as per i am using the flash control on my recent application on
camera.
Here is the code which i used :
public Camera(int j)
{
k = j;
try
{
Player player = Manager.createPlayer("capture://video");
player.realize();
_videoControl = (VideoControl) player.getControl("VideoControl");
flashControl = new FlashControl()
{
public void setMode(int mode)
{
// TODO Auto-generated method stub
}
public boolean isFlashReady()
{
// TODO Auto-generated method stub
return false;
}
public int[] getSupportedModes()
{
// TODO Auto-generated method stub
return null;
}
public int getMode()
{
// TODO Auto-generated method stub
return 0;
}
};
flashControl = (FlashControl) player
.getControl("javax.microedition.amms.control.camera.FlashControl");
try {
if (k == 1)
{
flashControl.setMode(FlashControl.AUTO);
Dialog.alert("slect Auto");
}
else if (k == 2)
{
flashControl.setMode(FlashControl.OFF);
Dialog.alert("slect No");
}
}
catch (Exception e)
{
System.out.println(e);
}
if (_videoControl != null)
{
_videoField = (Field) _videoControl.initDisplayMode(
VideoControl.USE_GUI_PRIMITIVE,
"net.rim.device.api.ui.Field");
// _videoControl.setDisplaySize(330, 420);
// _videoControl.setDisplayLocation(getContentWidth(),
// getContentHeight());
_videoControl.setVisible(true);
add(_videoField);
capture = new ButtonField("Capture", Field.FIELD_HCENTER);
capture.setChangeListener(this);
add(capture);
player.start();
}
}
catch (Exception e)
{
System.out.println(e);
}
}
this logic has been implemented simultaneously with Pinkesh as my colleage
in the comapny
The FlashControl class, available from OS 5.0 allows you to turn the flash on. Just set a flash control on your player with the FORCE flag:
FlashControl flash = (FlashControl)player.getControl("javax.microedition.amms.control.camera.FlashControl");
if(flash!=null) {
try {
flash.setMode(FlashControl.FORCE);
} catch(IllegalArgumentException iae){}
}
For this to work, you'll probably need to open a player to record video or take a picture. I'm not showing that in my code for the sake of brevity, but here you can read a tutorial. If your app is only about turning on the flash, you'd probably like to have the video field hidden.
Try something like this
LED.setState(LED.STATE_ON); // for LED
Backlight.enable(true); // for Screen
this.setMode(FlashControl.ON); // for flash light.
or else import this package
package lsphone.flash.microfireps;

problem with back button

when i am pressing the back button a pop screen is displayed which shows three button save, discard and cancel button i don't want this screen to be popped up. is this possible.
Thanks in advance
The default behaviour of the back button is to save changes for dirty screens. Rewrite the onClose() method to overwrite the default behaviour.
public boolean onClose() {
int choice = Dialog.ask(Dialog.D_YES_NO, "¿Do you want to exit?", Dialog.YES);
if (choice == Dialog.YES) {
//write a close() routine to exit
close();
}
return true;
}
You return true because you managed the ESC button pressed event. Review the Screen class docs.
You can also change the default behaviour of the ESC button rewriting the keyChar method as follows:
protected boolean keyChar(char character, int status, int time) {
if (character == Keypad.KEY_ESCAPE) {
onClose();
return true;
}
return super.keyChar(character, status, time);
}
close() should be somenthing like:
public void close() {
System.exit(0);
}
Override the onSavePrompt method. Then that screen will not come. Actually that popup screen will come only when something is changed on your screen. So it will ask you for the appropriate action.
protected boolean onSavePrompt() {
return true;
}
Skip the saving prompt with it
protected boolean onSavePrompt() {
return false;
}
Override onClose() method like this:
public boolean onClose() {
close();
return true;
}
you will not get that annoying alert message.

Resources