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
My Carrier: Virgin Mobile
Accepted Solution

Detect SDCARD on App Startup

Hi.  I have an app set to run on startup, priority 7.  The app needs to check/create a sqlite db at launch.  Seems to be a timing issue... Need to detect sdcard before creating db.  I've tried everything Ive seen online but cannot get this to work properly. Doesnt seem to be that app is in .inStartup().  What is the best way to detect an sdcard in an app set to auto-run?

 

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

Re: Detect SDCARD on App Startup

PS.  actual device with sdcard, not simulator.

 

Thanks a lot.

Please use plain text.
Developer
Iyenemi_Tyger
Posts: 132
Registered: ‎10-25-2011
My Device: Tour 9630
My Carrier: MTN

Re: Detect SDCARD on App Startup

welcome to the club i had the same issue , took me a while to figure it out thanks to peter strange , he gave me the insight i put it together and it worked like a charm try this  , please accept as solution as it works for you.

 

in your main class implement systemlistener 


import java.io.IOException;

import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.SystemListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import carniriv.db.DBConnect;
import carniriv.model.beans.Users;
import carniriv.utilities.Utilities;

/**
* This class extends the UiApplication class, providing a
* graphical user interface.
*/
public class Launcher extends UiApplication implements SystemListener
{
/**
* Entry point for application
* @param args Command line arguments (not used)
*/
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
Launcher theApp = new Launcher();
if (ApplicationManager.getApplicationManager().inStartup())
{
theApp.addSystemListener(theApp);

}
else
{
theApp.doStartupWorkLater();
}

theApp.enterEventDispatcher();
}

/**
*
* do the db copy and sd card check here 
*/
private void doStartupWork()
{
try {
if(!Utilities.isSDCardAvailable())
throw new IOException("Please insert an sd card to use app");
// check if the db file is in the sd card
if(DBConnect.isDBFileLoaded())
{
// check if the user is authenticated
String val = Users.isAuthenticated();
// nothing is returned that means he is not registered
if(val == null)
{
pushScreen(new RegisterScreen());
}
// if true take him straight to app console
else if(val.equals("true"))
{
pushScreen( new ConsoleScreen());
}
//if not any of the above take him to the login screen
else
{
pushScreen( new WelcomeScreen(true));
}
}
else
{
// copy the db file to sd card
DBConnect.copyDBRecommendedWay();
pushScreen( new WelcomeScreen());
}



} catch (Throwable e) {
// TODO Auto-generated catch block
Dialog.alert(e.getMessage());
}
}

 

/** @see SystemListener#powerUp() */
public void powerUp()
{
removeSystemListener(this);
doStartupWork();
}

private void doStartupWorkLater()
{
invokeLater(new Runnable() {
public void run() {
doStartupWork();
}
});

}

/**
* Creates a new Launcher object
*/
public Launcher()
{
// Push a screen onto the UI stack for rendering.

}

 

public void batteryGood() {
// TODO Auto-generated method stub

}


public void batteryLow() {
// TODO Auto-generated method stub

}


public void batteryStatusChange(int status) {
// TODO Auto-generated method stub

}


public void powerOff() {
// TODO Auto-generated method stub

}

 



}

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

Re: Detect SDCARD on App Startup

Thanks for the reply.  Unfortunately the app still does not see the sdcard after phone restarts and app auto runs.  Here is the code I am using...

 

package pk.MyApp;

import java.util.Enumeration;
import javax.microedition.io.file.FileSystemRegistry;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.SystemListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class MyApp extends UiApplication implements SystemListener
{
 public static void main(String[] args) {
  MyApp theApp = new MyApp();
  if (ApplicationManager.getApplicationManager().inStartup()) {
   theApp.addSystemListener(theApp);
  }
  else {
   theApp.doStartupWorkLater();
  }

  theApp.enterEventDispatcher();
 }

 private void doStartupWork() {
  try {
   // Determine if an SDCard is present
   boolean sdCardPresent = false;
   String root = null;
   Enumeration e = FileSystemRegistry.listRoots();
   while (e.hasMoreElements()) {
    root = (String)e.nextElement();
    if(root.equalsIgnoreCase("sdcard/")) {
     sdCardPresent = true;
    }
   }
   if(!sdCardPresent) {
    UiApplication.getUiApplication().invokeLater(new Runnable() {
     public void run() {
      Dialog.alert("App requires SD card. Exiting.");
      System.exit(0);
     }
    });
   }
   else { //sd card is present
    // code here to create db and tables if not already exist...

    pushScreen(new MyAppMainScreen());
   }
  } catch (Throwable e) {
   Dialog.alert(e.getMessage());
  }
 }

 public void powerUp() {
  removeSystemListener(this);
  doStartupWork();
 }

 private void doStartupWorkLater() {
  invokeLater(new Runnable() {
   public void run() {
    doStartupWork();
   }
  });
 }

 public MyApp() {
 }

 public void batteryGood() {
 }

 public void batteryLow() {
 }

 public void batteryStatusChange(int status) {
 }

 public void powerOff() {
 }

}

 

App needs to auto-run when device starts... it's just not finding the sdcard after device restart.  Fine after launching app later, just not after device starts up.

 

Any assistance would be greatly appreciated.  Thank you much.

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

Re: Detect SDCARD on App Startup

Anyone have any thoughts?  This seems to only be an issue when app set to auto-run (which it needs to be).

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

Re: Detect SDCARD on App Startup

I've even tried Thread.sleep(50000) prior to detecting the sdcard, however, still not working. Any ideas?
Please use plain text.
Trusted Contributor
KJake
Posts: 202
Registered: ‎11-21-2010
My Device: 9700
My Carrier: Virgin Mobile

Re: Detect SDCARD on App Startup

Any thoughts?  Is this just not possible?

Please use plain text.
Developer
jhw1701
Posts: 176
Registered: ‎11-06-2008
My Device: Not Specified

Re: Detect SDCARD on App Startup

Is there an actual error thrown?

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

Re: Detect SDCARD on App Startup

I didn't read the all the code... but basically you need to loop until the phone is out of the startup stage. There is a method somewhere that returns a boolean telling you if the phone is still in startup or not. I'm sure its somewhere on the forums.

Please use plain text.
Developer
jhw1701
Posts: 176
Registered: ‎11-06-2008
My Device: Not Specified

Re: Detect SDCARD on App Startup

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

 

Here's how you should check for inStartUp() status in a loop, I haven't tested this, I just merged my loop into your code 

Please use plain text.