三星Note 2无法达到onLocationChanged()

时间:2015-05-05 14:59:27

标签: android samsung-mobile android-location

适用于除Galaxy Note 2以外的大多数设备。它连接到Google客户端,但无法访问实现onLocationChanged()的{​​{1}}。任何人都知道它是什么原因以及为什么只能在这个设备上?

LocationListener

1 个答案:

答案 0 :(得分:5)

修改:从评论中NullPointerException发生的地方开始,确保mLastLocation不为空。

if (mLastLocation != null){
    address = server + String.valueOf(mLastLocation.getLatitude()) + "&lng=" + String.valueOf(mLastLocation.getLongitude()) + "&distance=" + distance;
} 

需要注意的另一点是,在使用mGoogleApiClient之前,应始终确保if (mGoogleApiClient != null && mGoogleApiClient.isConnected()){ //..... use mGoogleApiClient..... } 不为空且已连接。

private boolean isGooglePlayServicesAvailable() {
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (ConnectionResult.SUCCESS == status) {
            return true;
        } else {
            GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
            return false;
        }
    }

See documentation here

您还应该添加一项检查,以查看Google Play服务是否可用,因为设备上的可用版本有时低于您编译应用的版本。如果是这种情况,可以显示一个对话框。

以下是检查Google Play服务是否可用的方法。

getLastLocation()

请注意,getLastLocation()很有可能返回null,因此如果从第一次调用LocationListener获得空值,则可以注册位置侦听器。 请参阅此帖子:LocationClient getLastLocation() return null

以下是如何注册LocationCallback mFusedLocationCallback = new LocationCallback();

的指南

创建一个监听器:

private class LocationCallback implements LocationListener {

        public LocationCallback() {

        }

        @Override
        public void onLocationChanged(Location location) {

                 mLastLocation = location;
                 lat = String.valueOf(mLastLocation.getLatitude());
                 lng = String.valueOf(mLastLocation.getLongitude());


             }
    };

班级定义:

LocationListener

然后只需注册 mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(minTime); mLocationRequest.setFastestInterval(fastestTime); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); mLocationRequest.setSmallestDisplacement(distanceThreshold); LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mFusedLocationCallback);

/**
 * Runs when a GoogleApiClient object successfully connects.
 */
@Override
public void onConnected(Bundle connectionHint) {
    // Provides a simple way of getting a device's location and is well suited for
    // applications that do not require a fine-grained location and that do not need location
    // updates. Gets the best and most recent location currently available, which may be null
    // in rare cases when a location is not available.
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        lat = String.valueOf(mLastLocation.getLatitude());
        lng = String.valueOf(mLastLocation.getLongitude());
    } else {
        Toast.makeText(this, R.string.no_location_detected, Toast.LENGTH_LONG).show();
    }


     mLocationRequest = new LocationRequest();
     mLocationRequest.setInterval(1000);
     mLocationRequest.setFastestInterval(500);
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mFusedLocationCallback);
}

编辑:您应该在注册位置回调之前等待API连接,它应该是这样的:

application

文档: for requestLocationUpdates....LocationRequest

最后一件事,请确保在 <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 标记内的AndroidManifest.xml中有此内容:

import java.util.*;
public class pooped
{
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) 
    {
        int day;
        System.out.println(" Days of the week are numbered 1-7"+
                    "From Sunday to Saturday, enter a number now");
        day = console.nextInt();

        System.out.println(isWeek(day));
        printday(day);
    }

    public static boolean isWeek(int day)
    {
        return day >= 0 && day <= 7;
    }

    static Scanner console = new Scanner(System.in);

    public static void addDay()
    {
        int date;
        System.out.println("Enter how many days you want to go forward.");
        date = console.nextInt();

        if (int date > 0)
        {
            int day = date + 1;
        }
    }

    public static void printday(int day) 
    {
        switch (day) {

        case 1:
            System.out.println("Sunday");
        break;

        case 2:
            System.out.println("Monday");
        break;

        case 3:
            System.out.println("Tuesday");
        break;

        case 4:
            System.out.println("Wednesday");
        break;

        case 5:
            System.out.println("Thursday");
        break;

        case 6:
            System.out.println("Friday");
        break;

        case 7:
            System.out.println("Saturday");

        default:
        break;
        }
    }
}