Android上的GPS程序,真機測試。
就是不顯示數據,Eclipse中報錯:Fail to find provider info for com.google.android.gsf.gservices
我開始的時候猜想,可能是因為在室內,所以得不到GPS的數據,可是在室外試了一次也沒得到任何數據,難道是天氣問題?
注:此程序在AVD中運行的時候用DDMS發送數據可以正常顯示。
解答:
在晴朗的天氣,戶外,終於得到了數據。
另:真機測試時發現有的手機可以,有的手機不可以得到數據,所以可能跟具體的硬件也有關系。
最后注明:GPS一定要開放,即通知區域應該顯示GPS的那個小圓圈轉啊轉的。
附上代碼:
package com.example.hellogps; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.Menu; import android.widget.TextView; public class HelloGPSActivity extends Activity { public static final String LOG_TAG = "HelloGPS"; LocationManager locationManager = null; String provider = null; @Override public void onCreate(Bundle savedInstanceState) { Log.v(LOG_TAG, "onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello_gps); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); provider = LocationManager.GPS_PROVIDER; Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); } @Override protected void onResume() { Log.v(LOG_TAG, "onResume"); super.onResume(); locationManager.requestLocationUpdates(provider, 0, 0, mLocationListener); } @Override protected void onPause() { Log.v(LOG_TAG, "onPause"); super.onPause(); locationManager.removeUpdates(mLocationListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { Log.v(LOG_TAG, "onCreateOptionsMenu"); getMenuInflater().inflate(R.menu.activity_hello_gps, menu); return true; } /** * 新位置更新 * * @param location */ private void updateWithNewLocation(Location location) { Log.v(LOG_TAG, "updateWithNewLocation"); TextView myLocationText = (TextView) findViewById(R.id.myLocationText); String latLongString = "No Location Found"; if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "Lat: " + lat + "\nLong: " + lng; } // 顯示 myLocationText.setText("Your current Location is:\n" + latLongString); } LocationListener mLocationListener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.v(LOG_TAG, "onStatusChanged"); } @Override public void onProviderEnabled(String provider) { Log.v(LOG_TAG, "onProviderEnabled"); } @Override public void onProviderDisabled(String provider) { Log.v(LOG_TAG, "onProviderDisabled"); } @Override public void onLocationChanged(Location location) { Log.v(LOG_TAG, "onLocationChanged ++" + location.toString()); updateWithNewLocation(location); } }; }
Manifest文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hellogps" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".HelloGPSActivity" android:label="@string/title_activity_hello_gps" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
布局文件:
<?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/myLocationText" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LinearLayout>