2015-11-06 104 views
1

我是android开发新手。在过去的几个月中,我构建了一个现在可以运行的应用程序现在我想要改善每次打开应用程序时所有打扰我的小事情。Android GPS数据不显示

在这种情况下,我尝试获取GPS(仅GPS)位置。每当位置更新进入时,都应该显示。

出于任何原因,第一个职位(我可以看到他们进入调试日志中)被忽略并且不会显示在我的设备上。

下面是代码:

public class GpsLogging extends Activity{ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gps_logging); 

    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    LocationListener mlocListener = new MyLocationListener(); 
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 
} 

public class MyLocationListener implements LocationListener 
{ 
    @Override 
    public void onLocationChanged(Location loc) 
    { 
     double lat = loc.getLatitude(); 
     double lon = loc.getLongitude(); 
     float accuracy = loc.getAccuracy(); 

     TextView textView = (TextView) findViewById(R.id.gpsPosition); 
     textView.setText("My current location is: \n" + 
       "Latitude = " + lat +"\n"+ 
       "Longitude = " + lon +"\n"+ 
       "Accuracy = " + accuracy +" m"); 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
    } 
} 

将是很好,如果有人有一个想法,为什么unprecise数据被忽略。

这里是相应的XML的要求:

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

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/gpsPosition" 
    android:text="Aquiring GPS Position" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="46dp" /> 

<ProgressBar 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/progressBar" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 

+0

帖子布局xml也... –

+0

您的设备有GPS激活?检查 – LXSoft

+0

“我可以看到他们进入调试日志”: - 您在哪里编写日志的打印语句。 –

回答

0

使用

.getLastKnownLocation() 

方法。

例如:

Location earlierLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

理由:你刚刚打印onLocationChanged(新GPS)。

+0

谢谢。 我认为传入的数据总是一个更新的位置。 直列“源:2 纬度:53.072959 经度:8.793933 高度:181.399994 速度:0.000000 轴承:0.000000 精度:7.000000 时间戳:1446805781000 rawDataSize:0 RAWDATA:为0x0 会话状态:0 技术mask:1' – Entropia

+0

您的建议有效:) 谢谢 – Entropia

+0

@Entropia很好。然后你可以接受我的答案。干杯 – qecce