我认为这将是非常容易的 - 但这需要的时间比它应该更长。
我只是试图让GPS坐标转到logcat,这就是字面意思。
目的很好。 "意图被解雇"每次都会显示消息。该应用程序只是拒绝转到位置更改事件。
如果有人可以看一看,让我知道我失败的地方,我会非常感激。
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");
}
}
谢谢
答案 0 :(得分:2)
好吧,位置管理器异步工作,IntentService
以这种方式实现,当onHandleIntent(Intent intent)
返回时,服务自行停止。这意味着在将任何位置提供给您的onLocationChanged(location location)
方法之前,它已被销毁。如果您想使用IntentService
,则需要阻止onHandleIntent(Intent intent)
方法在您拥有位置之前返回,或者在获得{Service
时使用标准stopSelf
并执行Location
{1}}对象。
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一样。
答案 1 :(得分: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;
}
}