如何在Google Maps Android API v2中访问后台位置更改?

4
在我的应用程序中,我正在使用 Google Maps android API v2,并且必须在位置更改时获取当前位置坐标,即使我的应用程序在后台运行也要执行某些任务。我该如何做到这一点?
编辑:
以下是我的跟踪位置代码。

LocationService.java

public class LocationService extends Service {

String TAG = "LocationService";

LocationManager mylocManager;
LocationTracker mylocListener;  

String myprovider;
database obj = new database(this);

@Override
public IBinder onBind(Intent arg0) 
{
    return null;
}

@Override
public void onDestroy() 
{    
    super.onDestroy();
    if(mylocManager!=null)
    {
        mylocManager.removeUpdates(mylocListener);
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{

    mylocManager   = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mylocListener  = new LocationTracker();

    Location loc = null;

    for(String myprovider:mylocManager.getAllProviders())
    {
       loc = mylocManager.getLastKnownLocation(myprovider);
       if(loc!=null)
       {
           Log.d(TAG,"Updating with last known location");
           updateLocation(loc);
           break;
       }   
    }

    if(Build.FINGERPRINT.contains("generic"))
    {
        Log.d(TAG,"App running on emulator");
        mylocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mylocListener);            
    }
    else if(mylocManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
    {
        Log.d(TAG,"GPS Provider available");
        mylocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mylocListener);
    }
    else if(mylocManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
    {
         Log.d(TAG,"NETWORK Provider available");
        mylocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mylocListener);
    }
    else
    {
        Log.w(TAG,"No provider found. Stopping service");
        stopSelf();

    }  
    return super.onStartCommand(intent, flags, startId);
}




public class LocationTracker  implements LocationListener
{
    @Override
    public void onLocationChanged(Location loc)
    {
        Log.d(TAG,"Updating exact coordinates");
        Toast.makeText(getApplicationContext(), "Location changed", Toast.LENGTH_SHORT).show();
        updateLocation(loc);
        stopSelf();
    }

    @Override
    public void onProviderDisabled(String arg0) 
    {
        stopSelf();
    }

    @Override
    public void onProviderEnabled(String arg0)
    {
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2)
    {
    }
}
}
1个回答

3

您需要创建一个Service,即使您的应用程序在后台运行也会运行。

这个Service必须实现或拥有一个LocationListener对象。

当触发onLocationChanged时,您可以执行您的任务,因为这表示您已收到新的位置更新。


我写了那个代码。但它根本无法访问我的 GPS。 - Aswathy P Krishnan
您介意开一个新问题并在此处发布链接吗?请将您的代码放在新问题中。 - Emil Adz
1
代码看起来没问题,你是在哪里尝试运行它?requestLocationUpdates的哪种方法被运行了?也就是说,日志输出是什么? - Emil Adz
1
谢谢你提醒我运行服务!我在我的MainActivity中使用了这段代码。Intent locationService = new Intent( this, LocationService.class ); startService( locationService ); - Aswathy P Krishnan
嗨,我也遇到了在后台获取位置更新的问题。你能分享一下代码吗?这会对我很有帮助。 - Karthick
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接