java-memidpmidp-2.0jsr179

SecurityException is raised when trying to use JSR-179


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?


Solution

  • 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.