Ui Applicatiopn class use in other class - blackberry

(public class GPSDemo extends UiApplication) its a class name
i want to use this class in this code. i am make a object of this class but throw the exception
here is the place where we use it
popRunnable = new Runnable()
{
public void run()
{
/*StartUpScreen mainScreen = new StartUpScreen();
pushScreen(mainScreen); */
GPSDemo mainScreen =new GPSDemo();
// mainScreen.enterEventDispatcher();
//pushScreen(mainScreen);
}
};
who we use it

UiApplication and MainScreen are different things
public class Main extends UiApplication{
Main() {
pushScreen(new GPSDemo());
}
public static void main(String args[]) {
Main app = new Main();
app.enterEventDispatcher();
}
class GPSDemo extends MainScreen {
GPSDemo() {
this.setTitle("GPSDemo");
}
}

Related

Getting TypeLiterals via method to reduce verbosity

I want to reduce the verbosity of binding a generic interface to several implementations based on TypeLiterals...
I have an interface FieldComputer<T extends ComputeField> where ComputeField is my model interface.
Tried extending a ShortLiteral class (see example below) to reduce the verbosity but it doesn't seem to work. would like to understand why?
// A typical Guice Module
public class ConflationModule implements Module {
// typical overridden configure method
public void configure(Binder binder) {
// Works but is verbose....
bindField_1(binder,
new TypeLiteral<FieldComputer<ComputeFieldImpl>>(){},
FieldComputerImpl.class);
// Doesn't Work
bindField_1(binder,
new ShortLiteral<ComputeFieldImpl>(){},
FieldComputerImpl.class);
// Doesn't Work
bindField_2(binder,
new ShortLiteral<ComputeFieldImpl>(){},
FieldComputerImpl.class);
}
private static class ShortLiteral<CF extends ComputeField> extends TypeLiteral<FieldComputer<CF>>{}
private <CF extends ComputeField> void bindField_1(Binder binder,
TypeLiteral<FieldComputer<CF>> typeLiteral,
Class<? extends FieldComputer<CF>> clazz
) {
binder.bind(typeLiteral).to(clazz);
}
private <CF extends ComputeField> void bindField_2(Binder binder,
ShortLiteral<CF> typeLiteral,
Class<? extends FieldComputer<CF>> clazz
) {
binder.bind(typeLiteral).to(clazz);
}
}
I would suggest you just create TypeLiteral programmatically, here is an example how to do it with different implementations of one interface:
class TypeLiteralModule extends AbstractModule {
#Override
protected void configure() {
customBind(String.class, StringConsumer.class);
customBind(Integer.class, IntegerConsumer.class);
}
private <T> void customBind(Class<T> clazz, Class<? extends Consumer<T>> impl) {
var typeLiteral = (TypeLiteral<Consumer<T>>) TypeLiteral.get(Types.newParameterizedType(Consumer.class, clazz));
bind(impl).in(Singleton.class);
bind(typeLiteral).to(impl);
}
}
class StringConsumer implements Consumer<String> {
#Override
public void accept(String s) {
}
}
class IntegerConsumer implements Consumer<Integer> {
#Override
public void accept(Integer s) {
}
}

Blackberry global popup

I'm trying to popup a global dialog from a background thread that I started from an alternate entry point.
public static void main(String[] args) {
MyApp theApp = new MyApp();
if (args != null && args.length > 0 && args[0].equals("test")) {
new Thread(new Runnable() {
public void run() {
try {
synchronized (UiApplication.getEventLock()) {
UiEngine ui = Ui.getUiEngine();
Screen screen = new Dialog(Dialog.D_OK, "Test", Dialog.OK,
Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION),
Manager.VERTICAL_SCROLL);
ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_MODAL);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}).start();
} else {
theApp.enterEventDispatcher();
}
}
I tried so many variations to make it work but it's still not showing up. I tried
synchronizing Application.getEventLock(), I also tried
UiApplication.getUiApplication().invokeLater,
UiApplication.getUiApplication().invokeAndWait. I even tried synchronizing the eventlock first before calling the invokeLater (which I think is redundant, but I still tried...). I'm not sure what I'm doing wrong.
okk i am giving you a sample demo ....
First of all edit the BlackBerry_App_Descriptor.xml click on Application Tab
In ApplicationArgument write alternate and check Auto Run on start up
Click on Alternate Entry Points click on add and write in the title BackgroundApp
Make a class which will extend Application class rather than UiApplication class like this way
import net.rim.device.api.system.Alert;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.component.Dialog;
public class BackGroundApp extends Application {
// this class is used for the background processing .....
public void startBackgroundThread()
{
new Thread(){
public void run() {
while (true) {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (getEventLock()) {
//with this UiEngine pushGlobal dialogs
//whenever with the app in background
UiEngine ui = Ui.getUiEngine();
Screen screen = new Dialog(Dialog.D_OK, "You have updates!",
Dialog.OK, Bitmap
.getPredefinedBitmap(Bitmap.EXCLAMATION),
Manager.VERTICAL_SCROLL);
ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_QUEUE);
}
}
}
}.start();
}
}
Make a class which will extend UiApplication class like this way
public class GuiTest extends UiApplication {
static Timer t;
public static void main(String[] args) {
if(args.length>0&&"alternate".equals(args[0])){
BackGroundApp app = new BackGroundApp();
app.startBackgroundThread();
app.enterEventDispatcher();
}
else{
GuiTest test = new GuiTest();
test.enterEventDispatcher();
}
}
public GuiTest(){
Myscreen screeMyscreen = new Myscreen();
pushScreen(screeMyscreen);
}
}
Now make a class MyScreen and add all your Ui in it .... and push the screen
public class Myscreen extends MainScreen {
public Myscreen(){
CreateGui();
}
public void CreateGui(){
// Your Ui goes here .......
}
}
run the sample you will see after one minute a dialog will appear on your screen no matter if you are in the application or out side of it. Thanks may be this might be help full.

Blackberry: limit app to portrait mode

I've tried this piece of code in my UiApplication class, but I get an illegalstatexception.
Ui.getUiEngineInstance().setAcceptableDirections(Display.DIRECTION_PORTRAIT);
I dont want my app to change its orientation. I want it to stay in portrait mode.
EDIT:
How it is used:
public class HelloWorld extends UiApplication {
public static void main(String[] args){
Ui.getUiEngineInstance().setAcceptableDirections(Display.DIRECTION_PORTRAIT);
HelloWorld theapp = new HelloWorld();
theapp.enterEventDispatcher();
}
public HelloWorld(){
pushScreen(new FeaturedScreen());
}
}
DemoClass()
{
int direction = Display.DIRECTION_PORTRAIT;
Ui.getUiEngineInstance().setAcceptableDirections(direction);
pushScreen(new AppScreen(this));
}
public static void main(String[] args) {
DemoClass app1 = new DemoClass();
app1.enterEventDispatcher();
}
public class HelloWorld extends UiApplication {
public static void main(String[] args){
UiEngineInstance ui = Ui.getUiEngineInstance();//I have added this new line in your code
ui.setAcceptableDirections(Display.DIRECTION_NORTH);
HelloWorld theapp = new HelloWorld();
theapp.enterEventDispatcher();
}
public HelloWorld(){
pushScreen(new FeaturedScreen());
}
}

Blackberry: display Alert/Status/Dialog early and exit

Let's say I have a typical Blackberry app:
public class MyApp extends UiApplication {
public static void main(String[] args) {
MyApp app = new MyApp();
app.enterEventDispatcher();
}
public MyApp() {
pushScreen(new MyScreen());
}
}
and already at the beginning I notice, that a mandatory condition is missing (wrong Display dimensions; missing SD card; some IT policy; etc.)
Is there a way to display a short and quick message to the user (in the form of Alert/Status/Dialog/whatever) and exit straight away - without/before instantiating a complex Screen/registering Acceleration listeners/installing complex CleanupRunnable?
I've tried Status.show(), Dialog.alert() - they do not work (RuntimeException "pushModalScreen called by a non-event thread"):
public class MyScreen extends MainScreen {
public MyScreen() {
if (Display.getWidth() < 400) {
Status.show("Goodbye");
return;
}
}
}
Instead of direct invocation use invokeLater. Sample is below:
Application.getApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Your message here...");
}
});
Instead of Dialog.inform you may use Status.show()
Actually the following is better, than what's suggested by Rafael - because it doesn't have the ugly white screen underneath. Here is my complete example MyApp.java:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.image.*;
public class MyApp extends UiApplication {
public static void main(String[] args) {
MyApp app = new MyApp();
app.enterEventDispatcher();
}
public MyApp() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen implements DialogClosedListener {
Dialog myDialog = new Dialog(Dialog.OK, "Goodbye!", 0, Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), Dialog.GLOBAL_STATUS);
public MyScreen() {
// XXX just some condition, like wrong dimensions or IT policy
if (Display.getWidth() > 40) {
myDialog.setDialogClosedListener(this);
Application.getApplication().requestBackground();
Application.getApplication().invokeLater(new Runnable() {
public void run() {
myDialog.show();
}
});
return;
}
// XXX heavy stuff to be skipped
}
public void dialogClosed(Dialog dialog, int choice) {
if (dialog == myDialog) {
System.out.println("XXX exiting XXX");
System.exit(1);
}
}
}

How to enter the blackberry app if I am not using enterEventDispatcher?

My application extends the ui.Manager class. Is it possible to enter the app without using the enterEventDispatcher. As it needs me to inherit the Application/UiApplication to do that.
Is multiple inheritance the solution?
Multiple inheritance is rather not possible in Java. So is it about using interfaces?
You should really inherit App from UiApplication (if it has some UI) or from Application (if it's background app, service).
If you have some Manager extension, place it into Screen extension.
Like this:
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
public class CenterButtonPanelApp extends UiApplication {
public CenterButtonPanelApp() {
pushScreen(new Scr());
}
public static void main(String[] args) {
CenterButtonPanelApp app = new CenterButtonPanelApp();
app.enterEventDispatcher();
}
}
class Scr extends MainScreen {
public Scr() {
CenterButtonPanel centerPanel = new CenterButtonPanel();
add(centerPanel);
}
}
class CenterButtonPanel extends HorizontalFieldManager {
int mWidth = Display.getWidth();
public CenterButtonPanel() {
super(FIELD_HCENTER);
}
public int getPreferredWidth() {
return mWidth;
}
protected void sublayout(int maxWidth, int maxHeight) {
super.sublayout(mWidth, maxHeight);
setExtent(mWidth, maxHeight);
}
}

Resources