2015-09-25 81 views
0

我认为这将是超级简单的 - 但这是花了更长的时间比它应该。当Android意图开始时获取GPS

我只是想获得GPS坐标去logcat,这就是字面意思。

意图着火。每次都会出现“Intent Fired”消息。该应用程序拒绝去位置更改事件。

如果有人可以看看,让我知道我失败的地方,这将是超级赞赏。

public class MyReceiver extends IntentService implements LocationListener { 
private LocationManager locationManager; 
public MyReceiver() { 
    super("MyReceiver"); 
} 

protected void onHandleIntent(Intent intent) { 
    Log.i("DebugMe", "Intent Fired"); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
} 
public void onLocationChanged(Location location) { 
    String lat = String.valueOf(location.getLatitude()); 
    String lon = String.valueOf(location.getLongitude()); 
    Log.i("DebugMe", lat + " " + lon); 
    locationManager.removeUpdates(this); 
} 
public void onProviderDisabled(String provider) { 
    Log.d("DebugMe ", "Disabled"); 
} 
public void onProviderEnabled(String provider) { 
    Log.d("DebugMe","Enabled"); 
} 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    Log.d("DebugMe","Status"); 
} 
} 

谢谢

回答

2

好,位置管理器以异步方式工作,并IntentService以这样的方式来实现,当onHandleIntent(Intent intent)回报,服务将停止本身。这意味着它可以在任何位置提供给您的方法之前销毁。如果要使用IntentService,则需要在获得位置之前阻止onHandleIntent(Intent intent)方法返回,或者在获得Location对象时使用标准Service并执行stopSelf

public class MyReceiver extends Service implements LocationListener { 

    private LocationManager locationManager; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     onHandleIntent(intent); 
     return START_NOT_STICKY; 
    } 

    protected void onHandleIntent(Intent intent) { 
     Log.i("DebugMe", "Intent Fired"); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
    } 

    public void onLocationChanged(Location location) { 
     String lat = String.valueOf(location.getLatitude()); 
     String lon = String.valueOf(location.getLongitude()); 
     Log.i("DebugMe", lat + " " + lon); 
     locationManager.removeUpdates(this); 
     // this is where you stop the service 
     stopSelf(); 
    } 

    public void onProviderDisabled(String provider) { 
     Log.d("DebugMe ", "Disabled"); 
    } 

    public void onProviderEnabled(String provider) { 
     Log.d("DebugMe", "Enabled"); 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
     Log.d("DebugMe", "Status"); 
    } 

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

请记住,此实现将不会在默认情况下作为IntentService运行在单独的线程中。

+0

“当您获得Location对象时,使用标准Service和执行stopSelf。” - 恕我直言,这是更好的方法。 – CommonsWare

+0

它当然是 – maciekjanusz

+0

嗯,感谢上帝,我问 - 我永远不会想到我自己。为什么我使用意向服务的原因是因为这个应用程序需要能够从adb采取命令,这个解决方案允许我从笔记本电脑上做“adb shell am startservice ...”...有没有另外一种方法来实现GPS从adb cmd行查找? – Nefariis

0

你可以使用这个类。

public class GPSTracker extends Service implements LocationListener { 

    private final Context mContext; 

    // flag for GPS status 
    boolean isGPSEnabled = false; 

    // flag for network status 
    boolean isNetworkEnabled = false; 

    // flag for GPS status 
    boolean canGetLocation = false; 

    Location location; // location 
    double latitude; // latitude 
    double longitude; // longitude 
    double altitude; // altitude 
    public boolean isLocationAlertShown = true; 
    public boolean comeBackAfterTurnGPSON = false; 


    // The minimum distance to change Updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

    // Declaring a Location Manager 
    protected LocationManager locationManager; 

    public GPSTracker(Context context) { 
     this.mContext = context; 
     getLocation(); 
    } 

    public Location getLocation() { 
     try { 
      locationManager = (LocationManager) mContext 
        .getSystemService(LOCATION_SERVICE); 

      // getting GPS status 
      isGPSEnabled = locationManager 
        .isProviderEnabled(LocationManager.GPS_PROVIDER); 

      // getting network status 
      isNetworkEnabled = locationManager 
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

      if (!isGPSEnabled && !isNetworkEnabled) { 
       // no network provider is enabled 

      } else { 
       this.canGetLocation = true; 
       if (isNetworkEnabled) { 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (isGPSEnabled) { 
        if (location == null) { 
         locationManager.requestLocationUpdates(
           LocationManager.GPS_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (locationManager != null) { 
          location = locationManager 
            .getLastKnownLocation(LocationManager.GPS_PROVIDER); 

          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return location; 
    } 
    public void Bearing(Location start,Location end) 
    { 
     start.bearingTo(end); 
    } 

    /** 
    * Stop using GPS listener 
    * Calling this function will stop using GPS in your app 
    * */ 
    public void stopUsingGPS(){ 
     if(locationManager != null){ 
      locationManager.removeUpdates(GPSTracker.this); 
     }  
    } 

    /** 
    * Function to get latitude 
    * */ 
    public double getLatitude(){ 
     if(location != null){ 
      latitude = location.getLatitude(); 
     } 

     // return latitude 
     return latitude; 
    } 

    /** 
    * Function to get longitude 
    * */ 
    public double getLongitude(){ 
     if(location != null){ 
      longitude = location.getLongitude(); 
     } 

     // return longitude 
     return longitude; 
    } 
    /** 
    * Function to get altitude 
    * */ 
    public double getAltitude(){ 
     if(location != null){ 
      altitude = location.getAltitude(); 
     } 

     // return latitude 
     return altitude; 
    } 
    /** 
    * Function to check GPS/wifi enabled 
    * @return boolean 
    * */ 
    public boolean canGetLocation() { 
     return this.canGetLocation; 
    } 

    /** 
    * Function to show settings alert dialog 
    * On pressing Settings button will lauch Settings Options 
    * */ 
    public void showSettingsAlert(){ 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 
     alertDialog.setCancelable(true); 

     // Setting Dialog Title 
     //  alertDialog. 

     // Setting Dialog Message 
     alertDialog.setMessage("Please Turn on Location Services"); 

     // On pressing Settings button 
     alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int which) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       mContext.startActivity(intent); 
       comeBackAfterTurnGPSON = true; 
      } 
     }); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     EventBus.getDefault().post(new OnLocationChange(location)); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

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

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

}