2015-11-06 38 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