我正在尝试从以下代码中获取用户位置名称。我收到错误,因为GUIStatics无法解析为变量。如何纠正它。任何人都可以告诉这个代码是否是实现位置名称的正确方法。
package com.example.location;
import java.util.List;
import java.util.Locale;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.CellLocation;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener {
LocationManager lmanager;
String provider;
String er="Please try again";
TextView t;
public static Context mContext;
private double latitude,longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
mContext=this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(TextView)findViewById(R.id.textView1);
lmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
lmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
lmanager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
locationUpdate();
}
private void locationUpdate() {
// TODO Auto-generated method stub
CellLocation.requestLocationUpdate();
}
private void getAddress(double lat, double lon) {
// TODO Auto-generated method stub
Geocoder coder=new Geocoder(MainActivity.mContext,Locale.getDefault());
try
{
List<Address> listaddress=coder.getFromLocation(lat, lon, 1);
Address obj=listaddress.get(0);
String add=obj.getAddressLine(0);
GUIStatics.currentAddress=obj.getSubAdminArea()+","+obj.getAdminArea();
GUIStatics.longitude=obj.getLongitude();
GUIStatics.latitude=obj.getLatitude();
GUIStatics.currentCity=obj.getSubAdminArea();
GUIStatics.currentState=obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
Toast.makeText(this, "Address=>" + add,Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude=location.getLatitude();
longitude=location.getLongitude();
GUIStatics.latitude=latitude;
GUIStatics.longitude=longitude;
Log.v("Test", "IGA" + "Lat" + latitude + " Lng" + longitude);
getAddress(latitude, longitude);
if(location!=null)
{
lmanager.removeUpdates(this);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Gps Disabled", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
if(status == LocationProvider.TEMPORARILY_UNAVAILABLE)
{
Toast.makeText(MainActivity.this,"LocationProvider.TEMPORARILY_UNAVAILABLE", Toast.LENGTH_SHORT).show();
}
else if(status== LocationProvider.OUT_OF_SERVICE)
{
Toast.makeText(MainActivity.this, "LocationProvider.OUT_OF_SERVICE", Toast.LENGTH_SHORT).show();
}
}
}
我的清单文件为
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.location"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name="com.example.location.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
提前致谢。请帮帮我