问题
I'm having trouble with the locationmanager in Android. I have the following class:
package com.flyer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ContentProvider implements LocationListener {
private Activity connectedActivity;
public ContentProvider(Activity connectedActivity) {
this.connectedActivity = connectedActivity;
LocationManager locationManager = (LocationManager)connectedActivity.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, this);
}
public void onLocationChanged(Location loc)
{
MyActivity.rowsData.add(new RowData(0, "Test", "Val: " + Math.random(), 0));
}
public void onProviderDisabled(String provider)
{
Intent in = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
connectedActivity.startActivity(in);
}
public void onProviderEnabled(String provider)
{
Toast.makeText(connectedActivity.getApplicationContext(),
"Gps Enabled",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
MyActivity.rowsData.add(new RowData(0, "Test", "Val: "+Math.random(), 0));
}
}
I call the constructor ContentProvider(this) in my mainactivity. However when I telnet to my emulator and do a geo fix, nothing happens.
I tried setting the content with a breakpoint, it won't reach.
Also I'm developing for Android 2.1.
Any help would be appreciated.
Getting a warning:
10-30 21:10:38.787: WARN/GpsLocationProvider(52): Could not open GPS configuration file /etc/gps.conf
回答1:
Can you publish your manifest XML? Have you added the permissions necessary?
<android:name="android.permission.ACCESS_COARSE_LOCATION" />
<android:name="android.permission.ACCESS_FINE_LOCATION" />
Were you getting any exceptions in your logcat trace? I recommend trying this on a real device to rule out config issues with your emulator.
FYI, here's how I turn on debug trace for my app using adb logcat:
adb shell stop
adb shell setprop log.tag.MYTAG DEBUG
adb shell start
adb logcat MYTAG:D *:W > c:\temp\trace.txt
To avoid having to mess around with DEBUG level trace, I suggest you stick with logging at Log.INFO level. In that case you only need the last command.
来源:https://stackoverflow.com/questions/7946035/android-locationmanager-not-calling-methods