我对你来说是个谜!我可以在下面的代码上运行运行2.2(Froyo)的Samsung Galaxy测试手机。在运行2.3.4(Gingerbread)的HTC Thunderbolt测试手机上,该功能无法正常工作。这几乎与收集位置信息一样简单,但是我还实现了here解释的最佳实践,并得到了相同的结果:Froyo走了,Gingerbread死了。
为了解决这个问题,我应该尝试些什么?是我的代码吗?是硬件,软件吗?权限?我是否激怒了计算机科学之神?我会接受给出解决方案的建议的第一个答案。谢谢您的帮助!
我的活动:
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class SimpleLocationProjectActivity extends Activity {
private LocationManager mLocationManager;
private MyLocationListener mLocationListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationListener = new MyLocationListener();
}
@Override
protected void onPause() {
mLocationManager.removeUpdates(mLocationListener);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
Log.i("SIMPLE APP", "Starting location services.");
List<String> providers = mLocationManager.getAllProviders();
for (int i = 0 ; i < providers.size(); i++){
mLocationManager.requestLocationUpdates(providers.get(i), 1, 1, mLocationListener);
}
}
public class MyLocationListener implements LocationListener{
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
String statusStr;
switch (status){
case LocationProvider.OUT_OF_SERVICE:
statusStr = "Out of Service";
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
statusStr = "Temporarily unavailable";
break;
case LocationProvider.AVAILABLE:
statusStr = "Available";
break;
default:
statusStr = "Huhhh??";
}
Log.i("SIMPLE APP", "Provider: " + provider + " Status is: " + statusStr);
}
@Override
public void onProviderEnabled(String provider) {
Log.i("SIMPLE APP", "Provider: " + provider + " is enabled.");
}
@Override
public void onProviderDisabled(String provider) {
Log.i("SIMPLE APP", "Provider: " + provider + " is disabled.");
}
@Override
public void onLocationChanged(Location location) {
double latitute = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(SimpleLocationProjectActivity.this, "Lat is " + latitute + " and lng is " + longitude, Toast.LENGTH_SHORT).show();
//This never gets called on my HTC Thunderbolt...
Log.i("SIMPLE APP", "Provider: " + location.getProvider() + " Lat is " + latitute + " and lng is " + longitude);
}
}
}
我的清单:
<?xml version="1.0" encoding="UTF-8"?>
<manifest android:versionCode="1" android:versionName="1.0"
package="com.silithcrowe.simplelocationproject" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk android:minSdkVersion="8"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".SimpleLocationProjectActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
编辑:运行时logcat的示例:
03-01 11:37:43.529: D/RPC(1662): read RPC packet
03-01 11:37:43.529: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.529: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.529: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.529: D/RPC(1662): read RPC packet
03-01 11:37:43.529: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.529: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.529: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.529: D/RPC(1662): read RPC packet
03-01 11:37:43.529: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.529: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.529: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.529: D/RPC(1662): read RPC packet
03-01 11:37:43.529: D/RPC(1662): read RPC packet size: [96]
03-01 11:37:43.529: V/locapi_rpc_glue(1662): Callback received: 2 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.529: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.529: D/RPC(1662): read RPC packet
03-01 11:37:43.529: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.529: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.529: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.539: D/RPC(1662): read RPC packet
03-01 11:37:43.539: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.539: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.539: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.539: D/RPC(1662): read RPC packet
03-01 11:37:43.539: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.539: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.539: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.539: D/RPC(1662): read RPC packet
03-01 11:37:43.539: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:43.539: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:43.539: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:43.559: D/lib_locapi(1662): loc_eng_report_sv: valid_mask = 0x30, num of sv = 0
03-01 11:37:43.559: D/GpsLocationProvider(1662): reportSvStatus
03-01 11:37:44.410: D/RPC(1662): read RPC packet
03-01 11:37:44.410: D/RPC(1662): read RPC packet size: [208]
03-01 11:37:44.410: V/locapi_rpc_glue(1662): Callback received: 1 (cb_id=0x67E0001 handle=2)
03-01 11:37:44.410: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:44.410: D/lib_locapi(1662): loc_eng_report_position: valid mask = 0x61ed, sess status = 1
03-01 11:37:44.520: D/RPC(1662): read RPC packet
03-01 11:37:44.520: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:44.520: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:44.520: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:44.520: D/RPC(1662): read RPC packet
03-01 11:37:44.520: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:44.520: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:44.520: D/RPC(1662): written RPC packet size: [28]
03-01 11:37:44.520: D/RPC(1662): read RPC packet
03-01 11:37:44.520: D/RPC(1662): read RPC packet size: [272]
03-01 11:37:44.520: V/locapi_rpc_glue(1662): Callback received: 4 (cb_id=0x67E0001 handle=2)
03-01 11:37:44.520: D/RPC(1662): written RPC packet size: [28]
请求位置更新时的logcat示例:
I/SIMPLE APP(12524): Starting location services.
D/AK8975 ( 1544): Ignore M_Sensor: (y, p, r) = ( 293, 1, 2), Status = 2
D/GpsLocationProvider( 1662): [handleMessage] message :8
D/GpsLocationProvider( 1662): [handleMessage] ADD_LISTENER
D/GpsLocationProvider( 1662): addListener(com.seedlabs.simplelocationproject)
D/GpsLocationProvider( 1662): setMinTime 0
V/GpsLocationProvider( 1662): enableLocationTracking ++
D/GpsLocationProvider( 1662): [handleMessage] message :3
D/GpsLocationProvider( 1662): [handleMessage] ENABLE_TRACKING
D/GpsLocationProvider( 1662): startNavigating
D/GpsLocationProvider( 1662): HtcBuildFlag.HtcCIQFlag(FALSE)
V/LocationManagerService( 1662): requestLocationUpdates
V/LocationManagerService( 1662): requestLocationUpdatesLocked: updateProvidersLocked
D/LocationManagerService( 1662): [updateProvidersLocked]
V/LocationManagerService( 1662): requestLocationUpdates
V/LocationManagerService( 1662): requestLocationUpdatesLocked: updateProvidersLocked
D/LocationManagerService( 1662): [updateProvidersLocked]
V/LocationManagerService( 1662): requestLocationUpdates
V/LocationManagerService( 1662): requestLocationUpdatesLocked: updateProvidersLocked
D/LocationManagerService( 1662): [updateProvidersLocked]
D/lib_locapi( 1662): [GPS:] loc_eng_set_qos_time_out(standalone = 16000, agps = 16000)
V/GpsLocationProvider( 1662): set_agps_qos_time_out complete
D/GpsLocationProvider( 1662): GpsLocationProvider_set_position_mode
D/lib_locapi( 1662): [GPS:] loc_eng_set_position mode, client = 2, interval = 1000, mode = 0
D/lib_locapi( 1662): [GPS:] loc_eng_set_position mode: GPS_POSITION_MODE_STANDALONE
D/lib_locapi( 1662): [GPS:] loc_eng_set_position mode before IOCTL, interval=1000, mode =4 preferred_accuracy=50, qos_timeout_standalone=16000, recurrence_type=1
V/lib_locapi( 1662): loc_eng_ioctl called: client = 2, ioctl_type = 2
V/locapi_rpc_glue( 1662): loc_ioctl
D/RPC ( 1662): written RPC packet size: [96]
最佳答案
尝试在onCreate()
函数中重用此代码,
List<String> providers = mLocationManager.getAllProviders();
for (int i = 0 ; i < providers.size(); i++){
mLocationManager.requestLocationUpdates(providers.get(i), 1, 1, mLocationListener);
}