If you think the Android project gasp-gcm-client listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.cloudbees.gasp.fragment;
/*fromwww.java2s.com*/import android.app.Fragment;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesUtil;
/**
* Copyright (c) 2013 Mark Prichard, CloudBees
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/publicclass LocationFragment extends Fragment implements LocationListener {
privatestaticfinal String TAG = LocationFragment.class.getName();
privatestatic LocationManager locationManager;
privatestatic String provider;
@Override
publicvoid onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
publicstatic Location getLocation(Context context) {
Location location = null;
try {
if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) == 0) {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
@Override
publicvoid onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 500, 50, this);
}
@Override
publicvoid onLocationChanged(Location location) {
String locationMessage = "Location: " + String.format("%.6f", location.getLatitude())
+ ", " + String.format("%.6f", location.getLongitude())
+ " (via " + location.getProvider() + ")";
Toast.makeText(getActivity(), locationMessage, Toast.LENGTH_SHORT).show();
}
@Override
publicvoid onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
publicvoid onProviderEnabled(String provider) {
Toast.makeText(getActivity(), "Enabled provider " + provider, Toast.LENGTH_SHORT).show();
}
@Override
publicvoid onProviderDisabled(String provider) {
Toast.makeText(getActivity(), "Disabled provider: " + provider, Toast.LENGTH_SHORT).show();
}
}