我尝试实现一个gps应用程序,(我从指南中获取代码)但是不起作用! 我认为问题是提供商已启用,因此应用无法计算纬度和经度。 请帮帮我,谢谢。
package it.wstech.gps;
import java.util.Date;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;
public class GPSMainActivity extends Activity {
private String providerId= LocationManager.GPS_PROVIDER;
private LocationListener myLocationListener= new LocationListener(){
//methods of Location Listener interface
@Override
public void onStatusChanged (String provider, int status, Bundle extras){
if (status==LocationProvider.AVAILABLE){
setTextViewValue(R.id.available,"TRUE");
}
else {
setTextViewValue(R.id.available,"FALSE");
}
}
@Override
public void onProviderEnabled (String provider){
setTextViewValue(R.id.enabled,"TRUE");
}
@Override
public void onProviderDisabled (String provider){
setTextViewValue(R.id.enabled,"FALSE");
}
@Override
public void onLocationChanged (Location location){
updateLocationData(location);
}
};
//activity methods
@Override
public void onCreate (Bundle savedIstanceState){
super.onCreate(savedIstanceState);
setContentView(R.layout.activity_gpsmain);
}
@Override
protected void onResume(){
super.onResume();
setTextViewValue(R.id.provider,providerId);
LocationManager locationManager = (LocationManager) getSystemService (LOCATION_SERVICE);
LocationProvider provider= locationManager.getProvider(providerId);
if (provider== null) {
setTextViewValue (R.id.available,"FALSE");
} else {
setTextViewValue (R.id.available,"TRUE");
boolean gpsEnabled=locationManager.isProviderEnabled(providerId);
if (gpsEnabled){
setTextViewValue (R.id.enabled,"TRUE");
}
else {
setTextViewValue (R.id.enabled,"FALSE");
}
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location !=null){
updateLocationData(location);
}
locationManager.requestLocationUpdates(providerId, 5, 1,myLocationListener);
}
}
@Override
protected void onPause(){
super.onPause();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.removeUpdates(myLocationListener);
}
private void setTextViewValue (int textViewId, String value){
TextView testView=(TextView) findViewById(textViewId);
if (testView!=null){
testView.setText(value);
}
}
private void updateLocationData(Location location){
Date timestamp= new Date (location.getTime());
setTextViewValue (R.id.timestamp, timestamp.toString());
double latitude=location.getLatitude();
setTextViewValue (R.id.latitude, String.valueOf(latitude));
double longitude = location.getLongitude();
setTextViewValue (R.id.longitude, String.valueOf(longitude));
if (location.hasAltitude()){
double altitude = location.getAltitude();
setTextViewValue (R.id.altitude, String.valueOf(altitude));
}
if (location.hasSpeed()){
float speed=location.getSpeed();
setTextViewValue(R.id.speed, String.valueOf(speed));
}
}
}
我的activity_gpsmain.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow
android:padding="5px">
<TextView
android:text="Provider"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/provider"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:padding="5px">
<TextView
android:text="Disponibile"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/available"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:padding="5px">
<TextView
android:text="Abilitato"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@+id/enabled"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:padding="5px">
<TextView
android:text="Timestamp"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/timestamp"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:padding="5px">
<TextView
android:text="Latitudine"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/latitude"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:padding="5px">
<TextView
android:text="Longitudine"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@+id/longitude"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:padding="5px">
<TextView
android:text="Altitudine"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@+id/altitude"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:padding="5px">
<TextView
android:text="Velocità"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@+id/speed"
android:padding="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.wstech.gps"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name ="android.permission.ACCESS_FINE_LOCATION"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".GPSMainActivity"
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>
结果:
提供商gps
Disponibile TRUE
Abilitato false
时间戳
Latitudine
纵向错误
Altitudine false
Velocitàfalse
答案 0 :(得分:-1)
创建一个类GPSTracker.java
package com.example.gpstracker;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener
{
private final Context _context;
boolean isGPSEnabled=false;
boolean isNetworkEnabled=false;
boolean canGetLocation=false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=5; //5 meters
private static final long MIN_TIME_BW_UPDATES=1000*60*1; // 1 minute
protected LocationManager locationManager;
public GPSTracker(Context contet)
{
this._context=contet;
getLocation();
}
public Location getLocation()
{
try
{
locationManager =(LocationManager)_context.getSystemService(LOCATION_SERVICE);
//getting GPS Status
isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting network status
isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled)
{
}
else
{
this.canGetLocation=true;
//first get location from network provider
if(isNetworkEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.i("Network","Network");
if(locationManager !=null)
{
location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location !=null)
{
latitude=location.getLatitude();
longitude=location.getLongitude();
}
}
}
// if GPS Enabled get lat/log
if(isGPSEnabled)
{
if(location == null)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.i("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;
}
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude()
{
if(location !=null)
{
latitude=location.getLatitude();
}
return latitude;
}
public double getLongitude()
{
if(location !=null)
{
longitude=location.getLongitude();
}
return longitude;
}
public boolean canGetLocation()
{
return this.canGetLocation;
}
public void showSettingAlert()
{
AlertDialog.Builder alert=new AlertDialog.Builder(_context);
alert.setTitle("GPS is Setting");
alert.setMessage("GPS is not enabled. Do you want to go to settings..?");
alert.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
_context.startActivity(i);
}
});
alert.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.show();
}
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
创建活动类文件MainActivity.java
package com.example.gpstracker;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button btnClick;
GPSTracker tracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClick=(Button)findViewById(R.id.btnClick);
btnClick.setOnClickListener(this);
}
@Override
public void onClick(View v) {
tracker=new GPSTracker(getApplicationContext());
if(tracker.canGetLocation())
{
double latitude=tracker.getLatitude();
double longitude=tracker.getLongitude();
Toast.makeText(MainActivity.this,"your Location is \n Latitude : " + latitude +
"\n Longitude : "+longitude,Toast.LENGTH_LONG).show();
}
else
{
tracker.showSettingAlert();
}
}
}
在manifest.xml中添加以下行
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>