嗨伙计们,我是 android 新手,在使用 LocationManager.GPS_PROVIDER 创建位置对象时我得到了空值。
这是我的 GPSDemoActivity :
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class GPSDemoActivity extends Activity {
private static final long MINIMUM_DISTANCE = 1;
private static final long MINIMUM_TIME = 1000;
protected LocationManager locationManager;
// private Button showlocationButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// showlocationButton = (Button) findViewById(R.id.btn_showlocation);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MINIMUM_DISTANCE, MINIMUM_TIME, new MyLocationListener());
}
public void showLocation(View v) {
Toast.makeText(this, "Clicked", Toast.LENGTH_LONG).show();
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);//here getting null.
if (location != null) {
String message = String.format(
"Current Location: \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
String message = String.format(
"Your Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(GPSDemoActivity.this, message, Toast.LENGTH_LONG);
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(GPSDemoActivity.this, "Provider disabled by the user. GPS turned off", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(GPSDemoActivity.this,
"Provider disabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(GPSDemoActivity.this,
"Provider status changed", Toast.LENGTH_LONG).show();
}
}
}
在我的 xml 下方。主.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_showlocation"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/btn_showlocation"
android:layout_gravity="center"
android:onClick="showLocation"
/>
</LinearLayout>
这是我的 manifest.xml。我的清单.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".GPSDemoActivity"
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 android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>