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
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Detect SDCARD on App Startup

[ Edited ]

Curious why this code doesnt function as it loks like it should...

 

public static void main(String[] args)
{
  Launcher theApp = new Launcher();
  if(ApplicationManager.getApplicationManager().inStartUp())
  {
    theApp.invokeLater(new Runnable() {
      public void run()
      {
        ApplicationManager appManager = ApplicationManager.getApplicationManager();
        while(appManager.inStartUp())
        {
          //still booting, sleep some
          try
          {
            Thread.sleep(1000);
          }
          catch (Throwable error)
          {
            // nothing
          }
        }
       
        try
        {
           boolean sdCardPresent = false;
           String root = null;
           Enumeration e = FileSystemRegistry.listRoots();
           while (e.hasMoreElements() && !sdCardPresent ) {
              root = (String)e.nextElement();
              if(root.equalsIgnoreCase("sdcard/")) {
                sdCardPresent = true;
              }
           }
           if(sdCardPresent) {
             //create your DB here or do whatever else SD card initialization you need
           }
        }
        catch(Throwable error)
        {
          //log the error
        }

        System.exit(0);
      }
    });
  }
 
  theApp.enterEventDispatcher();
}

 

 

 

Still doesnt detect sdcard after device restart.  Looks like it should.

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

Re: Detect SDCARD on App Startup

invokeLater runs on the event thread. You're blocking it, so nothing else is going to run now. Use a new Thread.

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

Re: Detect SDCARD on App Startup

[ Edited ]

Thanks mreed.  Do you mean using UiApplication.getUiApplication().invokeLater() insdead of  theApp.invokeLater?

 

Or something like this...

 

	public static void main(String[] args) {
		MyApp theApp = new MyApp();
		if(ApplicationManager.getApplicationManager().inStartup())
		{
			new Thread() {
				public void run(){
					ApplicationManager appManager = ApplicationManager.getApplicationManager();
					while(appManager.inStartup())
					{
						//still booting, sleep some
						try
						{
							Thread.sleep(1000);
						}
						catch (Throwable error)
						{
							// nothing
						}
					}
				}
			}.start();

			try
			{
				boolean sdCardPresent = false;
				String root = null;
				Enumeration e = FileSystemRegistry.listRoots();
				while (e.hasMoreElements() && !sdCardPresent ) {
					root = (String)e.nextElement();
					if(root.equalsIgnoreCase("sdcard/")) {
						sdCardPresent = true;
					}
				}
				if(sdCardPresent) {
					//create your DB here or do whatever else SD card initialization you need
				}
			}
			catch(Throwable error)
			{
				//log the error
			}

			System.exit(0);
		}

		theApp.enterEventDispatcher();
	}

 

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

Re: Detect SDCARD on App Startup

new Thread()

 

But you just moved all your stuff after the while out of the thread.... so its not going to wait anymore.

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

Re: Detect SDCARD on App Startup

Thanks for the reply.  Not sure I completely understand, looked correct.  Do you mean something like this...

 

 public static void main(String[] args) {
  MyApp theApp = new MyApp();
  if(ApplicationManager.getApplicationManager().inStartup())
  {
   new Thread() {
    public void run(){
     ApplicationManager appManager = ApplicationManager.getApplicationManager();
     while(appManager.inStartup())
     {
      //still booting, sleep some
      try
      {
       Thread.sleep(1000);
      }
      catch (Throwable error)
      {
       // nothing
      }
     }
     
     try
     {
      boolean sdCardPresent = false;
      String root = null;
      Enumeration e = FileSystemRegistry.listRoots();
      while (e.hasMoreElements() && !sdCardPresent ) {
       root = (String)e.nextElement();
       if(root.equalsIgnoreCase("sdcard/")) {
        sdCardPresent = true;
       }
      }
      if(sdCardPresent) {
       //create your DB here or do whatever else SD card initialization you need
      }
      else {
       System.exit(0);
      }
     }
     catch(Throwable error)
     {
      //log the error
     }
     
    }
   }.start();
  }
  theApp.enterEventDispatcher();
 }

 

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

Re: Detect SDCARD on App Startup

[ Edited ]

Ok, took a different approach but still the app returns "no ad card" after device restarts...

 

	private Thread _thread;

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

	public MyApp() {
		_thread = new Thread(new checkRequest());
        _thread.start();
	}

	
	public class checkRequest extends Thread  {
        public void run()  {

            ApplicationManager myApp = ApplicationManager.getApplicationManager();

            while(myApp.inStartup()) 
            {
                try 
                {
                    Thread.sleep(2000);
                } catch(Exception e) {
                    // Catch Exception
                }
            }

            UiApplication.getUiApplication().invokeLater(new Runnable() 
            {
                public void run() 
                {
			pushScreen(new MyAppMainScreen());
                }
            });

        }
    }

 

 And in MyAppMainScreen constructor I check for sd card...

 

public MyAppMainScreen() 
{
boolean sdCardPresent = false; String root = null; Enumeration e = FileSystemRegistry.listRoots(); while (e.hasMoreElements() && !sdCardPresent ) { root = (String)e.nextElement(); if(root.equalsIgnoreCase("sdcard/")) { sdCardPresent = true; } } if(!sdCardPresent) { Dialog.alert("no sd card"); //System.exit(0); } }

 

SD CArd detected fine if app launched via click, just not after device startup. 

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

Re: Detect SDCARD on App Startup

[ Edited ]

Yea... and it does what? Work? Does it get past the loop? What file system roots are available when it gets there? Is it crashing? Does it get to System.exit() or your db block? Do you actually log the exception, or is it just that comment saying to log?

 

EDIT: slowing posting on my part.

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

Re: Detect SDCARD on App Startup

[ Edited ]

Thanks mreed. With my new approach _thread = new Thread(new checkRequest()); the app runs fine after launching it manually but returns "no sd card" dialog after device restarts.  Seems like inStartup() does not mean that sd card is mounted.

 

In my new approach the app only gets to dialog.alert("no sd card") after device restarts but runs fine if launched manually. 

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ
Trusted Contributor
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Detect SDCARD on App Startup

[ Edited ]

I did implement FileSystemListener but still not detecting sd card after device restart

 

publicvoid rootChanged(int state, String rootName) {

if( state == ROOT_ADDED ) {

           

if( rootName.equalsIgnoreCase("sdcard/") ) {

           

sdCardPresent = true;

            }

        }

}

 

http://supportforums.blackberry.com/t5/Java-Development/Programmatically-determine-if-a-microSD-card... 

 

 

I've tried many, many things but cannot get app to see that sd card is available after device restarts.  App set to auto run on startup.  App sees sd card no problem when clicked but will not see card after device restart until clicked on.  Any ideas?