所以我想为我的手机做一个简单的 Lat Long。我正在使用我自己的固件级别 2.3.6 的手机进行测试。
这是主要代码:
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
public class MainActivity extends FragmentActivity {
private LocationManager locationManager = null;
private LocationListener locationListener = null;
// private LocationListener onLocationChange=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
System.out.println("I got here");
}
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
/*
* System.out.println("This happened"); String message =
* "Longitude:" + loc.getLongitude() + " Latitude: " +
* loc.getLongitude(); Toast.makeText( getBaseContext(),
* "Location changed: Lat: " + loc.getLatitude() + " Lng: " +
* loc.getLongitude(), Toast.LENGTH_SHORT).show();
*/
System.out.println("IN ON LO
CATION CHANGE, lat="
+ loc.getLatitude() + ", lon=" + loc.getLongitude());
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
这是清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cloud.pushpin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.cloud.pushpin.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyArmkmqy3i-wNsVH0f8RH15OsaR4lv4Sp8" />
</application>
<permission
android:name="com.cloud.pushpin.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.cloud.pushpin.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
</manifest>
所以基本上我看到的是当我启动应用程序时没有任何反应。当我摇晃手机时,我收到了很多次“我到了”的消息,但从来没有显示经纬度。我不明白为什么没有发生回调。其他权限供我稍后实现 google maps api 时使用。手机和移动数据都启用了 GPS。