In one of my applications I am dynamically enabling the GPS system
setting based on a user's request. My problem is that I also have a
LocationListener which doesn't pick up this change. If the device is
restarted or the system settings Activity is brought up the
LocationListener kicks in -- the onProviderEnabled() method is called.
So.. it seems pretty obvious that I'm not notifying the system
correctly of my changes. I have tried sending a broadcast from both
the normal context and the application context. I thought this would
be fairly straight forward, but it has proven to be rather
frustrating. I put together a small test activity to demonstrate my
problem.

Any help is greatly appreciated!

Regards,
Ken


Here is my test Activity:
---------------------------------------------------
package com.example;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

public class TestActivity extends Activity {

    LocationListener locListener;
        LocationManager locManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);

        locListener = new MyLocationListener();
           locManager = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
           locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locListener);

            if(!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                Settings.System.putString(getContentResolver
(),Settings.System.LOCATION_PROVIDERS_ALLOWED,
LocationManager.GPS_PROVIDER);
                Intent intent = new Intent(Intent.ACTION_PROVIDER_CHANGED);
                sendBroadcast(intent);
                }
    }

    private class MyLocationListener implements LocationListener
    {
        public MyLocationListener() {
                Log.d("test", "MyLocationListener()");
        }

        public void onLocationChanged(Location loc) {
                Log.d("test", loc.getLatitude() + "/" + loc.getLongitude());
        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
                Log.d("test", "*** onProviderDisabled (" + provider + ")
***");
        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
                Log.d("test", "*** onProviderEnabled (" + provider + ")
***");
        }

        public void onStatusChanged(String provider, int status,
Bundle extras) {
            // TODO Auto-generated method stub
                Log.d("test", "*** onStatusChanged ***");
                Log.d("test", "provider = " + provider + "\tstatus = " +
status);
        }
    }
}

Here is the manifest:
---------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
    package="com.example">

    <uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.WRITE_SETTINGS" />

    <application android:label="test">
        <activity android:name="TestActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category
android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to