gpt4 book ai didi

java - Android中的定位方法不起作用

转载 作者:行者123 更新时间:2023-11-30 03:23:30 24 4
gpt4 key购买 nike

我正在学习一个教程,但陷入了一些代码中。问题是我无法将当前坐标获取到我的textView(id:red)中,并且它不会更改状态。我怀疑该错误是未调用方法onLocationChanged()。我也随附了Java程序和布局xml文件。感谢您的帮助!

Android MainActivity.java

package com.example.wepicit;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.content.Context;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;

import com.facebook.*;
import com.facebook.model.*;


public class MainActivity extends Activity implements LocationListener{

protected LocationManager locationManager;
protected LocationListener locationListener;
Location newLocation = new Location(LocationManager.GPS_PROVIDER);
protected Context context;
String lat;
String provider;
protected String gps_enabled,network_enabled;
Button button;
TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
newLocation.set(location);
//System.out.println("Lat:"+ location.getLatitude() +" Long:"+ location.getLongitude());
text = (TextView) findViewById(R.id.red);
text.setText("Lat:"+ newLocation.getLatitude() +" Long:"+ newLocation.getLongitude());
Log.d("Lat:"+ newLocation.getLatitude() +" Long:"+ newLocation.getLongitude(), gps_enabled);
}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
Log.d("Latitude","status");
}

}


布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:text="Tester" />

<TextView
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:text="TextView" />

</RelativeLayout>

最佳答案

尝试使用此代码,这将为您提供GPS或网络提供商的位置。

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 Enabled");
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", "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;
}

关于java - Android中的定位方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18673448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com