我正在尝试使用可检测GPS是否启用的应用。如果没有,应用程序必须使用启用GPS时收到的数据更新位置数据,但现在我无法这样做。我的代码如下:
public class App extends Activity implements LocationListener {
private GoogleMap googleMap;
protected TextView city;
protected TextView street;
LocationManager lm = null;
LocationListener ll = null;
Location location;
Boolean isGPSEnabled;
Boolean isNetworkEnabled;
Boolean canGetLocation;
double longitude;
double latitude;
public static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
public static final long MIN_TIME_BW_UPDATES = 1000*60*1; // 1 minute
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parking_coche);
boolean flag = displayGpsStatus();
if (flag){
initilizeMap();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
location = getLocation();
longitude = location.getLongitude();
latitude = location.getLatitude();
Geocoder geocoder;
List<Address> addresses;
String ciudad="",calle="";
try{
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
calle = addresses.get(0).getAddressLine(0);
ciudad = addresses.get(0).getLocality();
}
catch(IOException ex)
{
System.out.println(ex);
}
city = (TextView)findViewById(R.id.txtCiudad);
street = (TextView)findViewById(R.id.txtCalle);
city.setText(ciudad);
street.setText(calle);
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.car_map_35_35));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(latitude, longitude)).zoom(17).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
else
{
alertbox("Estado GPS", "Su GPS está desactivado");
}
}
@Override
public void onLocationChanged(Location location) {
boolean flag = displayGpsStatus();
if (flag){
initilizeMap();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
location = getLocation();
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Geocoder geocoder;
List<Address> addresses;
String ciudad="",calle="";
try{
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
calle = addresses.get(0).getAddressLine(0);
ciudad = addresses.get(0).getLocality();
}
catch(IOException ex)
{
System.out.println(ex);
}
city = (TextView)findViewById(R.id.txtCiudad);
street = (TextView)findViewById(R.id.txtCalle);
city.setText(ciudad);
street.setText(calle);
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.car_map_35_35));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(latitude, longitude)).zoom(17).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
else
{
alertbox("Estado GPS", "Su GPS está desactivado");
}
}
protected void alertbox(String title, String mymessage) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(mymessage)
.setCancelable(false)
.setTitle(title)
.setPositiveButton("Habilitar GPS",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
dialog.cancel();
}
})
.setNegativeButton("Cancelar",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the dialog box
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public Location getLocation() {
try {
lm = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = lm
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = lm
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
lm.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (lm != null) {
location = lm
.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) {
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS", "GPS Enabled");
if (lm != null) {
location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"No se ha podido mostrar el mapa", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
ll = new App();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, ll, null);
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.removeUpdates(ll);
lm = null;
}
现在,如果GPS已禁用,然后我启用它,则显示的数据为空。如何在启用GPS后更新数据?
先谢谢你。
在onResume方法中输入相同的onCreate方法代码后更新代码:
10-27 10:08:16.578:E / AndroidRuntime(30986):java.lang.RuntimeException:无法恢复活动{com.axiomapps.app/com.axiomapps.app.app}:java.lang.NullPointerException 10-27 10:08:16.578:E / AndroidRuntime(30986):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2919) 10-27 10:08:16.578:E / AndroidRuntime(30986):在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948) 10-27 10:08:16.578:E / AndroidRuntime(30986):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1358) 10-27 10:08:16.578:E / AndroidRuntime(30986):在android.os.Handler.dispatchMessage(Handler.java:99) 10-27 10:08:16.578:E / AndroidRuntime(30986):在android.os.Looper.loop(Looper.java:176) 10-27 10:08:16.578:E / AndroidRuntime(30986):在android.app.ActivityThread.main(ActivityThread.java:5419) 10-27 10:08:16.578:E / AndroidRuntime(30986):at java.lang.reflect.Method.invokeNative(Native Method) 10-27 10:08:16.578:E / AndroidRuntime(30986):at java.lang.reflect.Method.invoke(Method.java:525) 10-27 10:08:16.578:E / AndroidRuntime(30986):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1046) 10-27 10:08:16.578:E / AndroidRuntime(30986):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 10-27 10:08:16.578:E / AndroidRuntime(30986):at dalvik.system.NativeStart.main(Native Method) 10-27 10:08:16.578:E / AndroidRuntime(30986):引起:java.lang.NullPointerException 10-27 10:08:16.578:E / AndroidRuntime(30986):at com.axiomapps.app.App.onResume(App.java:249) 10-27 10:08:16.578:E / AndroidRuntime(30986):在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209) 10-27 10:08:16.578:E / AndroidRuntime(30986):在android.app.Activity.performResume(Activity.java:5450) 10-27 10:08:16.578:E / AndroidRuntime(30986):在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2909) 10-27 10:08:16.578:E / AndroidRuntime(30986):... 10 more
答案 0 :(得分:0)
您创建活动的新对象,这是错误的。你不能这样做:
ll = new App(); // you created new instance of your activity, you can't do that
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, ll, null);
必须这样做:
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null); //replaced to "this"