Android后台位置服务不返回新位置

时间:2014-06-05 16:59:28

标签: android location

我是Android开发环境的新手。我希望有一个能够在后台运行的服务,以便在手机移动30米时为我提供位置更新。我可以获取最后一个位置(我在Web视图中显示它)但在移动手机时没有获得任何新的更新。     我已将此行放在AndroidManifest.xml中。

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


    <!-- for google play services -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <service android:name="LocationBackgroundService"></service>

这是代码。我错过了什么?提前感谢您的帮助。

MainActivity onCreate

    boolean connected = myLocationManager.servicesConnected();
    // services connected is the same code as   http://developer.android.com/training/location/receive-location-updates.html
    // load my web page ….

因为我希望首先加载我的网页,所以我可以在其地图上绘图,直到我才连接到Google Play服务 我的网络视图客户端中的 onPageFinished

    myLocationManager.connectToGoogleLocationServices();

看起来像:

    this.locationClient = new LocationClient(MainActivity.this, this, this);
    this.locationClient.connect();  

一旦我进行连接,就会调用连接的回调。 这是我创建后台服务的地方,并通过使用 onConnected 回调中的挂起意图来启动它:

    locationHandler = new LocationBackgroundService();
    this.myPendingIntent = locationHandler.startHandler(MainActivity.this, locationClient);
    // get current location
Location location = locationClient.getLastLocation();
if (location != null)
{
        locationHandler.storeLastLocation(location) ;
        // at this point we know the web page is up to draw on

}

this.request = LocationRequest.create();
this.request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
this.request.setInterval(2 * 1000);
this.request.setSmallestDisplacement(distanceFilter);

// start monitoring locations in the background
this.locationClient.requestLocationUpdates(request, this.myPendingIntent);

我确实看到“最后位置”出现在我的网页上的正确位置。但就是这样 - 不再是。

以下是LocationBackgroundService服务的代码:

    public class LocationBackgroundService extends IntentService
    {
    /**
     * Constructor
     * 
     */
     public LocationBackgroundService()
     {
        super("LocationReceiverHandler");

        logger.warning("LocationReceiverHandler constructed!");
     }

     /**
      * method gets the location changes
      */
     @Override
     protected void onHandleIntent(Intent intent)
     {
        logger.info("in onHandleIntent in thread " + Thread.class.getName());

        Location location =                       intent.getParcelableExtra(LocationClient.KEY_LOCATION_CHANGED);
        if (location != null)
        {
            onLocationChanged(location) ;
            //notifyMainActivity(location);
        }
        else
        {
            MainActivity.getJavaScript().showMessageOnWebPage("No LOCATION");
        }

    public PendingIntent startHandler(Context myAppContext, LocationClient locationClient)
     {

        this.myAppContext = myAppContext;
        logger.warning("in startHandler " + Thread.class.getName());
        Intent mIntentService = new Intent(myAppContext, this.getClass());
        // Retrieve a PendingIntent that will start a service, like calling Context.startService().
        PendingIntent pendingIntent = PendingIntent.getService(myAppContext, 1, mIntentService,         PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
     }

     public void storeLastLocation (Location location )
     {
        this.lastLocation = location ;


        String newLocaionJSonString = createJSONLocationString(location);
        MainActivity.getJavaScript().drawMeOnWebScreen(newLocaionJSonString);
     }

     public void onLocationChanged(Location location)
     {

        if (location != null)
            {
            logger.warning("onLocationChanged " + location);
            boolean send = isFarEnoughLocationFromNew(location, this.distanceFilter);
            //if (send)
            {
            String newLocaionJSonString = createJSONLocationString(location);
            MainActivity.getJavaScript().drawMeOnWebScreen(newLocaionJSonString);
            }
        }
        else
        {
            logger.severe("onLocationChanged return null location");
        }

     }

1 个答案:

答案 0 :(得分:0)

你不想使用IntentService,因为一旦onHandleIntent完成,服务就会停止。所以你不会得到回调。您需要使用常规Service来继续获取位置更新。

如果您在MainActivity中执行所有操作可能会更好