android - 位置监听器在 Debug模式下工作但不在编译的 APK 中

标签 android eclipse geolocation

我有一个奇怪的问题,我在服务中配置了一个位置监听器(Google Play 服务融合位置)。我使用挂起的 Intent 方法(推荐用于服务)。

问题是,当我使用 Eclipse 进行调试时,一切正常,我会收到定期的位置更新。但是当我编译 apk 并运行它时,行为发生变化并且位置更新仅被调用一次。

这是简化的代码:

public class TrackingService extends Service implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener {

        //fields

    private PendingIntent locPendingIntent;

    // ====== CONFIGURATION ========

    BroadcastReceiver locationReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // Do this when the system sends the intent
            if (intent.getAction().equals(AppManager.ACTION_LOCATION_READY)) {

                // Called only once on compiled apk. Working fine when debugging


                Bundle b = intent.getExtras();
                Location loc = (Location) b
                        .get(LocationClient.KEY_LOCATION_CHANGED);


                if(loc != null) {

                    onLocationChanged(loc); 
                }
            }

        }
    };

    /*
     * Create a new location client, using the enclosing class to handle
     * callbacks.
     */
    private void setUpLocationClientIfNeeded() {
        if (mLocationClient == null)
            mLocationClient = new LocationClient(this, this, this);
    }

    // ======== LIFE CYCLE ========

    @Override
    public void onCreate() {
        super.onCreate();

        mInProgress = false;
        // Create the LocationRequest object
        mLocationRequest = LocationRequest.create();
        mLocationRequest
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        // Set the update interval to 5 seconds
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        // Set the fastest update interval to 2.5 second
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);


        setUpLocationClientIfNeeded();

        registerReceiver(locationReceiver, new IntentFilter(AppManager.ACTION_LOCATION_READY));
    }

    public int onStartCommand(Intent intent, int flags, int startId) {

        super.onStartCommand(intent, flags, startId);

        if (mLocationClient.isConnected() || mInProgress)
            return START_STICKY;

        setUpLocationClientIfNeeded();

        if (!mLocationClient.isConnected() || !mLocationClient.isConnecting()
                && !mInProgress) {
            mInProgress = true;
            mLocationClient.connect();
        }
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }


    // ===== GOOGLE PLAY SERVICES CALLBACKS ======

    /*
     * Called by Location Services when the request to connect the client
     * finishes successfully. At this point, you can request the current
     * location or start periodic updates
     */
    public void onConnected(Bundle bundle) {

        startListeningForLocations();

    }

    /*
     * Called by Location Services if the connection to the location client
     * drops because of an error.
     */
    public void onDisconnected() {
        // Turn off the request flag
        mInProgress = false;
        // Destroy the current location client
        mLocationClient = null;

    }

    /*
     * Called by Location Services if the attempt to Location Services fails.
     */
    public void onConnectionFailed(ConnectionResult connectionResult) {
        mInProgress = false;
        /*
         * Google Play services can resolve some errors it detects. If the error
         * has a resolution, try sending an Intent to start a Google Play
         * services activity that can resolve error.
         */
        if (connectionResult.hasResolution()) {

        } else {
            // If no resolution is available, display an error dialog

        }
    }


    private void startListeningForLocations() {

        Intent intent = new Intent(AppManager.ACTION_LOCATION_READY);
        locPendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        mLocationClient.requestLocationUpdates(mLocationRequest,
                locPendingIntent);

    }

    // Define the callback method that receives location updates
    public void onLocationChanged(Location location) {

        // process with location

    }



}

list 权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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_MOCK_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

如果有人需要更多详细信息,请问我。 非常感谢。

最佳答案

我遇到了同样的问题,但我想我可以解决它。 在我的代码中,我有一个 Activity 和一个服务。我正在从服务中调用 Location Client。在 Debug模式下它工作正常,但是当我构建发布 APK 时,它不起作用。

我创建了一个调用 Location Client 的方法,而不是从服务调用,我从 Activity 中调用它(在 onServiceConnected 方法的末尾)并且它起作用了。

希望对你有用。

最好的问候, 莱昂纳多。

关于android - 位置监听器在 Debug模式下工作但不在编译的 APK 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20997699/

相关文章:

android - 检查 USB 连接或不在按钮中单击 android

android - 是否可以在 Android 中检测到被拒绝的调用?

android - Media Player同步播放音频和视频

eclipse - Eclipse 如何正确识别 MPI 工件?

eclipse - 如何仅编译 gwt 模块的更改部分?

c - 尝试进入 for 循环时出现段错误(核心转储)

ios - 检查用户是否在定义的区域内

android - 在 Android 上获取用户当前位置的最简单、最可靠的方法是什么?

javascript - 如何在 Javascript 中从图像文件数据读取地理位置

android - 如何实现惰性列一次滚动一列的功能