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
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

Thank you very much, mreed.  You'r sample is working great.  Really appreciate your time and assistance.

 

The app I am building must run on device startup and must use an sd card.  So the requirement is that when the app is lauinched (after device startup or manual launch) it must see an sd card.  If not, present user with screen telling them no card found, then exit.

 

I guess I'm going to need a timer or something like that to keep looking for the sc card for a certain lengh of time then tell the user when not found.  Filesystemlistener is working great in your sample but for my purpose wont ever get reached because sd card not available after app launched (after device startup).  Do you think a timer would be the best approach in this case?

 

Really appreciate all of your help on this frustrating issue, mreed.

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Detect SDCARD on App Startup

If you launch the app and its not found, its not found. The listener will tell you when its found, you don't need to keep checking with a timer. If you're having an issue because you're trying to push a screen at auto-start, but the sdcard isn't ready.... then don't push the screen at auto-start, and just let the user open the app. I'm not sure what you app is for, but trying to force a screen into the foreground at device startup sounds like it would be a bad experience. If you need to do some work at auto-start, you should be using multiple entry points (search or make another thread for that). If for some reason you do have that requirement to make a screen visible at auto-start, then yea, i guess you could use a timer and if the listener doesn't get hit within the specific time, display your screen, otherwise cancel the timer once the listener is hit. i highly recommend against pushing the screen at device auto-start though.

 

If that last post with the attachment/code works for you, feel free to mark it as the solution.

Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

Thank you very much mreed. Really appreciat it. My app must run at startup and will be set to continually run (user cannot close app). Corporate app. Problem is, app requires an sd card to log various events etc. Really need to alert the user after device restart is sd card not dound and app exiting.
Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

Would it be best to create a tinmer for say 5 seconds in the screen constructor to determine if sdcard is present then raise an alert/close app if not?
Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

Hi mreed.  I've really been tryingto get this to work but am not successful at all.  App keep locking up.  After the device restarts I need to have the app auto run, detect if sd card is available/wait for filesystemlistener if card not mounted yet.  Then push a not founf screen if sd card not available and a founf screen if found.  Basically just need to pause for a few seconds when app starts/device restarts to let the device mount sd card.  Any thoughts?  I just cant get this to work with thread.sleep or wait or now thread, etc.

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Detect SDCARD on App Startup

[ Edited ]

Where r u pausing? Post code? I would use a TimerTask in powerUp() rather than sleep. From my previous test it was about 10 seconds after powerUp() before it mounted.

Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

Thanks mreed. I will have to put some non working code together again. Tried so many different things that now I havew nothing of any true value. Thank. S you for all the help.
Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

Okay.  Added code to powerup() which pushes screen if card found/not found but app wont launch again after closed and clicked on.

 

public class MyApp extends UiApplication implements SystemListener2, FileSystemListener {

    public static void main( String[] args ) {
        final MyApp theApp = new MyApp();
        theApp.invokeLater( new Runnable() {

            public void run() {
                theApp.bootloader();
            }
        } );
        theApp.enterEventDispatcher();
    }

    private void bootloader() {
        ApplicationManager applicationManager = ApplicationManager.getApplicationManager();
        boolean inStartup = applicationManager.inStartup();

        if ( inStartup ) {
            addSystemListener( this );
            FileSystemRegistry.addFileSystemListener( this );
        }
    }

    private boolean isSDCardPresent() {
        Enumeration roots = FileSystemRegistry.listRoots();
        while (roots.hasMoreElements()) {
            String root = (String) roots.nextElement();
            if ( root.equals( "SDCard/" ) ) {
                return true;
            }
        }
        return false;
    }

    public void powerUp() {
        removeSystemListener( this );
        
        if (!isSDCardPresent()) {
        	try {
				Thread.sleep(10000);
				if (!isSDCardPresent()) {
					pushScreen(new MyAppNoSDCardScreen());
				}
				else {
					pushScreen(new MyAppMainScreen());
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }

    public void rootChanged( int state, String rootName ) {

        boolean sdCardPresent = isSDCardPresent();

        if ( sdCardPresent ) {
            FileSystemRegistry.removeFileSystemListener( this );
        }
    }

    public void usbConnectionStateChange( int state ) {}
    public void batteryGood() {}
    public void batteryLow() {}
    public void batteryStatusChange( int status ) {}
    public void powerOff() {}
    public void backlightStateChange( boolean on ) {}
    public void cradleMismatch( boolean mismatch ) {}
    public void fastReset() {}
    public void powerOffRequested( int reason ) {}

}

 

Will need to add code in constructor?  Seems the sleep() works but is this the recommended approach?

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Detect SDCARD on App Startup

You should really  go back and reread all the replies. You'll probably pick up something you missed before, or something that didn't make sense that might now be more clear. Everything you need has already been posted.

 

Seems the sleep() works but is this the recommended approach?

No.. its not recommended approach. The recommended approach is what was recommended in my last reply :smileyhappy: ...If you're going down this path, TimerTask is the best tool for you. Plus, your sleep isn't even in a new Thread... so you're probably stalling the system startup for 10 seconds depending on who is calling your listener.

 

but app wont launch again after closed and clicked on.

That is because you're not pushing anything when you launch the app from the ribbon. The inStartup will be false in this case. See the code I attached in a previous reply.

Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700

Re: Detect SDCARD on App Startup

Thanks a lot mreed.  Really appreciate your efforts.  I just cant get it to work as expected.  My app has to run on startup and whehn it starts it needs to pause a few moments (around 10 seconds) and check or an sdcard.  After instartup and pause for sdcard to mount the app simply needs to alert user that no card there, then exit.  I just cannot get this to function no matter what I try.  Do you have a code example I can reference?  I cannot get it to go as needed... app just doesnt see the sdcard after device startup.  No issue with detecting card when app opened from ribbon, just not at device startup.  Would you happen to have code exmaple showing this/working for you?

Please use plain text.