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
New Developer
gtSasha
Posts: 19
Registered: ‎08-21-2009
My Device: Not Specified
Accepted Solution

Auto-run on startup problem

 

Hello,

 

I am having a strange issue trying to have my application start on device power-up.  I set the appropriate flag in the project settings and it would start my app, but it would be in some kind of a "sleep" state - it will appear in the list of running applications, but it won't actually run in the background until opened up by the user.  I could not find any post on this forum describing a similar issue.  Do you have any idea what is going on?

 

I write a simple app to test what is happening, but it dies with error 156 - application is not responding.  It does not look like I am blocking the event thread so I don't know why it would not respond.

 

Here is the sample code.  Do you see anything wrong?

 

 

/*
 * AutoStart.java
 *
 * © <your company here>, 2003-2008
 * Confidential and proprietary.
 */
import java.util.Timer;
import java.util.TimerTask;

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.system.ApplicationManager;

/**
 * 
 */
class AutoStart extends UiApplication implements FieldChangeListener {
    
    private ButtonField mExitBtn;
    private LabelField mCntLabel;
    
    private Timer mTimer;
   
    private int mSecondCount;
    
    public static void main(String[] args) {
        System.out.println("AutoStart:  entered main()");
        
        ApplicationManager mgr = ApplicationManager.getApplicationManager();
        
        System.out.println("AutoStart:  waiting for system to initialize");
        
        while (mgr.inStartup()) {
            try {
                Thread.sleep(2000);
            }
            catch (Exception e){}
        }
        
        AutoStart theApp = new AutoStart();
        
        System.out.println("AutoStart:  entering dispatcher");
        
        theApp.enterEventDispatcher();
    }
    
    AutoStart() {    
    
        System.out.println("AutoStart:  creating main screen");
        
        // MainScreen is the basic screen or frame class of the RIM UI.
        MainScreen mainScreen = new MainScreen();
        
        mainScreen.setTitle(new LabelField("AutoStart Test", 0));
        
        VerticalFieldManager vfm = new VerticalFieldManager();
    
        mCntLabel = new LabelField("0:00", Field.USE_ALL_WIDTH);
        vfm.add(mCntLabel);
        
        mExitBtn = new ButtonField("Quit", DrawStyle.HCENTER);
        vfm.add(mExitBtn);
        
        mExitBtn.setChangeListener(this);
        
        mainScreen.add(vfm);
        
        mTimer = new Timer();
        
        System.out.println("AutoStart:  scheduling timer");
        
        mTimer.scheduleAtFixedRate(new TimerTask(){
            public void run() {
                timerEvent();
            }
        }, 1000, 1000);
        
        //show screen
        pushScreen(mainScreen);
    }
    
    private void timerEvent() {
        
        System.out.println("AutoStart:  timer event 1");
        
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                
                System.out.println("AutoStart:  timer event 2");
                
                ++mSecondCount;
                
                int minutes = mSecondCount / 60;
                int seconds = mSecondCount % 60;
                
                String strMin = String.valueOf(minutes);
                String strSec = String.valueOf(seconds);
                
                if (strSec.length() == 1) {
                    strSec = "0" + strSec;
                }
                
                mCntLabel.setText(strMin + ":" + strSec);
            }
        });
    }
    
    public void fieldChanged(Field field, int context) {
        if (field == mExitBtn) {
            System.exit(0);
        }
    }
} 

 

 

Please use plain text.
Developer
KushBB
Posts: 374
Registered: ‎04-07-2010
My Device: Bold 9000
My Carrier: Airtel

Re: Auto-run on startup problem

You have to create an Alternate Blackberry Application Entry Point for your application and Pass the Arguments to your main method there..

 

Take a look:

http://www.blackberry.com/developers/javaknowledge/appArchitecture/kpa0207105853.shtml

 

 

Kush

 

Press the Like button (Thumb up) on the left side to thank and
please mark the Thread as solved if its solved...
K.S
Please use plain text.
Developer
peter_strange
Posts: 19,598
Registered: ‎07-14-2008
My Device: Not Specified

Re: Auto-run on startup problem

[ Edited ]

What KushBB says is in my opinion, not correct. 

 

Using an Alternate Entry is an often suggested solution, but one that also brings its own problems.  And in this case, it is not the reason for this problem. 

 

The issue here is that you are trying to do work on the Event Thread, specifically you are waiting for the device to be ready.  There was a KB article that did this sort of delay, but it did it in a Background Thread.  You are doing this in main().

 

Instead I suggest that you use this KB article that describes how to wait for the device to be ready to accept your screen interaction, without the wait loop. 

 

http://supportforums.blackberry.com/t5/Java-Development/Write-safe-initialization-code/ta-p/444795

 

This KB article has similar information:

 

http://supportforums.blackberry.com/t5/Java-Development/Detect-system-availability-on-startup/ta-p/4...

 

A lot of people will tell you that you need to do your initialization using an Alternate Entry.  This is not true.  There are good reasons to use an Alternate Entry for this processing, but it is not applicable in all cases, and is certainly not in this case. 

Please use plain text.
Developer
KushBB
Posts: 374
Registered: ‎04-07-2010
My Device: Bold 9000
My Carrier: Airtel

Re: Auto-run on startup problem

Thanks Peter for correcting me if I was wrong... :smileyhappy:

 

But I still did not get why in some cases we cannot use the Alternate Entry Point...??

Press the Like button (Thumb up) on the left side to thank and
please mark the Thread as solved if its solved...
K.S
Please use plain text.
Developer
swati-srivastava
Posts: 282
Registered: ‎02-25-2010
My Device: curve,bold,storm
My Carrier: Verizon,AT&T

Re: Auto-run on startup problem

hey i agree with peter

 

its a good link

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

Re: Auto-run on startup problem

@KushBB.  I'm not sure if you are asking me

1) why you shouldn't use Alternate Entry, or

2) why Alternate Entry would not work in this case.

 

The answer to the first question is not clear cut - there are times that Alternate Entry makes sense.  just not all the time.  But it seems some people have a built-in reaction, auto start means use Alternate Entry. 

 

The second question is easy.  In this case, the poster would have used the same code on the Alternate Entry as he was using now.  Which means it would have failed in exactly the same way.  Using an Alternate Entry does not change rules on the Event Thread. 

Please use plain text.
Developer
KushBB
Posts: 374
Registered: ‎04-07-2010
My Device: Bold 9000
My Carrier: Airtel

Re: Auto-run on startup problem

Thanks Peter...

 

Thanks for clearing my doubts, I will keep these points in mind...

 

Yesterday I posted a thread to which you replied and  suggested me something, I have posted the results... Waiting for your reply...

 

Please take a look...

 

http://supportforums.blackberry.com/t5/Java-Development/Unknown-Exception-In-Message-Thread/td-p/556...

 

Regards

Press the Like button (Thumb up) on the left side to thank and
please mark the Thread as solved if its solved...
K.S
Please use plain text.
New Developer
gtSasha
Posts: 19
Registered: ‎08-21-2009
My Device: Not Specified

Re: Auto-run on startup problem

Thank you Peter!

 

Your suggestion worked.

Please use plain text.