2015-02-23 84 views
0

我试图实现locationlistener,但是当我尝试启动服务时什么都没有发生。无法在Android中启动locationlistener服务

这是我在manifest资源配置文件实现的:

<service android:name="LocationService" 
     android:enabled="true" 
     android:exported="false" 
     android:label="LocationService" 
     /> 

我实现了一个类Locationservice用下面的代码:

public class LocationService extends Service 
{ 
    public static final String BROADCAST_ACTION = "Hello World"; 
    private static final int TWO_MINUTES = 1000 * 60 * 2; 
    public LocationManager locationManager; 
    public MyLocationListener listener; 
    public Location previousBestLocation = null; 

    Intent intent; 
    int counter = 0; 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     intent = new Intent(BROADCAST_ACTION); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) 
    { 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     listener = new MyLocationListener(); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); 
    } 

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

    protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
     if (currentBestLocation == null) { 
      // A new location is always better than no location 
      return true; 
     } 

     // Check whether the new location fix is newer or older 
     long timeDelta = location.getTime() - currentBestLocation.getTime(); 
     boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
     boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
     boolean isNewer = timeDelta > 0; 

     // If it's been more than two minutes since the current location, use the new location 
     // because the user has likely moved 
     if (isSignificantlyNewer) { 
      return true; 
      // If the new location is more than two minutes older, it must be worse 
     } else if (isSignificantlyOlder) { 
      return false; 
     } 

     // Check whether the new location fix is more or less accurate 
     int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); 
     boolean isLessAccurate = accuracyDelta > 0; 
     boolean isMoreAccurate = accuracyDelta < 0; 
     boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

     // Check if the old and new location are from the same provider 
     boolean isFromSameProvider = isSameProvider(location.getProvider(), 
       currentBestLocation.getProvider()); 

     // Determine location quality using a combination of timeliness and accuracy 
     if (isMoreAccurate) { 
      return true; 
     } else if (isNewer && !isLessAccurate) { 
      return true; 
     } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 
      return true; 
     } 
     return false; 
    } 



    /** Checks whether two providers are the same */ 
    private boolean isSameProvider(String provider1, String provider2) { 
     if (provider1 == null) { 
      return provider2 == null; 
     } 
     return provider1.equals(provider2); 
    } 



    @Override 
    public void onDestroy() { 
     // handler.removeCallbacks(sendUpdatesToUI); 
     super.onDestroy(); 
     Log.v("STOP_SERVICE", "DONE"); 
     locationManager.removeUpdates(listener); 
    } 

    public static Thread performOnBackgroundThread(final Runnable runnable) { 
     final Thread t = new Thread() { 
      @Override 
      public void run() { 
       try { 
        runnable.run(); 
       } finally { 

       } 
      } 
     }; 
     t.start(); 
     return t; 
    } 
    public class MyLocationListener implements LocationListener 
    { 

     public void onLocationChanged(final Location loc) 
     { 
      Log.i("**************************************", "Location changed"); 
      if(isBetterLocation(loc, previousBestLocation)) { 
       loc.getLatitude(); 
       loc.getLongitude(); 
       intent.putExtra("Latitude", loc.getLatitude()); 
       intent.putExtra("Longitude", loc.getLongitude()); 
       intent.putExtra("Provider", loc.getProvider()); 
       sendBroadcast(intent); 

       //Toast.makeText(MainActivity.this, "Location changed! Data sended to GCM & you are " + distance + "meters away from geofence",Toast.LENGTH_SHORT).show(); 


      } 
     } 

     public void onProviderDisabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
     } 


     public void onProviderEnabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 
     } 


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

     } 

    } 
} 

而在我的Activity类我想启动该服务,

Intent LocationService = new Intent(this, HomeActivity.class); 
this.startService(LocationService); 

什么都没有发生,当我从位置改变时,任何人都知道我是什么唱?

+0

看一看这个问题,它可能会帮助你:http://stackoverflow.com/questions/4765517/start -service-in-android – Bono 2015-02-23 14:19:05

+0

您是否已将所需的权限添加到清单中? – 2015-02-23 15:54:27

回答

0

意图是一条消息给系统。在这里你想告诉它作为服务启动你的LocationService类。要做到这一点,你需要提供你想要启动类服务:LocationService.class

Intent service = new Intent(this, LocationService.class); 
this.startService(service);