Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.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
GPS android请求位置更新_Android_Gps - Fatal编程技术网

GPS android请求位置更新

GPS android请求位置更新,android,gps,Android,Gps,我有一个名为GPSSampleLocation 有一个功能可以打开gps并基于该位置获取信息 这里gpsEnabled将是真的,但在我的手机中,当我请求gps时,手机上方的gps图标将闪烁。。。但它也不闪烁。。。我没有得到数据 public void getGPSLocation() { LocationManager mlocManager = (LocationManager) GPSSampleLocation.this .getSystemService(

我有一个名为
GPSSampleLocation

有一个功能可以打开gps并基于该位置获取信息

这里gpsEnabled将是真的,但在我的手机中,当我请求gps时,手机上方的gps图标将闪烁。。。但它也不闪烁。。。我没有得到数据

public void getGPSLocation() {
    LocationManager mlocManager = (LocationManager) GPSSampleLocation.this
            .getSystemService(Context.LOCATION_SERVICE);
    boolean gpsEnabled = mlocManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (!gpsEnabled) {
        String provider = Settings.Secure.getString(
                getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (!provider.contains("gps")) { // if gps is disabled
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            sendBroadcast(poke);
        }
    }
    gpsEnabled = mlocManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    System.out.println("GPS ENALBLE" + gpsEnabled);
    if (gpsEnabled) {
        mlocManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0,
                GPSSampleLocation.this);
    }
}
我还使用了以下权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

以下是我测试RequestLocationUpdate、LocationManager和LocationListener的代码

package com.test.locationmanager;

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.widget.TextView;

public class LocationManagerStatus extends Activity {
    private LocationManager locationManager;
    private TextView textView;

    private final LocationListener gpsLocationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            switch (status) {
            case LocationProvider.AVAILABLE:
                textView.setText(textView.getText().toString() + "GPS available again\n");
                break;
            case LocationProvider.OUT_OF_SERVICE:
                textView.setText(textView.getText().toString() + "GPS out of service\n");
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                textView.setText(textView.getText().toString() + "GPS temporarily unavailable\n");
                break;
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            textView.setText(textView.getText().toString() + "GPS Provider Enabled\n");
        }

        @Override
        public void onProviderDisabled(String provider) {
            textView.setText(textView.getText().toString() + "GPS Provider Disabled\n");
        }

        @Override
        public void onLocationChanged(Location location) {
            locationManager.removeUpdates(networkLocationListener);

            textView.setText(textView.getText().toString() + "New GPS location: "
                    + String.format("%9.6f", location.getLatitude()) + ", "
                    + String.format("%9.6f", location.getLongitude()) + "\n");

        }
    };

    private final LocationListener networkLocationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            switch (status) {
            case LocationProvider.AVAILABLE:
                textView.setText(textView.getText().toString() + "Network location available again\n");
                break;
            case LocationProvider.OUT_OF_SERVICE:
                textView.setText(textView.getText().toString() + "Network location out of service\n");
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                textView.setText(textView.getText().toString() + "Network location temporarily unavailable\n");
                break;
            }
        }

        @Override
        public void onProviderEnabled(String provider) {
            textView.setText(textView.getText().toString() + "Network Provider Enabled\n");

        }

        @Override
        public void onProviderDisabled(String provider) {
            textView.setText(textView.getText().toString() + "Network Provider Disabled\n");
        }

        @Override
        public void onLocationChanged(Location location) {
            textView.setText(textView.getText().toString() + "New network location: "
                    + String.format("%9.6f", location.getLatitude()) + ", "
                    + String.format("%9.6f", location.getLongitude()) + "\n");

        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textView = (TextView) findViewById(R.id.textview);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    }

    @Override
    protected void onResume() {
        super.onResume();

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, networkLocationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, gpsLocationListener);
    }

    @Override
    protected void onPause() {
        super.onPause();

        locationManager.removeUpdates(networkLocationListener);
        locationManager.removeUpdates(gpsLocationListener);
    }
}

GPS可能无法在建筑物内工作,GPS获取位置的过程很慢。但它应该闪烁!!!它也不会在通知区域顶部闪烁图标…下载并等待,直到您得到修复。然后,您可以尝试查看您的应用程序是否能够获取位置。如果图标不闪烁,请转到Settins->location&Security并选中“使用GPS卫星”复选框。我只能假设您没有足够的GPS信号。请检查其他应用程序是否有效,如谷歌地图。