Android上的GPS程序报错:Fail to find provider info for com.google.android.gsf.gservices
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>