How to get current location using LocationManager and timeout?
<p>I need to do the following task: when my alarm wakes up the application will get the current location using GPS with timeout 60 sec; if the timeout is then the application will try to get location using NetworkProvider with timeout 60 sec again; if the the timeout is again, then the application will log in the LOG. So, I have the following code:</p>

<p>Code for Alarm events:</p>

<pre><code>public class AlarmEventReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.e("event", "onReceive");
        LocationManager lm = (LocationManager)context.getSystemService(context.LOCATION_SERVICE);
        String provider=lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ? LocationManager.GPS_PROVIDER :
            LocationManager.NETWORK_PROVIDER;

        CustomLocationListener listener=new CustomLocationListener(lm, provider);
        lm.requestLocationUpdates(provider, 1000, 1, listener);

        Handler handler=new Handler();
        handler.postDelayed(new TimeoutTask(lm, listener), 60*1000);
    }
}
</code></pre>

<p>Events work correctly. The code for the TimeoutTask:</p>

<pre><code>public class TimeoutTask implements Runnable {

    private LocationManager manager;
    private CustomLocationListener listener;

    public TimeoutTask(LocationManager manager, CustomLocationListener listener) {
        this.manager=manager;
        this.listener=listener;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        if (listener.isCompleted()) {
            Log.e("provider", "completed");
        }
        else {
            Log.e("provider", "timeout");
            manager.removeUpdates(listener);
            if (listener.isGpsProvider()) {

                if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                    Log.e("provider", "network provider is enabled");
                    CustomLocationListener cll=new CustomLocationListener(manager, LocationManager.NETWORK_PROVIDER);
                    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 120*1000, 0, 
                        cll);
                    Handler h=new Handler();
                    h.postDelayed(new TimeoutTask(manager, cll), 60*1000);
                }
                else {
                    Log.e("provider", "network provider is disabled");
                }
            }
            else {
                Log.e("provider", "timeout and network provider");
            }
        }
    }
}
</code></pre>

<p>The code for the CustomLocationListener:</p>

<pre><code>public class CustomLocationListener implements LocationListener {

    private LocationManager manager;
    private boolean isCompleted = false;
    private String provider;

    public CustomLocationListener(LocationManager manager, String provider) {
        this.provider=provider;
        this.manager=manager;
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        Log.e("listener", "location is founded");
        isCompleted=true;
        manager.removeUpdates(this);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}

    @Override
    public void onProviderEnabled(String provider) {}

    @Override
    public void onProviderDisabled(String provider) {}

    public boolean isCompleted() {
        return isCompleted;
    }

    public boolean isGpsProvider() {
        return (provider.equals(LocationManager.GPS_PROVIDER)) ? true : false;
    }
}
</code></pre>

<p>Manifest:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.atdq.beacon"
    android:versionCode="2"
    android:versionName="1.1" &gt;

    &lt;uses-sdk android:minSdkVersion="7" /&gt;

    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
    &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;

    &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;"

    &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
    &lt;uses-permission android:name="android.permission.RECEIVE_SMS" /&gt;
    &lt;uses-permission android:name="android.permission.READ_SMS" /&gt;
    &lt;uses-permission android:name="android.permission.SEND_SMS" /&gt;
    &lt;uses-permission android:name="android.permission.WRITE_SMS" /&gt;
    &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;

    &lt;application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" &gt;
        &lt;activity
            android:label="@string/app_name"
            android:name=".MainActivity"
            android:screenOrientation="portrait" &gt;
            &lt;intent-filter &gt;
                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

        &lt;receiver android:name=".ShutdownReceiver" &gt;
            &lt;intent-filter &gt;
                &lt;action android:name="android.intent.action.ACTION_SHUTDOWN" /&gt;       
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;
        &lt;receiver android:name=".AlarmEventReceiver" /&gt;
        &lt;receiver android:name=".RebootingReceiver" &gt;
            &lt;intent-filter &gt;
                &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;
        &lt;receiver android:name=".SmsReceiver" &gt;
            &lt;intent-filter android:priority="100" &gt;
                &lt;action android:name="android.provider.Telephony.SMS_RECEIVED" /&gt;
                &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;
    &lt;/application&gt;

&lt;/manifest&gt;
</code></pre>

<p>So, it works correctly if a GPS is disabled. But if I enable a GPS then the application then the GPS icon will be shown during 1 sec on device's notification bar, and the application won't write anymore except "event" in LOG. Please, tell me, where have I made a mistake? </p>
