openGeeksLab / codenameone

Automatically exported from code.google.com/p/codenameone
0 stars 0 forks source link

setLocationListener crashes when location services are turned off #458

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

this is an issue according to email sent by nickkoirala. (I have the same 
problem)

=======================

On Android and iPhone if I setLocationListener when the status is anything 
other than LocationManager.AVAILABLE then it'll crash (Application closes 
unexpectedly provider==null and exception that doesn't seem to caught by try / 
catch around setLocationManager ). 

The problem is that at least one of the Android devices we have reports 
LocationManager.TEMPORARILY_UNAVAILABLE for both GPS disabled and current 
position not found.

This means that if it can't get an immediate position we can't safely set a 
listener to get current position (because if the position is unavailable due to 
location services being turned off in the phone settings the app will quit).

This is on a Huawei IDEOS X3 but has been reported to me by a number of testers 
on different phones.  On my Samsung Galaxy Nexus it allows me to set a listener 
without crashing when location services are turned off - but from 10 - 20 
testers it seems to be the exception rather than the rule, most phones are 
crashing if the LocationListener is set with locations services turned off. 

            if (LocationManager.getLocationManager().getStatus() != LocationManager.TEMPORARILY_UNAVAILABLE) {
                try {
                LocationManager.getLocationManager().setLocationListener(new LocationListener() {
                    @Override
                    public void locationUpdated(final Location location) {
                        Log.p("Location Updated");
                    }

                    @Override
                    public void providerStateChanged(int newState) {
                        Log.p("Location State Changed");
                    }
                });
                }catch(Exception e) {
                    Log.e(e);
                }
            }

    If I don't check for LocationManager.TEMPORARILY_UNAVAILABLE then it'll crash when GPS is turned off in phone settings
    I would've thought LocationManager.OUT_OF_SERVICE would be a good fit here, but the phone never reports that status.
    The phone seems to report TEMPORARILY_UNAVAILABLE for both GPS turned off and when GPS is turned on but a position is not available yet (e.g., GPS only location services and phone has been powered off so there is no last known location).
    If I do check status as above then the listener won't be set if locations services are on but not currently available.
    The exception that is thrown and closes the app is not caught by the code above - that would otherwise be my next approach, attempt to set the listener and catch the exception.

What am I doing wrong here?  I have written native Android apps that use 
location services and they don't have this problem.

Regards,

Original issue reported on code.google.com by rashydos on 31 Dec 2012 at 9:37

GoogleCodeExporter commented 9 years ago
Assigning to Chen for evaluation.

Original comment by shai.almog on 31 Dec 2012 at 10:22

GoogleCodeExporter commented 9 years ago
can you provide a stack trace from the ddms?

Original comment by cf27...@gmail.com on 1 Jan 2013 at 7:56

GoogleCodeExporter commented 9 years ago
I have tested it on Android emulator and it crash. I don't really know how can 
I get those traces.

Original comment by rashydos on 1 Jan 2013 at 10:31

GoogleCodeExporter commented 9 years ago
under the Android emulator directory run the ddms
on my machine it is located here:
C:\Program Files (x86)\Android\android-sdk\tools\ddms.bat

once you run the emulator (select the emulator node on the top left corner in 
the ddms)you will see the output and exception to the ddms log.

Original comment by cf27...@gmail.com on 2 Jan 2013 at 10:18

GoogleCodeExporter commented 9 years ago
Here you are... Thanks for the info.

The bestProvider  is null :)
=======================================================================

01-02 10:23:49.081: I/System.out(811): bestProvider null
01-02 10:23:49.081: D/AndroidRuntime(811): Shutting down VM
01-02 10:23:49.081: W/dalvikvm(811): threadid=1: thread exiting with uncaught 
exception (group=0x40a13300)
01-02 10:23:49.091: E/AndroidRuntime(811): FATAL EXCEPTION: main
01-02 10:23:49.091: E/AndroidRuntime(811): java.lang.IllegalArgumentException: 
provider==null
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
android.location.LocationManager.requestLocationUpdates(LocationManager.java:477
)
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
com.codename1.impl.android.d$1.run(Unknown Source)
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
android.os.Handler.handleCallback(Handler.java:615)
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
android.os.Handler.dispatchMessage(Handler.java:92)
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
android.os.Looper.loop(Looper.java:137)
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
android.app.ActivityThread.main(ActivityThread.java:4745)
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
java.lang.reflect.Method.invokeNative(Native Method)
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
java.lang.reflect.Method.invoke(Method.java:511)
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 10:23:49.091: E/AndroidRuntime(811):  at 
dalvik.system.NativeStart.main(Native Method)
01-02 10:23:49.121: I/System.out(811): showKeyboard false
01-02 10:23:49.131: W/ActivityManager(151):   Force finishing activity 
ma.gpsweb/.GPSWebMarocStub

the app is on http://m.gpsweb.ma

Original comment by rashydos on 2 Jan 2013 at 10:28

GoogleCodeExporter commented 9 years ago
I think the problem is on bindListener() that didn't check provider before 
requesting location update

    public void bindListener() {
        Handler mHandler = new Handler(Looper.getMainLooper());
        mHandler.post(new Runnable() {

            public void run() {
                locationManager.requestLocationUpdates(bestProvider, 0, 0, AndroidLocationManager.this);
            }
        });
    }

Original comment by rashydos on 2 Jan 2013 at 10:38

GoogleCodeExporter commented 9 years ago
the issue occurs because the device is not able the get a location from the gps 
and the network.
usually if the gps is off we try to get it from the network, but it is possible 
the network is also not ready or enables, anyway I'll to patch this, in any 
case it should not crash.

Original comment by cf27...@gmail.com on 2 Jan 2013 at 10:49

GoogleCodeExporter commented 9 years ago
Also LocationManager.getLocationManager() should return null if provider is 
null 
or 
getStatus() should return out of service in this case.

Thanks.

Original comment by rashydos on 2 Jan 2013 at 10:50

GoogleCodeExporter commented 9 years ago
make sense, I will return an out of service in this case.

Original comment by cf27...@gmail.com on 2 Jan 2013 at 11:19

GoogleCodeExporter commented 9 years ago
Thanks Chen 

Original comment by rashydos on 2 Jan 2013 at 11:23

GoogleCodeExporter commented 9 years ago
fixed, will be available in the next server update, thanks

Original comment by cf27...@gmail.com on 2 Jan 2013 at 12:11

GoogleCodeExporter commented 9 years ago
I think this bug should be reopened.

I have the same issue on j2me platform.
Mobile phone: Nokia 5530 Express Music

My application successfully runs in emulator but when it runs in the phone it 
fails.

Method LocationManager.getStatus() returns constant TEMPORARILY_UNAVAILABLE in 
the emulator as well as in the phone. Weird but location listener in the 
application receives location events even when LocationManager has this status 
(TEMPORARILY_UNAVAILABLE) when the application runs in the emulator. In the 
real phone the application fails with NullPointerException just after binding 
listener. I have no stack trace but I traced the error and found out that NPE 
is thrown from Codenameone framework.

Original comment by dmitry.t...@gmail.com on 26 Nov 2013 at 3:53

GoogleCodeExporter commented 9 years ago
Its unrelated. I suggest asking on the forum and including your code.

Original comment by shai.almog on 26 Nov 2013 at 7:12