2012-03-27 48 views
1

我想找出當前位置的應用程序在後臺查找當前位置,當應用程序在後臺運行不工作

運行時,我有下面的代碼在後臺運行的服務查找當前位置。但它不工作。

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
public class MyService extends Service { 
private static LocationManager mlocManager; 
private static final String TAG = "MyService"; 


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

@Override 
public void onCreate() { 
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onCreate"); 

} 

@Override 
public void onDestroy() { 
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onDestroy"); 

} 

@Override 
public void onStart(Intent intent, int startid) { 
    Toast.makeText(this, "start My Service Started", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onStart"); 
    mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

    LocationListener mlocListener = new MyLocationListener(); 
    //Location newloc= new Location(); 


    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

    Toast.makeText(this, "hiiiits created", Toast.LENGTH_LONG).show(); 

} 
/* Class My Location Listener */ 

public class MyLocationListener implements LocationListener 

{ 

@Override 

public void onLocationChanged(Location loc) 

{ 
    Log.d(TAG, "onlocationchnage"); 
loc.getLatitude(); 

loc.getLongitude(); 

String Text ="My current location is: " + 

"Latitud = " + loc.getLatitude() + 

"Longitud = " + loc.getLongitude(); 

Toast.makeText(getApplicationContext(), 

Text, 

Toast.LENGTH_SHORT).show(); 

} 

@Override 

public void onProviderDisabled(String provider) 

{ 

Toast.makeText(getApplicationContext(), 

"Gps Disabled", 

Toast.LENGTH_SHORT).show(); 

} 

@Override 

public void onProviderEnabled(String provider) 

{ 

Toast.makeText(getApplicationContext(), 

"Gps Enabled", 

Toast.LENGTH_SHORT).show(); 

} 

@Override 

public void onStatusChanged(String provider, int status, Bundle extras) 

{ 

} 

}/* End of Class MyLocationListener */ 
} 

回答

1

我想你需要現在的緯度和使用GPS經度值..

請點擊此鏈接.. Click link

我已經嘗試過了,在Android手機。它的工作進行測試。

注: 模擬器我們沒有得到CURENT GPS位置。你必須和應該在手機中安裝應用程序.. 首先,你在手機的GPS後,試試吧,它會工作。

祝您有美好的一天。

相關問題