简体   繁体   中英

Retrieving location updates in Service in Android

I try to retrieve Location updates in Service by network provider, but the updates doesn,t perform. My code looks like this:

public class TimeService extends Service{

LocationManager lm;
LocationListener ll = new LocationListener()
{

    @Override
    public void onLocationChanged(Location location) {

        double latitude = location.getLatitude();
        double longtitude = location.getLongitude();

        Intent inte =new Intent( TimeService.this, myAppWidgetProvider.class);
        inte.setAction("action_location");
        inte.putExtra("lat", latitude);
        inte.putExtra("long", longtitude);

        PendingIntent pinte = PendingIntent.getBroadcast(TimeService.this, 0, inte, 0);

        try {
            pinte.send();
        } catch (CanceledException e) {
            // TODO Auto-generated catch block
            Log.d("sendPIException", ":-(");
        }

        Log.d("locReceived", "onLochanged");

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status,
            Bundle extras) {
        // TODO Auto-generated method stub

    }

};

Log "locReceived" in onLocationChanged doesn't appear i logcat Following code responsible for Location in my Service, too:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.registerReceiver(br, new IntentFilter(Intent.ACTION_TIME_TICK));
        lm =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
        return super.onStartCommand(intent, flags, startId);
    }

What's wrong in my code?

Criteria criteria = new Criteria();

String provider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(provider);

lm.requestLocationUpdates(provider, 0, 0, ll);

if (location == null) {
    Log.e("Impl", "Location not available");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM