1

I want to get longitude and latitude of the phone mobile by this code:

    public void commandAction(Command com, Displayable d) {
        if (com == position)
        {
            try
            {
                Criteria cr = new Criteria();
                cr.setHorizontalAccuracy(500);
                LocationProvider lp = LocationProvider.getInstance(cr);
                // get the location, one minute timeout
                Location l = lp.getLocation(60);
                Coordinates coords = l.getQualifiedCoordinates();
                if (coords != null)
                {
                    double longitude = coords.getLongitude();
                    double latitude = coords.getLatitude();
                    String sLong = String.valueOf(longitude);
                    String sLat = String.valueOf(latitude);
                    Tlongitude.setString(sLong);
                    Tlatitude.setString(sLat);
                }
            } catch (LocationException ex) {
                Tlongitude.setString("LocationException");
                Tlatitude.setString("LocationException");
            } catch (InterruptedException ex) {
                Tlongitude.setString("InterruptedException");
                Tlatitude.setString("InterruptedException");
            }
        }
    }

The problem is that when clicking the "position" command then a system alert is shown saying : java.lang.SecurityException : Application not authorized to access the restricted API .

So what should I do?

gnat
  • 6,213
  • 108
  • 53
  • 73
pheromix
  • 18,213
  • 29
  • 88
  • 158

2 Answers2

1

You are trying to access a Location API, which a restricted API. To achieve this you must signed your mobile application with a signing certificate like Verisign,Thawte etc.

The cost of the Certificate is around 20K Indian Rupees.

You can visit my other answers here and here regarding Signing Certificate.

Community
  • 1
  • 1
Lucifer
  • 29,392
  • 25
  • 90
  • 143
1

add the relevant permission to your application and sign it with a certificate in the corresponding security domain.

The JSR179 specification defines 7 permissions under javax.microedition.location. Choose the ones you need based on what you need your code to do.

Lucifer's solution (Verisign or Thawte) will help if the location function group is in the trusted third party security domain for the phones you want to run your code on. The phone's mobile network operator or manufacturer might have decided to put location in their security domain instead, though.

https://stackoverflow.com/q/1716755 contains a brief explanation of the MIDP security model.

Community
  • 1
  • 1
michael aubert
  • 6,836
  • 1
  • 16
  • 32
  • Your link answer describes , how to stop those messages from emulator,but you not do same thing in real device – Lucifer May 25 '12 at 11:24
  • Unfortunately, java-me developers usually don't have access to the security policies of real devices. All you can hope for is some documentation from the manufacturer or mobile network operator telling you what security domain a function group belongs to (Nokia documentation is decent enough). typically, third party developers are forced to resort to trial and error to figure it out, though. – michael aubert May 26 '12 at 06:41
  • agree, but final solution is Signing Certificate only. – Lucifer May 26 '12 at 06:42