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
Regular Contributor
Posts: 66
Registered: ‎08-28-2010
My Device: 9630
Accepted Solution

Start Application after application reboot

Hi, 

 

 I am developing application that support above Blackberry OS 5.0.  

 

 I have requirement that my application should auto start after blackberry reboot and should get the lat and lng using location service in back-ground.

 

 I have done auto-start option by select auto-run on start up. my application get restart after reboot but i am not able

to get the lat and lng at every 3 minute.

 

Below is my code of GPS location listner that retrive the location at every 3 minute..but it is not retriving after reboot.

 

package com.blackberry.sl;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.microedition.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;

import net.rim.device.api.gps.BlackBerryCriteria;
import net.rim.device.api.gps.BlackBerryLocation;
import net.rim.device.api.gps.BlackBerryLocationProvider;
import net.rim.device.api.gps.GPSInfo;
import net.rim.device.api.gps.GPSSettings;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;

public class GPSLocation 
{
	private BlackBerryCriteria blackBerryCriteria = null;
	private BlackBerryLocation blackBerryLocation = null;
	private static BlackBerryLocationProvider blackBerryLocationProvider = null;
	double lat = 0.0;
	double lng = 0.0;
	static double newlat = 0.0;
	static double newlng = 0.0;
	GPSThread gpsThread;
	static boolean ThreadTrue = false;
	//int timerinterval = 2*60*1000;
	int timerinterval = 180;
	public static boolean isListnerCalled = false;
	public GPSLocation()
	{
		
	}
    public GPSLocation(boolean start)
    {
		
    		MainClass.debug("called constructor");
    		blackBerryCriteria = new BlackBerryCriteria();
    		if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_CELLSITE)) 
    	    {
    	        blackBerryCriteria.setMode(GPSInfo.GPS_MODE_CELLSITE);
    	        MainClass.debug("CELL SITE");
    	    }
    	    else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_ASSIST)) 
    	    {
    	        blackBerryCriteria.setMode(GPSInfo.GPS_MODE_ASSIST);
    	        MainClass.debug("MODE ASSIT");
    	    }
    	    else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_AUTONOMOUS)) 
    	    {
    	        blackBerryCriteria.setMode(GPSInfo.GPS_MODE_AUTONOMOUS);
    	        MainClass.debug("MODE AUTONOMOUS");
    	    }
    	    else 
    	    {
    	    	MainClass.debug("else blackBerryCriteria");
    	        blackBerryCriteria.setCostAllowed(true);
    	        blackBerryCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
    	    }
    	    try 
    	    {
    	    	if(start)
    	    	{
    	    		MainClass.debug("Thread called in gpslocation");
    	    		if(blackBerryLocationProvider == null)
    	    			blackBerryLocationProvider = (BlackBerryLocationProvider) BlackBerryLocationProvider.getInstance(blackBerryCriteria);
    	    		gpsThread = new GPSThread();
    	    		gpsThread.start();
    	    	}
    	    	else
    	    	{
    	    		MainClass.debug("startListner");
    	    		startListner();
    	    	}
    	    }
    	    catch (Exception e) 
    	    {
			}
    	   
    }
   /* public GPSLocation(String s)
    {
    	gpsThread = new GPSThread();
        gpsThread.start();
    }*/
    public void stopThread()
    {
    	ThreadTrue = false;
    }
    public void startListner()
    {
      
		try 
		{
			blackBerryCriteria = new BlackBerryCriteria();
			if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_CELLSITE)) 
    	    {
    	        blackBerryCriteria.setMode(GPSInfo.GPS_MODE_CELLSITE);
    	        MainClass.debug("startListner");
    	    }
    	    else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_ASSIST)) 
    	    {
    	        blackBerryCriteria.setMode(GPSInfo.GPS_MODE_ASSIST);
    	        MainClass.debug("MODE ASSIT");
    	    }
    	    else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_AUTONOMOUS)) 
    	    {
    	        blackBerryCriteria.setMode(GPSInfo.GPS_MODE_AUTONOMOUS);
    	        MainClass.debug("MODE AUTONOMOUS");
    	    }
    	    else 
    	    {
    	    	MainClass.debug("MODE blackBerryCriteria");
    	        blackBerryCriteria.setCostAllowed(true);
    	        blackBerryCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
    	    }
			 MainClass.debug("blackBerryLocationProvider :"+blackBerryLocationProvider);
    	    if(blackBerryLocationProvider == null)
    	     {
    	    	try
    	    	{
    	    		MainClass.debug("blackberry location provider is null start new listner");
    	        	blackBerryLocationProvider = (BlackBerryLocationProvider) BlackBerryLocationProvider.getInstance(blackBerryCriteria);
                    blackBerryLocation = (BlackBerryLocation) blackBerryLocationProvider.getLocation(60);
    	        	blackBerryLocationProvider.setLocationListener(new myLocationListener(), timerinterval, -1, -1);
    	    	}
    	    	catch (Exception e)
    	    	{
					// TODO: handle exception
				}
    	        	//blackBerryLocationProvider.setLocationListener(new myLocationListener(), timerinterval, -1, -1);
    	      }
	    	  else
	    	  {
	    		  if(!isListnerCalled)
	    		  {
	    			  MainClass.debug("provider is already define call the location listner");  
	    		    isListnerCalled = true;
	    	    	blackBerryLocation = (BlackBerryLocation) blackBerryLocationProvider.getLocation(60);
    	        	blackBerryLocationProvider.setLocationListener(new myLocationListener(), timerinterval, -1, -1);
	    		  }
	    	  }
    		} 
			catch (Exception e) 
			{
				 MainClass.debug("exception"+e.getMessage());
			}
    }
    
    public double getlat()
    {
    	return lat;
    }
    public double getlng()
    {
    	return lng;
    }
   /* public void setlat(double lati)
    {
    	lat = lati;
    }
    public void setlng(double longi)
    {
    	lng = longi;
    }*/
    public static void updateUIDisaply(final boolean isNewPoint)
    {
    	UiApplication.getUiApplication().invokeLater(new Runnable() {
			
			public void run() 
			{
			MainClass.debug("lat long changed");	
		});
    }
    class GPSThread extends Thread
    {
    	public void run() 
        {
    
		    		try
		    		{
	                    blackBerryLocation = (BlackBerryLocation) blackBerryLocationProvider.getLocation(60);
	
	                    QualifiedCoordinates qualifiedCoordinates = blackBerryLocation.getQualifiedCoordinates();
	
	                    lat = qualifiedCoordinates.getLatitude();
	                    lng = qualifiedCoordinates.getLongitude();
	                    MainClass.debug("lat"+ lat);
	                    MainClass.debug("lng"+ lng);
	                    if(lat != 0.0 && lng !=0.0 && CommonInterface.writelocation)
		    				{
		                    	CommonInterface.writelocation = false;                                  //outrange       //notify = false
			    				String l_point = String.valueOf(lat)+ "$" + String.valueOf(lng) + ":" + "false" + "&" + "false";
			    				MainClass.debug("pairing latitude"+ l_point);
			    				MainClass.debug("masterdoor name"+ CommonInterface.MasterDoorname);
			    				CommonInterface.hashgpsmonitor = CommonInterface.loadhashTable(CommonInterface.gps_pointFile);
			    				if(CommonInterface.hashgpsmonitor == null)
			    					CommonInterface.hashgpsmonitor = new Hashtable();
			    				if(CommonInterface.hashgpsmonitor != null && CommonInterface.MasterDoorname != null && CommonInterface.hashgpsmonitor.containsKey(CommonInterface.MasterDoorname))
			    					CommonInterface.hashgpsmonitor.remove(CommonInterface.MasterDoorname);
			    				
			    				CommonInterface.hashgpsmonitor.put(CommonInterface.MasterDoorname.trim(),l_point);
			    				CommonInterface.writeHastTableTotextFile(CommonInterface.hashgpsmonitor, CommonInterface.gps_pointFile);
			    				gpsThread.interrupt();
			    				MainClass.debug("Thread cleared");
		    				}
	        		}
	        		catch (Exception e)
	        		{
	        			MainClass.debug("GEt not coordinates");
	    			}
	    	    	   /* edt1.setText(String.valueOf(lat));
	    				ed2.setText(String.valueOf(lng));
	    	    	    blackBerryLocationProvider.setLocationListener(new myLocationListener(), -1, -1, -1);*/
	    	      
	        	}
	        	
    }
    class myLocationListener implements LocationListener
    {
       
        public void providerStateChanged(LocationProvider provider, int newState)
        {
        	if(newState==LocationProvider.AVAILABLE);
            else
            {
                provider.reset();
                provider.setLocationListener(null,-1,-1,-1);
                provider.setLocationListener(this,-1,-1,-1);
               
            }
        }

		public void locationUpdated(LocationProvider arg0, Location location)
		{
			try
			{
				//if(location != null && location.isValid())
				MainClass.debug("Location update found");
				if(location != null)
				{
					newlat = location.getQualifiedCoordinates().getLatitude();
					newlng = location.getQualifiedCoordinates().getLongitude();
					MainClass.debug("new Location "+ newlat);
					MainClass.debug("new Location "+ newlng);
					updateUIDisaply(true);
				}
		
           }
			catch(Exception e)
			{
				MainClass.debug("E stackStrace location not updated"+ e.getMessage());
			}
		}
    }
}

  

 after reboot my application get started in background and it also call the GPSLocation Class and print below debug message. but it never called the location update.

 

 

d SecuRemote - Application main launched - 7/3 01:36:18
d SecuRemote - blackberry location provider is null start new listner - 7/3 01:36:18
d SecuRemote - blackBerryLocationProvider :null - 7/3 01:36:18
d SecuRemote - startListner - 7/3 01:36:18
d SecuRemote - startListner - 7/3 01:36:18
d SecuRemote - CELL SITE - 7/3 01:36:18
d SecuRemote - called constructor - 7/3 01:36:17
d SecuRemote - Calling GPSLocation after restart - 7/3 01:36:17

 

 

Below is my code of main class that call the GPS location after reboot.

 

package com.blackberry.sl;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.TimeZone;

import javax.microedition.location.Criteria;

import net.rim.blackberry.api.invoke.Invoke;
import net.rim.device.api.gps.GPSInfo;
import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.io.Base64InputStream;
import net.rim.device.api.io.Base64OutputStream;
import net.rim.device.api.io.LineReader;
import net.rim.device.api.io.http.HttpDateParser;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EventLogger;
import net.rim.device.api.system.GPRSInfo;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.system.RuntimeStore;
import net.rim.device.api.system.SystemListener;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.UiEngineInstance;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.util.DateTimeUtilities;

public class MainClass extends UiApplication
{

    private Thread _thread;
	//public static final long APP_ID = 0x90ff1f5da2d8c3baL;
    private static final String APP_Name = "SecuRemote";
    private static final long GUID = 0x4fa949139a9d56ccL;
	    public MainClass(boolean isGui)
	    {
	    	_thread = new Thread(new checkRequest());
	        _thread.start();
	   
	    }
	    public class checkRequest extends Thread 
	    {
	        public void run() 
	        {
	            // Creates an instance of the system’s application manager.

	            ApplicationManager myApp = ApplicationManager.getApplicationManager();

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

	            UiApplication.getUiApplication().invokeLater(new Runnable() 
	            {
	                public void run() 
	                {
	                    //pushScreen(new MyMainScreen());
	                	String ReadData = null;
	        			CommonInterface.CheckDirectory();
	        			ReadData = CommonInterface.ReadFiletoString(CommonInterface.ConfigurationFilePathfile); // read the ISSConfig file if it exists
	        			if(ReadData == null) // if ReadData is null means application is loaded first time open registration page
	        			{
	        				MainClass.debug("Application WelcomeScreen launched");
	        				EventLogger.register(GUID, APP_Name, EventLogger.VIEWER_STRING);
	        				UiApplication.getUiApplication().pushScreen(new WelcomScreen());
	        			}
	        			else if(ReadData != null) // if application is already loaded and new version is installed
	        			{
	        				CommonInterface.isFirstAccountverify = true;
	        				CommonInterface.MainScreenRunning = true;
	        				EventLogger.register(GUID, APP_Name, EventLogger.VIEWER_STRING);
	        				try {
	        					Thread.sleep(2000);
	        				} catch (InterruptedException e) {
	        					// TODO Auto-generated catch block
	        					e.printStackTrace();
	        				}
	        				
	        				try
	        				{
	        				 
		    				if(CommonInterface.hashgpsmonitor == null)
		    					CommonInterface.hashgpsmonitor = new Hashtable();
		    					CommonInterface.hashgpsmonitor = CommonInterface.loadhashTable(CommonInterface.gps_pointFile);
		    					if(CommonInterface.hashgpsmonitor != null && CommonInterface.hashgpsmonitor.size() > 0)
		    					{
		    					  MainClass.debug("Calling GPSLocation after restart");
		    					  GPSLocation gps = new GPSLocation(false);	
		    					}
	        				}
	        				catch (Exception e) 
	        				{
	        					 MainClass.debug("Calling GPSLocation throw exception");
							}
	        				MainClass.debug("Application main launched");
	        				net.rim.device.api.ui.Ui.getUiEngineInstance().setAcceptableDirections(Display.DIRECTION_NORTH);
	        				UiApplication.getUiApplication().pushScreen(new NetworkingMainScreen(true));
	        			}
	                }
	            });

	            UiApplication.getUiApplication().requestForeground();
	        }
	    }
	    public static void debug(String value)
	    {
	    // add check for EventLogger.getMinimumLevel() >= debug level
	    EventLogger.logEvent(GUID, value.getBytes(), EventLogger.DEBUG_INFO);
	    }
	public static void main(String[] args)
	 {
        MainClass theApp = new MainClass(false);
       	theApp.enterEventDispatcher();
	 }
	}

 

  I am egarly looking for help.    

 

  Can anyone tell me where am i doing wrong ????????

 

 My primary goal is my application should auto start after reboot in back ground and should call the GPSLocation class and should retrive the location. my GPSLocation is simple java class.

 

Regards,

 

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: Start Application after application reboot

I'm just working through your code, so here are some random comments as I find things.  Note that I am only working through the code that would be executed in this case, there are things that look odd in other parts of your code but since they do not relate t this problem we can ignore them.  Note that, unless in bold, these are just suggestions that I hope will help you or improve your code.  . 

 

_thread = new Thread(new checkRequest());

why not just

_thread = new checkRequest();

 

MainClass.debug("Application WelcomeScreen launched");
EventLogger.register(GUID, APP_Name, EventLogger.VIEWER_STRING);

 

Shouldn't you register your app before you start trying to debug?  I would move

EventLogger.register(GUID, APP_Name, EventLogger.VIEWER_STRING);

into your public MainClass(boolean isGui) constructor.

 

             try {
              Thread.sleep(2000);
             } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
             }

This is blocking the Event Thread.  Not sure what will happen, but worst case is the OS will barf and kill this.  Why are you doing this?

 

net.rim.device.api.ui.Ui.getUiEngineInstance().setAcceptableDirections(Display.DIRECTION_NORTH);

You might like to move this earlier in the processing too - AFAIK this does not need to run on the Event Thread. 

 

UiApplication.getUiApplication().requestForeground();

I would probably do this after I had pushed the screens - i.e. in the invokelater. 

 

You go through this processing in GPSLocation constructor:

     if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_CELLSITE))
         {
             blackBerryCriteria.setMode(GPSInfo.GPS_MODE_CELLSITE);
             MainClass.debug("CELL SITE");
         }
         else if(GPSInfo.isGPSModeAvailable(GPSInfo.GPS_MODE_ASSIST))
...

and configure your criteria for Cell Site.  Then end up calling

startListner();

which promptly throws this away....

blackBerryCriteria = new BlackBerryCriteria();

 

You are aware that this will stop your application until it times out or gets a location?

blackBerryLocation = (BlackBerryLocation) blackBerryLocationProvider.getLocation(60);

please put a debugging statement in here indicating if you got a location

 

I recommend that you handle this Exception:

// TODO: handle exception

At the very least we need to know if it ends up here.  Please put some debugging statements in here. 

This is a recommended change, not a suggestion.  Please make sure your location listener establishment is not throwing an exception. 

 

public void providerStateChanged(LocationProvider provider, int newState)

please put a debugging statement in here. 

 

Having reviewed this code, my guess is that you are attempting to use a Cell Site look up immediately after the phone has restarted.  Cell Site location requires a network connection and also requires access to the BlackBerry infrastructure to retrieve the location.  There is likely to be a delay in this at phone start up, and as a result, the statement

blackBerryLocation = (BlackBerryLocation) blackBerryLocationProvider.getLocation(60);

will throw an timeout exception, which your code does not track.  The exception will bypass the location listener set up.

 

But the additional logging I have suggested you add, should in fact point this out too.

 

Hope this helps. 

 

Regular Contributor
Posts: 66
Registered: ‎08-28-2010
My Device: 9630

Re: Start Application after application reboot

Hi,

peter

 

           you currectly understand my problem. i want same thing as you said below

 

         " Having reviewed this code, my guess is that you are attempting to use a Cell Site look up immediately after the phone has restarted.  Cell Site location requires a network connection and also requires access to the BlackBerry infrastructure to retrieve the location.  There is likely to be a delay in this at phone start up, and as a result, the statement blackBerryLocation = (BlackBerryLocation) blackBerryLocationProvider.getLocation(60);

will throw an timeout exception, which your code does not track.  The exception will bypass the location listener set up. "

 

          can you then help me how could i add delay so it add properly listner after my phone is restarted. as per my knowledge if any blackberry has large number of apps it may take long time after restart.  so i don't understand what code should i add in my application so it add location listner perfectly after phone restart and has the  "network connection and also requires access to the BlackBerry infrastructure to retrieve the location ".

 

 

 

Regards,

 

 

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: Start Application after application reboot

Before we look at this can you please add the exception reporting code and confirm this is your problem. 

 

To resolve I think you would simply remove that request.  Since you are not using the value from it anyway, I'm not sure why you are doing it anyway. 

Regular Contributor
Posts: 66
Registered: ‎08-28-2010
My Device: 9630

Re: Start Application after application reboot

Hi,

Peter

 

 finally it works... getLocation(60) was creating problem when i add listner. i understand that i dont need getlocation

when i am adding listner. so i simply comment that line and it solved my problem.

 

 Thanks for your help and pointing me the correct soultion.

 

Regards,

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: Start Application after application reboot

Great. 

 

Please review my other comments and action appropriately.