2

我有一个谜要告诉你!我可以在运行 2.2 (Froyo) 的三星 Galaxy 测试手机上获取以下代码。它不适用于运行 2.3.4 (Gingerbread) 的 HTC Thunderbolt 测试手机。这在收集位置信息时非常简单,但我也实施了这里解释的最佳实践,结果相同: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]
4

3 回答 3

0

Are your testing this while walking around or while sitting at your dev machine with USB attached for logcat output? I suspect that 2.3.4 is being more strict about the second and third params to:

requestLocationUpdates(providers.get(i), 1, 1, mLocationListener)

Those params throttle the rate at which the hardware generates updates. What youre currently using says only give me updates if the location changes by more than a meter AND no more often than every second.

From the Location Manager docs

If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.

Try this to see if it makes a difference. If not, please post your logcat output for more ideas.

requestLocationUpdates(providers.get(i), 0, 0, mLocationListener)
于 2012-03-01T00:03:48.480 回答
0

我在我的安卓平板电脑上遇到了同样的问题,直到我意识到我没有启用谷歌定位服务。一旦我这样做了,onLocationChanged 就开始根据我连接的 WiFi 网络向我提供更新。有关如何启用它的信息,请参阅此帮助文档(大多数 Android 设备上的设置 -> 位置)。

于 2014-08-20T12:38:38.647 回答
0

尝试在onCreate()函数中重用此代码,

List<String> providers = mLocationManager.getAllProviders();
        for (int i = 0 ; i < providers.size(); i++){
            mLocationManager.requestLocationUpdates(providers.get(i), 1, 1, mLocationListener);
        }
于 2012-02-29T22:44:05.590 回答