2015-06-21 122 views
0

因此,我花了整整一天想知道爲什麼我的應用程序不工作,特別是GPSTracker,上個月工作得很好。但突然停下來,找不到任何出路,我試着在我的手機上重置工廠,它工作。但是,當我登錄到我的Google Play帳戶後,它立即停止工作,有誰知道這裏發生了什麼?是阻止我的位置服務或什麼? 下面的代碼:GPSTracker無法登錄到谷歌播放

public class GPSTracker extends Service implements LocationListener { 


private final Context context; 

boolean isGPSEnabled = false; 
boolean isNetworkEnabled = false; 
boolean canGetLocation = false; 

Location location; 

double latitude; 
double longitude; 

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 5; 
private static final long MIN_TIME_BW_UPDATES = 5000; 
protected LocationManager locationManager; 

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

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

     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if(!isGPSEnabled && !isNetworkEnabled) { 

     } else { 
      this.canGetLocation = true; 

      if (isNetworkEnabled) { 

       locationManager.requestLocationUpdates(
         LocationManager.NETWORK_PROVIDER, 
         MIN_TIME_BW_UPDATES, 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

       if (locationManager != null) { 
        location = locationManager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

        if (location != null) { 

         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 
        } 
       } 

      } 

      if(isGPSEnabled) { 
       if(location == null) { 
        locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

        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 stopUsingGPS() { 
    if(locationManager != null) { 
     locationManager.removeUpdates(GPSTracker.this); 
    } 
} 

public double getLatitude() { 
    if(location != null) { 
     latitude = location.getLatitude(); 
    } 
    return latitude; 
} 

public double getLongitude() { 
    if(location != null) { 
     longitude = location.getLongitude(); 
    } 

    return longitude; 
} 

public boolean canGetLocation() { 
    return this.canGetLocation; 
} 

public void showSettingsAlert() { 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 

    alertDialog.setTitle("GPS is settings"); 

    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      context.startActivity(intent); 
     } 
    }); 

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 

    alertDialog.show(); 
} 



@Override 
public void onLocationChanged(Location location) { 

    this.location = location; 
    getLatitude(); 
    getLongitude(); 

    ParseUser user2 = ParseUser.getCurrentUser(); 
    ParseGeoPoint geoPoint = new ParseGeoPoint(getLatitude(), getLongitude()); 
    user2.put("lat_long", geoPoint); 
    user2.saveInBackground(); 

    Log.d("Coordinates", getLatitude() + " " + getLongitude()); 

    String username = user2.getUsername(); 
    ParseQuery<ParseObject> query = ParseQuery.getQuery("_User"); 
    query.whereWithinKilometers("lat_long", geoPoint, 0.03); 
    query.whereNotEqualTo("username", username); 

    query.findInBackground(new FindCallback<ParseObject>() { 
     public void done(List<ParseObject> listUsers, ParseException e) { 

      for (ParseObject object : listUsers){ 

       String ids = object.getString("fullname"); 
       Log.d("These people are here:"," "+ids); 

       SharedPreferences sharedPrefs = context.getSharedPreferences("com.parseapp.eseen.eseen", Context.MODE_PRIVATE); 
       Boolean onOFF = sharedPrefs.getBoolean("ToggleOnOff", true); 
       SharedPreferences settings = context.getSharedPreferences("NotificationIDs", Context.MODE_PRIVATE); 
       SharedPreferences.Editor editor = settings.edit(); 
       Set<String> myStrings = settings.getStringSet("myStrings", new HashSet<String>()); 
       ParseInstallation installation = ParseInstallation.getCurrentInstallation(); 




       if (onOFF) { 
        int a = 0; 
        for (String checking : myStrings) { 

         if (object.getObjectId().equals(checking)) { 
          a = 1; 
         } 
        } 
        if (a == 1) { 
         Log.d("This user: ", object.getString("fullname")+"---Exists"); 

        } else { 

         ParseUser user = ParseUser.getCurrentUser(); 
         ParseQuery query = ParseInstallation.getQuery(); 
         query.whereEqualTo("user", user.getObjectId()); 
         installation.saveInBackground(); 



         ParsePush androidPush = new ParsePush(); 
         androidPush.setMessage(object.getString("fullname") + " is near you!"); 
         androidPush.setQuery(query); 
         androidPush.sendInBackground(); 
         myStrings.add(object.getObjectId()); 
         editor.putStringSet("myStrings", myStrings); 
         editor.apply(); 
        } 

       } 



      } 

     } 
    }); 







} 




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

} 

@Override 
public void onProviderEnabled(String arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    // TODO Auto-generated method stub 

} 

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

}

+0

試試這個,而不是GPSTracker https://github.com/mcharmas/Android-ReactiveLocation – cYrixmorten

回答

0

嘗試使用一體化位置提供API。 link

+0

是的,我轉移到這[鏈接](http://www.androidhive.info/2015/02/android-location- api-using-google-play-services /),但由於我希望它在後臺工作,所以我在將它移動到服務時遇到問題,您可能知道有人對此有所瞭解? – Jawee

+0

@Jawee Look [here](http://android-developers.blogspot.in/2011/06/deep-dive-into-location.html),可能會幫助你。 – PPB