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
BlackBerry Development Advisor
Posts: 15,723
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Send info from a Blackberry device to a database on a server using Java?

Have you made use of the auto-start feature?  If so, your application needs to be signed with keys obtained from RIM before you can run it on a real BlackBerry.  You can read more and find the order form here:

 

http://na.blackberry.com/eng/developers/downloads/api.jsp

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified

Re: Send info from a Blackberry device to a database on a server using Java?

Yes I did use the auto-start feature.  I'll order a signed key soon.

 

Just out of curiosity, I disabled the auto-start feature.

On my simulator... it doesn't start automaticaly, obviously... but when I click manually on the icon it works fine (as expected).  However, on my real Blackberry, I don't even see an icon for my application... should I see it?  Or I have to have a key to do that as well?

 

Thanks!

New Developer
Posts: 5
Registered: ‎07-17-2008
My Device: Not Specified

Re: Send info from a Blackberry device to a database on a server using Java?

You won't see an icon on a real device... it runs in the background and shouldn't do any UI either. If you need to do UI you need to build a second, separate app and have the two apps (the background one and the UI one) talk to each other (global events work well for this).

 

It's usually a good idea to have the UI app check to make sure the background app is still running and if it's not, launch it. Makes it easier to recover from errors in case your background app crashes... users reset devices rarely, after all... 

Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified

Re: Send info from a Blackberry device to a database on a server using Java?

I see... thank you all!
Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified

Re: Send info from a Blackberry device to a database on a server using Java?

Sorry, one last question.

 

I did my code based on the "How To - Create a Background Application" sample.

This use a thread that sleep to send GPS coordinates to a web server.

 

Here's my code (4.2.1):

package com.canam.postGPS;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import javax.microedition.io.*;
import javax.microedition.location.*;
import java.io.*;

public final class postGPS extends Application {

    private BackGroundThread _thread;

 

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

    public postGPS() {
        _thread = new BackGroundThread(); // create a new separate thread
        _thread.start(); // start the thread
    }

    /* Thread that will run in the background */
    private class BackGroundThread extends Thread {
        private static final int TIMER = 30; // seconds
        boolean _stop = false;
        
        public synchronized void stop() {
            _stop = true;
        }

        public void run() {
            while (!_stop) {
                String url = "http://intranet11-db2.canammanac.com/IntraWeb/DB2/People.nsf/receiveFromBlackberry";
                HttpConnection con = null;
                OutputStream out = null;
                int responseCode;
                try {
                    LocationProvider lp = LocationProvider.getInstance(null);
                    if (lp != null) {
                        Location l = lp.getLocation(60);
                        Coordinates c = l.getQualifiedCoordinates(); // get GPS coordinates
                        if (c != null) {
                            String lat = Double.toString(c.getLatitude()); // latitude
                            String lng = Double.toString(c.getLongitude()); // longitude
                            String coord = lat + ";" + lng; // post data
                            con = (HttpConnection)Connector.open(url); // open URL connection
                            con.setRequestMethod(HttpConnection.POST); // POST method
                            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                            out = con.openOutputStream(); // post data as a stream
                            out.write(coord.getBytes());                           
                            responseCode = con.getResponseCode(); // push data and receive response code
                            if (responseCode != HttpConnection.HTTP_OK) {
                                System.out.println("HTTP STATUS CODE: 404"); // error
                            } else {
                                System.out.println("HTTP STATUS CODE: 200"); // successfull
                            }
                            if (con!=null) con.close(); // close the URL connection
                        }
                        sleep(TIMER*1000); // pause the thread for a defined number of minutes
                    }
                } catch (Exception e) {
                    System.err.println("An error has occurred " + e.toString());
                    System.exit(0);
                }
            }
        }
    }

    /* Stop the thread on exit */
    protected void onExit() {
        _thread.stop();
    }
}

 

I'm using a background thread with a sleep to send GPS coordinates on a regular interval.

 

However, on the gpsdemo sample, a "LocationListener" is used to achieve that.

I was wondering which way is the best (most optimized) to do this?

I am missing something by doing it the way I did?

Should I modify my code to use a "LocationListener" instead?

 

Thanks again for your help!

Highlighted
New Developer
Posts: 5
Registered: ‎07-17-2008
My Device: Not Specified

Re: Send info from a Blackberry device to a database on a server using Java?

In theory, LocationListener is great from an architectural point of view, but my experience with it is that it fails to deliver events for no apparent reason... I had to go back to polling via explicit getLocation calls. Stick with what you have.
BlackBerry Development Advisor
Posts: 15,723
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Send info from a Blackberry device to a database on a server using Java?

Please note that applications that make network connections should extend UiApplication, not Application.  The reason for this is that an Application does not support a GUI, which can prevent the firewall from prompting the user to allow your network connection that in turn prevents your application from making the connection.  You won't see this issue in the BlackBerry simulator because its firewall is disabled by default.

 

You can prevent the application from show an icon on the home screen by checking off the System Module checkbox on the Application tab of the project's properties in the BlackBerry JDE.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified

Re: Send info from a Blackberry device to a database on a server using Java?

Thanks Éric for sharing your experience with me... I'll stick with my code as you suggested.

Thanks Marc for the tips.

New Developer
Posts: 23
Registered: ‎07-28-2008
My Device: Not Specified

Re: Send info from a Blackberry device to a database on a server using Java?

Great work congrats!

 

I have been working on similar JAVA project to send GPS info from blackberry to an email address.

 

Wondering how can I modify the code to send it to an email address automatically at an intervel of 15mins while the application runs in the background.

 

Thanks in advance.

 

Regards

Developer
Posts: 191
Registered: ‎04-03-2008
My Device: Bold 9700

Re: Send info from a Blackberry device to a database on a server using Java?

Something like this should work to send an email message:

 

public static void emailMessage(String subject, String body, String emailAddress) throws AddressException, MessagingException { Store store = Session.getDefaultInstance().getStore(); Folder[] folders = store.list(Folder.SENT); Folder sentfolder = folders[0]; Message msg = new Message(sentfolder); Address toList[] = new Address[1]; toList[0]= new Address(emailAddress, null); msg.addRecipients(Message.RecipientType.TO, toList); msg.setSubject(subject); msg.setContent(body); Transport.send(msg); }

 

Just provide the subject, message body, and recipient's email addr.  If you want to do it on an interval then use a Thread or a Timer.