2012-11-09 55 views
3

我想查找当前位置,并通过后台服务活动将其与我的数据库中的某些数据进行比较......但没有了解如何执行此操作...请给我一些建议......这就是我正在做的目前从后台服务中找到当前位置android

package com.example.alert; 

    import android.app.Service; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.media.MediaPlayer; 
    import android.os.Bundle; 
    import android.os.IBinder; 
    import android.widget.Toast; 

    public class service extends Service{ 
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public void onCreate() { 
    Toast.makeText(this, "Background Service Created", Toast.LENGTH_LONG).show(); 



} 

public void onDestroy() { 
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 


} 

public void onStart(Intent intent, int startid) { 

    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
    LocationListener ll = new MyLocationListener(); 
    //lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5*60*10000, 0, ll); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 



} 


public class MyLocationListener implements LocationListener 
{ 

    public void onLocationChanged(Location loc) 
    { 
     if(loc!=null) 
     { 
      double x; 


      String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud = " + loc.getLongitude(); 
      Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_SHORT).show(); 
     /* cr.moveToFirst(); 
      while(!cr.isAfterLast()) { 
       al.add(cr.getString(cr.getColumnIndex(dbAdapter.KEY_NAME))); //add the item 
       cr.moveToNext(); 
      }*/ 
     // lm.addProximityAlert(loc.getLatitude(), loc.getLongitude(), 10009,, intent) 

      //for loop, alerters retrieving one by one 
//  x= calcDistance(loc.getLatitude(),loc.getLongitude(),z,y); 
    //  if(x<1){ 
     //  Intent myIntent = new Intent(ViewAlerters.this,CallMode.class); 
      //  startActivity(myIntent); 
      } 

     } 



    public void onProviderDisabled(String arg0) { 
     // TODO Auto-generated method stub 

    } 

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

    } 

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

    } 


    } 
    } 
+0

退房山姆的一个问题,我问[这里]答案(http://stackoverflow.com/questions/12983431/not-receiving-location-data - 从线程)。它应该帮助你 – chRyNaN

回答