Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
djpinto
Posts: 41
Registered: ‎11-15-2012
My Device: Curve 9320
My Carrier: Optimus

run application in background

Good morning!

 

I'm a new blackberry developer, and I'm trying to develop an application that has to run in the background and some other normally.

 

My code is this:

 

public static void main(String[] args){

 

if(args!=null){
BackGroundApp.getBackGroundApp().run();
}
else{
BBackground3 app = new BBackground3();
app.initializeLater();
app.enterEventDispatcher();
}

}

 

 

private void initialize(){
System.out.println("mensagem teste");
_screen = new MainScreen();
_addNew = new ButtonField("Adicionar", ButtonField.FIELD_HCENTER
| ButtonField.CONSUME_CLICK);
_addNew.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("#####################################This is a print statement");
Dialog.alert("Mensagem a aparecer");
}
});

_viewData = new ButtonField("Ver", ButtonField.FIELD_HCENTER
| ButtonField.CONSUME_CLICK);
_viewData.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("mensagem a aparecer 2");
}
});
_screen.setTitle(new LabelField("Aplicacao titulo",
LabelField.USE_ALL_WIDTH));
_screen.add(_addNew);
_screen.add(_viewData);
pushScreen(_screen);

}

private void initializeLater(){
invokeLater(new Runnable(){
public void run(){
initialize();
}
});
}

 

 

//class background

private static class BackGroundApp extends Thread{
private static BackGroundApp thread = null;

boolean stopThread = false;
public synchronized void stop(){
stopThread = true;
}

public static BackGroundApp getBackGroundApp(){
if(thread == null){
thread = new BackGroundApp();
}

return thread;
}

public void run(){
while(!stopThread){

System.out.println("Applicatioin is running!");

try{
sleep(5000);
}
catch(Exception e){
//Exception handling would go here.
}
}
}
}

 

 

The application is start in background, but when I'm click for start app appeared "already application running".

Who is the problem?

 

Cumps Diogo Pinto

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: run application in background

[ Edited ]

Unfortunately, the best way to do this depends on your actual application requirements.

 

Most applications will require some background processing, by which we mean some processing that is not related to what the user is seeing on the screen.  This is usually handled by starting a number of Threads.

 

A lot of applications have a foreground part that runs sometimes and a background part that runs all the time.  The easiest way to implement this, though it can cause communication problems, is to have an alternate entry:

http://supportforums.blackberry.com/t5/Java-Development/Set-up-an-alternate-entry-point-for-an-appli...

 

So one entry, which is run at startup, starts the background processing, the other entry starts the GUI. 

 

Another alternative is to have one application that just goes into the background when the user dismisses it rather than closing.  For this you would use the Application method

requestbackground().

This approach woudl use background Threads for its background processing. 

 

Regardless of the approach you choose to use, this sort of question has been asked before, and you will find loads more, including some very use KB articles, using the Search box that you find near the top right of the screen. 

Please use plain text.
Contributor
djpinto
Posts: 41
Registered: ‎11-15-2012
My Device: Curve 9320
My Carrier: Optimus

Re: run application in background

Yeah already tried that way but the result is the same. That run only when the application was ruled good! How can I do so? Do you have any examples or something?

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: run application in background

Sorry, this doesn't make sense to me:

"That run only when the application was ruled good!"

I don't know anything about applications being made good? 

 

I think you will find some very useful stuff if you search, for example this KB article may help you get a background application going:

http://supportforums.blackberry.com/t5/Java-Development/Create-a-Background-Application/ta-p/445226

 

The best thing to do is to get a foreground application working, then use the alternate entry to try to start a background application.  So this KB article plus the one I gave you previously should be enough to get you going.  If not, please search for more information - you will find a load of useful stuff that will help you in the future too. 

 

Please use plain text.
Contributor
djpinto
Posts: 41
Registered: ‎11-15-2012
My Device: Curve 9320
My Carrier: Optimus

Re: run application in background

I apologize but I am using the google translator.

 

The aim was to take background running only access to the server every x time, and thus update the database. And the rest is done in the APPLICATION process to run by user.

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: run application in background

[ Edited ]

Apologies, having to translate everything must make this very hard.

 

Does the Server connection happen 24 hours a day, or only when the user has the application open?

Please use plain text.
Contributor
djpinto
Posts: 41
Registered: ‎11-15-2012
My Device: Curve 9320
My Carrier: Optimus

Re: run application in background

Do not worry :smileywink:.

 

The connection to the server will be in principle once a day but it has to happen regardless of whether the user opens the application or not! And having another background process with notifications but this is the next step now wanted to see if the background was working!

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: run application in background

The first step is to get the UI Application working as you want it.  Then set a startup argument for this Ii Application in the BlackBerry_App_Descriptors as described in the KB article I pointed you to before:

http://supportforums.blackberry.com/t5/Java-Development/Set-up-an-alternate-entry-point-for-an-appli...

 

Now change your main() processing to make sure there is an args parameter - this is the same as is in the KB article.  Make sure the GUI application is started OK. 

 

So the only thing left is to get the background p[processing going.  To do this, edit BlackBerry_App_Descriptor.xml and set Autostart on.  Then in main when you have NOT been started as a GUI, start your own application - not a UiApplication as users will see that - as is shown in the second KB article I gave you:

http://supportforums.blackberry.com/t5/Java-Development/Create-a-Background-Application/ta-p/445226 

 

This is running an infinite loop - which is what you want.  In that loop, at a specific time, you just need to start your Server Thread processing.

 

Hope this gets you going.

Please use plain text.
Contributor
djpinto
Posts: 41
Registered: ‎11-15-2012
My Device: Curve 9320
My Carrier: Optimus

Re: run application in background

Hello again!

 

I'm create 2 class:

first class is

 

class BBackground3 extends UiApplication {
private ButtonField _addNew, _viewData;
private MainScreen _screen;


public static void main(String[] args){

if(args!=null && args.length>0 && args[0].equals("gui")){
BBackground3 theApp = new BBackground3();
theApp.enterEventDispatcher();
}
else{
GUILessApp guiLessApp = new GUILessApp();
guiLessApp.enterEventDispatcher();
}
}


public BBackground3(){
initializeLater();
}


private void initialize(){
System.out.println("mensagem teste");
_screen = new MainScreen();
_addNew = new ButtonField("Adicionar", ButtonField.FIELD_HCENTER
| ButtonField.CONSUME_CLICK);
_addNew.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {

Dialog.alert("Mensagem a aparecer");
}
});

_viewData = new ButtonField("Ver", ButtonField.FIELD_HCENTER
| ButtonField.CONSUME_CLICK);
_viewData.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
}
});
_screen.setTitle(new LabelField("Aplicacao titulo",
LabelField.USE_ALL_WIDTH));
_screen.add(_addNew);
_screen.add(_viewData);
pushScreen(_screen);
}

private void initializeLater(){
invokeLater(new Runnable(){
public void run(){
initialize();
}
});
}
}

 

 

 

 

and second class

 

public final class GUILessApp extends Application
{
private BackGroundApp backGroundApp;

public static void main(String[] args)
{
GUILessApp theApp = new GUILessApp();
theApp.enterEventDispatcher();
}

public GUILessApp()
{
//Creates and starts a new BackGroundApp thread.
backGroundApp = new BackGroundApp();
backGroundApp.start();
}

//The thread that will run in the background.
private class BackGroundApp extends Thread
{
boolean stopThread = false;
public synchronized void stop()
{
stopThread = true;
}

public void run()
{
while (!stopThread)
{
//You would perform your processing here.
//This sample just prints out a line
//to the BlackBerry JDE Output Window
System.out.println("Application is running");

//Sleep for 5 seconds to prevent the
//application from running out of control
try
{
sleep(5000);
}
catch (Exception e)
{
//Exception handling would go here.
}
}
}
}
//Stop the thread on exit.
protected void onExit()
{
backGroundApp.stop();
}
}

 

 

Through these links thus created the application, but still appears three warnings:

Warning!: method 'mypackage.GUILessApp.main(String[])' not invoked.

Warning!: method 'mypackage.GUILessApp.onExit()' not invoked.

Warning!: method 'mypackage.GUILessApp$BackGroundApp.stop()' not invoked.

 


I do not understand why it's there because everything stated!

 

Cumps

Please use plain text.
Developer
peter_strange
Posts: 19,603
Registered: ‎07-14-2008
My Device: Not Specified

Re: run application in background

It appears I have confused you.

 

1) A project only has one main.  The args to that main determine what happens.  So you can remove the main from GUILessApp.

 

2) I suspect you are trying to use onExit to stop your GUILessApp.  onExit is not a standard method for an Application.  When were you planning on using it?

 

That said, the code looks like it will work.  Is it?

Please use plain text.