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
Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Detect SDCARD on App Startup

Post code and I'll tell you what I see.

Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Detect SDCARD on App Startup

[ Edited ]

Thanks mreed.  I have no code that I can post anymore... been tryingto modify your demo code to get my functionality to work.  Tried everything I can think of.  Used timer to wait for x num seconds on powerup when no card detected, not working right. 

 

Or what will happen is a screen will display showing no card, then over top of it the screen will show that there is a card (after powerup)

Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Detect SDCARD on App Startup

Would it be best to put a timer in you isSDCardPresent() function?

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Detect SDCARD on App Startup

Post code and I'll tell you what I see. I'm not going to write anything new... as you should already have everything you need in this thread.

Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Detect SDCARD on App Startup

Here is what I thgought would solve the issue.  TimerTask waiting for 10 seconds if sd card not found.  Then screen based on result.

 

package mypackage;

import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.io.file.FileSystemListener;
import javax.microedition.io.file.FileSystemRegistry;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.SystemListener2;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class MyApp extends UiApplication implements SystemListener2, FileSystemListener {

    private MainScreen mainScreen;

    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() {

        mainScreen = new MainScreen();

        ApplicationManager applicationManager = ApplicationManager.getApplicationManager();
        boolean inStartup = applicationManager.inStartup();

        mainScreen.add( new LabelField( "inStartup[" + inStartup + "]" ) );

        if ( !inStartup ) {
            mainScreen.add( new LabelField( "SDCard[" + isSDCardPresent() + "]" ) );
            mainScreen.add( new LabelField( "App launch complete" ) );
        }
        else {
            addSystemListener( this );
            FileSystemRegistry.addFileSystemListener( this );
        }

        if (isSDCardPresent()) {
        	pushScreen( mainScreen );
        }
        else {
        	Timer t = new Timer();
        	TimerTask tt = new TimerTask() 
        	{
        	  public void run() {
        		  if (isSDCardPresent()) {
        	        	pushScreen( mainScreen );
        	        }
        		  else {
        			  pushScreen(new NoCardScreen());
        		  }
        	  }
        	};
        	t.schedule(tt, 10000);
        }
    }

    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 );

        synchronized (UiApplication.getEventLock()) {
            mainScreen.add( new LabelField( "Power up complete. SDCard[" + isSDCardPresent() + "]" ) );
            mainScreen.add( new LabelField( "SystemListener2 removed" ) );
        }
    }

    public void rootChanged( int state, String rootName ) {

        boolean sdCardPresent = isSDCardPresent();

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

        synchronized (UiApplication.getEventLock()) {
            mainScreen.add( new LabelField( "Root changed. SDCard[" + sdCardPresent + "]" ) );
            if ( sdCardPresent ) {
                mainScreen.add( new LabelField( "FileSystemListener removed" ) );
            }
        }
    }

    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 ) {    }
}

 

 This does not work, just hangs.

Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Detect SDCARD on App Startup

Changed timer code to

 

Timer t = new Timer();
			TimerTask tt = new TimerTask() 
			{
				public void run() {
					UiApplication.getUiApplication().invokeLater(new Runnable() {
						public void run() {
							if (isSDCardPresent()) {
								pushScreen( mainScreen );
							}
							else {
								pushScreen(new NoCardScreen());
							}
						}
					} );
				}
			};
			t.schedule(tt, 10000);

 Now app runs after device restart but only shows no ad card screen.  Like it didnt detect card in time maybe?

Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Detect SDCARD on App Startup

Should this not just work?  Look for sdcard, if not found wait for some seconds then try again.  If not found push nosdcard screen, otherwise push mainscreen.,

 

package mypackage;

import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.io.file.FileSystemRegistry;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class MyApp extends UiApplication {

	private MainScreen mainScreen;

	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() {

		mainScreen = new MainScreen();
		mainScreen.add( new LabelField( "mainscreen" ) );

		if (isSDCardPresent()) {
			pushScreen( mainScreen );
		}
		else {
			Timer t = new Timer();
			TimerTask tt = new TimerTask() 
			{
				public void run() {
					UiApplication.getUiApplication().invokeLater(new Runnable() {
						public void run() {
							if (isSDCardPresent()) {
								pushScreen( mainScreen );
							}
							else {
								pushScreen(new NoCardScreen());
							}
						}
					} );
				}
			};
			t.schedule(tt, 20000);
		}
	}

	private boolean isSDCardPresent() {

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

 

 

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Detect SDCARD on App Startup

This is going to be my last post on this thread, then I'm unsubscribing because you're not listening. In a previous reply I said:

 

I would use a TimerTask in powerUp() rather than sleep. From my previous test it was about 10 seconds after powerUp() before it mounted.

 

10 seconds after powerUp(), not 10 seconds after the app auto-starts. And it was just 10 seconds that one time I happened to count, its not a hard number.

Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Detect SDCARD on App Startup

Thank mreed.  Sorry, Not sure what you mean.  Which sleep?

Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Detect SDCARD on App Startup

Sorry mreed.  Just trying to get this figured out and havng difficult time. Tried a lot of things and cant get it going. I got that youre trying and I'm, not seeming to figure this out.  Sorry, please unsubscribe, forget this thread.  I appreciate your help though, really do.  I know you have a much better understanding of how this should work.  Thanks for all of your assistance, very much appreciated it.