感谢您提前抽出时间和帮助。我正在使用GPS工作,只有我已经获得当前位置,我已经尝试更新位置值,但我没有任何改进。我可以问你怎么更新位置?代码如下:
activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/prov"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No provider selected yet"
android:layout_marginTop="10dp"
android:textSize="20dp" />
<TextView
android:id="@+id/lat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Latitude: -"
android:textSize="20dp" />
<TextView
android:id="@+id/lon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Longitude: -"
android:textSize="20dp" />
</LinearLayout>
MainActivity.java:
package com.example.locationservicetest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView latitude;
TextView longitude;
TextView provText;
LocationManager locationManager;
String provider;
MyLocationListener mylistener;
Criteria criteria;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitude = (TextView) findViewById(R.id.lat);
longitude = (TextView) findViewById(R.id.lon);
provText = (TextView) findViewById(R.id.prov);
//Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Define the criteria how to select the location provider
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); //default
//Charge of data?
criteria.setCostAllowed(false);
//Get the best provider depending on the criteria
provider = locationManager.getBestProvider(criteria, false);
//The last known location of this provider
Location location = locationManager.getLastKnownLocation(provider);
mylistener = new MyLocationListener();
if (location != null) {
mylistener.onLocationChanged(location);
} else {
//leads to the settings because there is no last known location
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
// location updates: at least 1 meter and 200millsecs change
locationManager.requestLocationUpdates(provider, 200, 1, mylistener);
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
// Initialize the location fields
latitude.setText("New Latitude: "+String.valueOf(location.getLatitude()));
longitude.setText("New Longitude: "+String.valueOf(location.getLongitude()));
provText.setText(provider + " provider has been selected.");
Toast.makeText(getBaseContext(),"Location changed!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(getBaseContext(), provider + "'s status changed to "+status +"!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(), "Provider " + provider + " enabled!",
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getBaseContext(), "Provider " + provider + " disabled!",
Toast.LENGTH_SHORT).show();
}
}
}
我更改了AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationservicetest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.locationservicetest.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>
</manifest>
logcat说:
05-20 20:29:40.375: D/ResourcesManager(16834): creating new AssetManager and set to /data/app/com.example.locationservicetest-2/base.apk
05-20 20:29:40.475: D/Activity(16834): performCreate Call secproduct feature valuefalse
05-20 20:29:40.475: D/Activity(16834): performCreate Call debug elastic valuetrue
05-20 20:29:40.495: D/OpenGLRenderer(16834): Render dirty regions requested: true
05-20 20:29:40.545: I/Adreno-EGL(16834): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
05-20 20:29:40.545: I/Adreno-EGL(16834): OpenGL ES Shader Compiler Version: E031.25.01.03
05-20 20:29:40.545: I/Adreno-EGL(16834): Build Date: 10/28/14 Tue
05-20 20:29:40.545: I/Adreno-EGL(16834): Local Branch: LA.BF.1.1_RB1_20141028_021_patches2
05-20 20:29:40.545: I/Adreno-EGL(16834): Remote Branch:
05-20 20:29:40.545: I/Adreno-EGL(16834): Local Patches:
05-20 20:29:40.545: I/Adreno-EGL(16834): Reconstruct Branch:
05-20 20:29:40.545: I/OpenGLRenderer(16834): Initialized EGL, version 1.4
05-20 20:29:40.565: I/OpenGLRenderer(16834): HWUI protection enabled for context , &this =0xafb22088 ,&mEglDisplay = 1 , &mEglConfig = 8
05-20 20:29:40.565: D/OpenGLRenderer(16834): Enabling debug mode 0
05-20 20:29:40.655: I/Timeline(16834): Timeline: Activity_idle id: android.os.BinderProxy@16c2c9f9 time:221858392
感谢您的指导,帮助和时间。谢谢。