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
New Contributor
Kardon
Posts: 9
Registered: ‎11-20-2013
My Device: 8830 WE
Accepted Solution

Application keeps launching during startup of device, also need advice for implementing network methods

Hi all,

 

I'm new to Blackberry development and I'm currently writting a fairly simple application to run on 7.1.

 

So far development has been interesting, I've managed to create my UI with the help of resources online (mainly this forum), but today I started trying to implement some of the networking classes that I'll need and now I've just run into constant issues.

 

I'm using Eclipse SDK 3.7.2 (the one that comes bundled with the RIM plugin)

 

First issue:

 

I'm not sure what I changed, but today I've noticed that every time I start the simulator (using 9360 sim) my application gets started during the intial startup process. I don't think it was doing this before, but to be honest I wasn't paying much attention. Reason I think it's starting during the device startup is because I've set a breakpoint in the constructor for my main screen class, which gets hit before the simulator even gets to the point where it's ready for user interaction. Also, once it is ready, if I hold the BB button down, I see my application listed.

 

I don't have it set to auto launch in the Application descriptior .xml.

 

I started this application from the HelloBlackBerry sample, here is my main method for application entry:

 

public static void main( String[] args ) {
        appSettings = AppSettings.fetch();
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        HelloBlackBerry theApp = new HelloBlackBerry();
        theApp.enterEventDispatcher();
        //openConnection();
        
       
    }


 As far as I know, this is the only entry point for the application.

 

More code

    public HelloBlackBerry() {
        // Push a screen onto the UI stack for rendering.
    	mainScreen = new ScreenHelloBlackBerry();
        pushScreen( mainScreen );
    }

 

 

Here is the .fetch() method from my AppSettings class

    // Retrieves a copy of the effective properties set from storage.
    public static AppSettings fetch()
    {
        AppSettings savedSettings = (AppSettings) _store.getContents();
        return new AppSettings(savedSettings);
    }

 Some of this code I didn't write myself, could the persistence store cause my auto launching? 

 

I've tried resetting the simulator to factory settings, no change.

 

Are all applications initialized during the device startup and then closed shortly after?

 

Part 2: Need advice for implementing network functions into application

 

I need to implement some methods which will retrieve data from a web service, and I'll also need to send data to this service. From what I've read, the ConnectionFactory is the way to go. I want my application to make a request when the app is opened, and then check back on a set interval. I wasn't sure exactly how to go about adding another thread for networking, I understand the network connections should not be created on the main event thread, so I tried doing this:

 

In the constructor for my class which implements MainScreen (ScreenHelloBlackBerry), I use the following to create a modal dialog to prompt the user for a username, I also try spawning the networking thread in the same segment:

 

        public ScreenHelloBlackBerry(){
...initialize fields and managers, add it all together...then near the end


//This will open the confirm dialog when the application is launched UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { openConfirmDialog(); //start connection - Possibly broken ConnectionThread ct = new ConnectionThread(); ct.start(); } });

 

I'm not sure if this is an OK way to do this, but it seems to work well for what I need. I want a user who is launching the application to get prompted, and if a user switches applications, when they come back they don't get asked again (unless they close the app, and reopen it).

 

Here is my ConnectionThread:

 

public class ConnectionThread extends Thread
{
	
	
    public void run()
    {
         
          ConnectionFactory connFact = new ConnectionFactory();
          ConnectionDescriptor connDesc;
          connDesc = connFact.getConnection("http://www.google.com");
          if (connDesc != null)
          {
              HttpConnection httpConn;
              httpConn = (HttpConnection)connDesc.getConnection();
              try
              {
                  final int iResponseCode = httpConn.getResponseCode();
                  UiApplication.getUiApplication().invokeLater(new Runnable()
                  {
                      public void run()
                      {
                          Dialog.alert("Response code: " + 
                                        Integer.toString(iResponseCode));
                          
                      }
                   });
                  httpConn.close();
                  
                  
               } 
               catch (IOException e) 
               {
                 System.err.println("Caught IOException: " 
                      + e.getMessage());
               }
          }
    }
}   

 Right now it's basically unmodified code from a sample I found, I'll be retooling it to fit my needs once I can get it working. I HAD this working to the point where after the user closed the confirm dialog, moments later a an alert dialog would appear with the response code of 302. I don't know what changed, but now it doesn't work at all, and if I try to step through with the debugger, I can't get past the httpCon.getResponseCode(). I saw some errors earlier about "No BBAuth service records configured", and I also got a timeout exception once.

 

I'm pretty sure I'm doing this wrong, but at the same time like I said it WAS working, and now it isn't. If anybody has made it this far, thanks so much for your time and any advice given is highly appreciated.

 

Also, should I be trying to start the network thread from the UIApplication class instead of MainScreen? Currently there is very little going on in the class which extends UIApplication, should I move most of my business logic out of the screen classes and into the UIApplication class?

 

Thanks in advance!

 

 

 

 

 

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

Re: Application keeps launching during startup of device, also need advice for implementing network methods

In future, I recommend that you ask different questions on different Threads.

 

!) Autorun on startup.  I am not sure there is any other way of this happening other than checking the

Auto-run on startup

check box which you see when editing the

BlackBerry_App_Descriptor.xml

Please confirm that you do not have this checked.

 

2) Your networking code looks OK, you use an invokeLater where you don't need one but hat will not effect it.  The 302 was the Http response code, which is a redirect - in other words it was sending you to a different web page.  Presumably in this case it was sending you to the mobile Google page.  Perhaps the reason it no longer works is that you are not starting the MDS Simulator?

 

If the http code blocks on getting the response code, it will not block there forever. It will block for 2 minutes and then time out - in which case it will probably go through your Exception handler with an IOException.  Watch out for that.

New Contributor
Kardon
Posts: 9
Registered: ‎11-20-2013
My Device: 8830 WE

Re: Application keeps launching during startup of device, also need advice for implementing network methods

Hi Peter, 

 

Thanks for your help, note taken, will try to seperate different topics to different threads in the future, thanks.

 

Like I mentioned, I've double checked the auto-run checkbox, I've even tried turning it on, and then back off, it didn't seem to have any effect.

 

I suppose the invokeLater which I don't need would be the one located in the ConnectionThread class? A response from the server was all I was looking for, the specific http code returned wasn't really significant. 

 

I haven't looked to check to see if the MDS simulator is running or not, I'll have to check that tomorrow. I assume it's located in the simulator window?

 

Does it matter where I spawn the thread for the networking? Currently it's in the class extending MainScreen, should or should I spawn in from the class extending UIApplication?

 

I'm still unsure if I'm structuring the application correctly - If you were to build an application which ran from primarily one screen and obtains info from an online resource, how would you pass data from the networking thread to the screen? Should it be managed within UIApplication? There will also be some logic methods which will need to process the incoming data before it gets passed to the screen. Should class extending UIApplication contain the references to these backend classes? If I think about the way I've got it setup now, the class extending UIApplication holds a reference to the MainScreen and AppSettings not much else. The class extending MainScreen holds the reference to the networking thread. 

 

Many thanks,

 

New Contributor
Kardon
Posts: 9
Registered: ‎11-20-2013
My Device: 8830 WE

Re: Application keeps launching during startup of device, also need advice for implementing network methods

Not sure if there is something wrong with my installation, or this is expected behaviour, but I just tried creating a new  HelloBlackBerry project in eclipse, I set a breakpoint in the main method and sure enough just like my other application, this breakpoint gets hit while the simulator is turning on, what gives? Just like my other app, once the sim is able to respond to user input, pushing and holding the BB button shows the HelloBlackBerry app as running in the background.

 

Unless something has gone haywire with my config, every app I've tried seems to do this, even the basic blank screen app. Any advice? 

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

Re: Application keeps launching during startup of device, also need advice for implementing network methods

Interesting.  No ideas sorry.  I was a bit stuck myself too. 

 

Can you paste the 'text' of the BlackBerry ....xml file in here.  just interested to see what it has.

 

Then I would reinstall.....

New Contributor
Kardon
Posts: 9
Registered: ‎11-20-2013
My Device: 8830 WE

Re: Application keeps launching during startup of device, also need advice for implementing network methods

[ Edited ]

Hi Peter, okay if this is abnormal I'll try a reinstall in abit. Here is a copy of the app descriptor

<!-- This file has been generated by the BlackBerry Plugin for Eclipse v3.7.101. -->
<Properties ModelVersion="1.1.2">
<General Title="xxxxx" Version="1.0.0" Vendor="yyyyy" Description=""/>
<Application Type="BlackBerry Application" MainMIDletName="" MainArgs="" HomeScreenPosition="0" StartupTier="7" IsSystemModule="false" IsAutostartup="false"/>
<Resources hasTitleResource="false" TitleResourceBundleName="" TitleResourceBundleRelativePath="" TitleResourceBundleClassName="" TitleResourceBundleKey="" DescriptionId="">
<Icons>
<Icon CanonicalFileName="res\img\icon.png" IsFocus="false"/>
</Icons>
</Resources>
<KeywordResources KeywordResourceBundleName="" KeywordResourceBundleRelativePath="" KeywordResourceBundleClassName="" KeywordResourceBundleKey=""/>
<Compile OutputCompilerMessages="false" ConvertImages="true" CreateWarningForNoExportedRoutine="true" CompressResources="false" AliasList="">
<PreprocessorDefines/>
</Compile>
<Packaging PreBuildStep="" PostBuildStep="" CleanStep="" OutputFileName="MyFirstBBapp" OutputFolder="deliverables" GenerateALXFile="true">
<AlxFiles/>
</Packaging>
<HiddenProperties>
<ClassProtection/>
<PackageProtection/>
</HiddenProperties>
<AlternateEntryPoints/>
</Properties>

 Also, like you mentioned earlier, my call to getResponseCode() is timing out, 

java.io.InterruptedIOException: Local connection timed out after ~ 120000

 Where might one 'turn on the MDS simulator' ? I see now I don't even have it installed, nvm. Odd that it was working earlier though..

New Contributor
Kardon
Posts: 9
Registered: ‎11-20-2013
My Device: 8830 WE

Re: Application keeps launching during startup of device, also need advice for implementing network methods

[ Edited ]

Peter,

 

Apologees for the double post, but I believe I found out what caused my networking to stop working.

 

I said that I had gotten the response 302 previously, but later I was unable to get a response at all. This is what happened:

 

When I was able to get the response 302, it was after I had connected to the default wifi network on the simulator. When this is enabled, the call to getResponseCode() works great. I didn't realize that this is what got it working at the time.

 

Then I noticed the fact that my app was launching during device startup (I think it's been doing it the whole time), so that distracted me into finding a way to stop this behaviour, one thing I tried was reseting the phone settings to factory, cleaning the simulator, etc. It was this resetting of the network connections which broke the code which was working. 

 

Long story short, I'm back to where I was yesterday, but now I know why. (sorta)

 

 

 

Edit:

 

I'm wondering if the fact I'm targetting JRE 7.1 has anything to do with this? I downloaded the MDS sim and started it, nothing seemed to happen. I then found the option in eclipse to start the MDS sim with the device sim, still didn't really change anything. I ended up putting this bit of code in:

          if (DeviceInfo.isSimulator()) {
        	    url += ";deviceside=true";
        	} 

 and now things are working with or without WiFi turned on.

 

I read a post on StackOverflow which makes me wonder if there are some preexisting compatability issues with JRE 7 and MDS sim: http://stackoverflow.com/questions/9339656/send-json-request-from-blackberry-application

User "MrVincenzo" answered the topic, and if you expand the comments on his answer you see he asks if the person was using JRE 7. Do you know if there are issues with MDS on JRE 7?

New Contributor
Kardon
Posts: 9
Registered: ‎11-20-2013
My Device: 8830 WE

Re: Application keeps launching during startup of device, also need advice for implementing network methods

Hi all,

 

Just to conclude this topic for those who may be interested, I found an excellent guide for implementing networking into BB applications, Click Here for a link.

 

Regarding my application being started during the startup of the device simulator, I have found that on an actual device this behaviour is not observed, and also, the behaviour seemed to stop after uninstalling and reinstalling my simulator. 

 

Cheers,

 

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

Re: Application keeps launching during startup of device, also need advice for implementing network methods

Thanks for letting us know.

 

Perhaps you could mark your previous post as the solution to this Thread?