09-27-2012 02:22 PM
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?
Solved! Go to Solution.
09-27-2012 02:24 PM
PS. actual device with sdcard, not simulator.
Thanks a lot.
09-27-2012 06:16 PM
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().inStar
{
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
}
}
09-28-2012 08:39 AM
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().inStar
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(n
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.
09-28-2012 06:14 PM
Anyone have any thoughts? This seems to only be an issue when app set to auto-run (which it needs to be).
09-29-2012 10:02 AM
10-01-2012 07:20 PM
Any thoughts? Is this just not possible?
10-02-2012 09:07 PM
Is there an actual error thrown?
10-02-2012 09:11 PM
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.
10-02-2012 09:53 PM
public static void main(String[] args)
{
Launcher theApp = new Launcher();
if(ApplicationManager.getApplicationManager().inSt artUp())
{
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