我写了一个位置定项目,在模拟器上可以定位,但装到手机上,就不能定位了,请问是怎么回事?我已打开GPS,配置文件件里也配置了代码如下
import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager; String context = Context.LOCATION_SERVICE; locationManager = (LocationManager) getSystemService(context); String provider = LocationManager.GPS_PROVIDER; String provider2 = LocationManager.NETWORK_PROVIDER; Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); } private void updateWithNewLocation(Location location){ String latLongString; TextView myLocationText; myLocationText =(TextView) findViewById(R.id.tv); if(location != null){ double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "Lat: "+lat+"\nLong: "+lng; }else{ latLongString = "No location found"; } myLocationText.setText("Your Current Position is: \n"+ latLongString); } }
配置文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.action" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Main" 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> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>