登录谷歌播放后,GPSTracker无法正常工作

时间:2015-06-21 09:14:39

标签: android gps location google-play google-play-services

所以整整一天我都想知道为什么我的应用程序没有工作,特别是GPSTracker,上个月工作正常。但是突然它停了下来,在找不到任何办法之后我尝试在我的手机上重置工厂,它有效。但在我登录我的Google Play帐户后,它立即停止工作,是否有人知道这里发生了什么?是阻止我的位置服务还是什么? 这是代码:

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;
}

}

1 个答案:

答案 0 :(得分:0)

尝试使用Fused Location Provider API。 link