Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么不是';三星手机上的gps定位技术?_Android - Fatal编程技术网

Android 为什么不是';三星手机上的gps定位技术?

Android 为什么不是';三星手机上的gps定位技术?,android,Android,我做了一个程序来获取用户的gps位置,它在一些手机上工作正常,但在三星S4(i9505)上不工作 我正在运行服务和扩展LocationListener。代码如下: public class MainService extends Service implements LocationListener { public double latitude; public double longitude; LocationManag

我做了一个程序来获取用户的gps位置,它在一些手机上工作正常,但在三星S4(i9505)上不工作

我正在运行服务和扩展LocationListener。代码如下:

     public class MainService extends Service implements LocationListener
    { 
        public double latitude;
        public double longitude;

        LocationManager mlocManager=null;
        LocationListener mlocListener;

        Timer timer;

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

            mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            mlocListener = this;
            mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

            if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
            {
                final String provider = LocationManager.GPS_PROVIDER;
                 // start new timer thread
                TimerTask task = new TimerTask() 
                {   
                    @Override
                    public void run() 
                    {
                        Location timerLoc = mlocManager.getLastKnownLocation(provider);

                        getFrndLocCloudAndSaveInDB();

                        SimpleDateFormat formata = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

                        String currentDateandTime = formata.format(new Date());


                         if(latitude>0 & longitude>0)
                       {locObj = new LocModel(myName, String.valueOf(latitude), String.valueOf(longitude), currentDateandTime);

                        Log.d("ccrc","my Loc:::"+ String.valueOf(timerLoc.getLatitude()) + "  --|--  " + String.valueOf(timerLoc.getLongitude()) + "at" + currentDateandTime);}

                        i=i+1;
                        Log.d("ccrc","from timer--"+ i);

                    }
                };

                timer = new Timer(true);
                timer.scheduleAtFixedRate(task, 5000, 10000); 


            }

         else {

           }

          return super.onStartCommand(intent, flags, startId);

        }


         @Override
        public void onLocationChanged(Location loc) 
        {
            this.latitude=loc.getLatitude();
            this.longitude=loc.getLongitude();
            Log.i("latusrvc", String.valueOf(latitude));

        }

}

在ccrc标签的日志中,我在其他手机上得到了结果,但在三星S4中没有显示经度和纬度。加上GPS是开着的-我也检查过了。真正的问题是什么?

我使用这个类通过
GPS
方法
canGetLocation()
获取当前位置的纬度和经度。如果
GPS
未启用,我们可以通过方法
showSettingsAlert
显示
Dailog
来请求激活手机的
GPS

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 2; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 5 * 1; // 1 minute
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener Calling this function will stop using GPS in your
 * app
 * */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

/**
 * Function to get latitude
 * */
public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * 
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog On pressing Settings button will
 * lauch Settings Options
 * */
public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    alertDialog.setTitle("GPS is settings");
    alertDialog
            .setMessage("GPS is not enabled. Do you want to go to settings menu?");
    alertDialog.setPositiveButton("Settings",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(
                            Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    mContext.startActivity(intent);
                }
            });
    alertDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

}

这不应该发生。但如果你说我可以给你一个很好的获取纬度和经度的代码。@Anshul Tyagi:那太好了!首先,一定要检查您正在尝试的手机中的LocationServices是否正常工作。试着打开谷歌地图,看看它是否能获得一个补丁(有时候需要几分钟才能得到第一个补丁)。@Angel Koh:我已经检查过了。谷歌地图可以工作,而且我在同一个地方用三星和另一部手机查看了位置。谢谢,这很有帮助。但代码是一样的。那么,为什么我的不能工作呢?奇怪的Netwrok提供商的另一个问题我不必启用sim卡的数据连接吗?我的意思是网络提供商将从我的运营商那里获取数据,所以我必须启用sim卡的数据连接——在这种情况下,WIFI无法工作。对吧?你的代码没什么不同。我现在无法运行您的代码。这就是为什么我给你一个运行代码来继续工作。不,你不需要Sim卡的数据连接,因为我们可以读取有效Sim卡的一些信息。