标签: android-location

使用IntentService进行位置监听,但onHandleIntent后立即调用onDestroy

我正在使用IntentService来监听GPS位置更新,以便在用户离开活动时运行.IS将消息中的Location发送给活动的处理程序.

问题是,当用户点击"停止跟踪"按钮时,状态栏通知"跟踪..."仍然存在,因为它是在IntentService中创建的.所以我凌驾于onDestory()所以我可以打电话:

mNotificationManager.cancel(TRACKING);
Run Code Online (Sandbox Code Playgroud)

但问题是,当我实现onDestroy()时,它会在onHandleIntent之后立即调用.如果我删除我的覆盖onDestroy()代码,那么它不会立即被调用并且运行正常,将位置发送回活动以在屏幕上显示.

任何想法为什么onDestroy()会立即调用,如果我实现它,但不是,如果我不?

我在这里很困惑.

谢谢大家,

Infinitifizz

android android-intent android-service android-location android-lifecycle

6
推荐指数
1
解决办法
1141
查看次数

onLocationChanged仅调用一次,不刷新

由于某种原因,我制作了一个小小的gps应用程序onLocationChanged不刷新,它在应用程序启动时仅运行一次。

这是我的代码:

public BackgroundLocationService() {
        super("myintentservice");

        locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        Criteria crt = new Criteria();
        crt.setAccuracy(Criteria.ACCURACY_FINE);
        String bestProvider = locManager.getBestProvider(crt, true);

        boolean gps_enabled;
        boolean network_enabled;
        boolean best_enabled;

        gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        best_enabled = locManager.isProviderEnabled(bestProvider);


        if (best_enabled) {
            locManager.requestLocationUpdates(bestProvider, 15000, 20, locListener);
            Log.i(TAG, "Best enabled: " + bestProvider);
        } else {
            Log.i("Location Provider: ", "best not enabled: " + bestProvider);
            if (gps_enabled) {
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
                Log.i(TAG, "gps enabled!");
            }
            if (network_enabled) {
                locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
                Log.i(TAG, …
Run Code Online (Sandbox Code Playgroud)

android android-location

6
推荐指数
1
解决办法
2367
查看次数

为结果启动设置活动

在我的应用程序中,我正在检查用户设备上是否启用了GPS,如果没有,我想将他发送到设置以让他打开它.

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, LocationHelper.LOCATION_SETTINGS_REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)

用户关闭"设置"屏幕后,我会在内部执行操作onActivityResult().

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == LocationHelper.LOCATION_SETTINGS_REQUEST_CODE) {
        LogUtils.d("onActivityResult from settings");
        fetchCurrentLocation();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,onActivityResult()不会被调用.我做错了什么或这种做法一般不起作用?提前致谢.

android android-intent android-location

6
推荐指数
2
解决办法
8659
查看次数

当屏幕指示灯熄灭时,LocationClient不会回调,但我的WakefulThread正如预期的那样完美运行

为了在后台检索融合位置,我创建了一个与Commonsguy创建的cwac -locpoll库非常相似的库.

在PollerThread内部,我正在尝试使用连接,请求和检索位置LocationClient.

我可以通过接收onConnected方法的回调来连接,但是我无法在方法上获得回调.因此onLocationChanged我的onTimeout线程按照确定的间隔执行.

注意: 此问题仅在屏幕指示灯熄灭时发生.否则它完全正常.

我怀疑新的位置Api可能存在错误.

这是我的实施PollerThread,

          private class PollerThread extends WakefulThread  implements GooglePlayServicesClient.ConnectionCallbacks,
      GooglePlayServicesClient.OnConnectionFailedListener,LocationListener{

            private static final String TAG = "PollerThread";

            //context
            private Context mContext=null;
            private LocationClient mLocationClient=null;
            private LocationRequest mLocationRequest=null;
            private LocationManager locMgr=null;
            private Intent intentTemplate=null;
            private Handler handler=new Handler();
            private Runnable onTimeout = new Runnable() {

            @Override
            public void run() {

            Log.e(TAG, "onTimeout");

            //prepare broadcast intent
              Intent toBroadcast=new Intent(intentTemplate);
              toBroadcast.putExtra(FusedPoller.EXTRA_ERROR, "Timeout!"); …
Run Code Online (Sandbox Code Playgroud)

android locationlistener android-location

6
推荐指数
1
解决办法
6787
查看次数

Android在等待位置时显示进度对话框

我正在使用此示例开发基于位置的应用程序:http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

但是当我打开手机时,该位置就不可用了.所以我想在等待位置时显示进度对话框.想在后台使用AsyncTask执行此操作.

您能否提出任何想法以及如何做到这一点?

android android-location

6
推荐指数
1
解决办法
6382
查看次数

如何在Android中使用Wifi获取位置?

我希望通过Wifi获取位置并在谷歌地图中工作,这对我不起作用,但Gps没问题,也没问题.

我的代码:

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

if (locationManager != null) {
    boolean gpsIsEnabled = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean networkIsEnabled = locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (gpsIsEnabled) {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 5000L, 10F,
                (android.location.LocationListener) this);
    } else if (networkIsEnabled) {
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 5000L, 10F,
                (android.location.LocationListener) this);
    } else {
        // Show an error dialog that GPS is disabled...
    }
} else {
    // Show some generic error dialog because something must have gone
    // wrong with location manager.
}
Run Code Online (Sandbox Code Playgroud)

AndroidManifest:

<uses-library android:name="com.google.android.maps" />

<meta-data
    android:name="com.google.android.maps.v2.API_KEY" …
Run Code Online (Sandbox Code Playgroud)

java android android-location location-provider

6
推荐指数
2
解决办法
2万
查看次数

定期获取位置(坐标),而不会显着增加电池消耗

我正在开发一个Android应用程序; 此应用程序需要定期(每10分钟)将当前位置(共存)发送到Web服务.但是......对于更正确的方式(对设备电池更友好)我有点困惑.

我读了这个答案,她的方法_getLocation()很好看; 但我不知道这种方法是否可以获得我需要的位置; 总可用性......

我想,如果使用GSM/WIFI无法使用该位置,应用程序会选择GPS方法.

是什么使这种方法?

private void _getLocation() {
    // Get the location manager
    LocationManager locationManager = (LocationManager) 
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
    } catch (NullPointerException e) {
        lat = -1.0;
        lon = -1.0;
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都知道一种方法来定期获取设备的坐标...而不会大幅增加电池消耗?

android locationmanager android-location google-play-services battery-saver

6
推荐指数
1
解决办法
4298
查看次数

三星Note 2无法达到onLocationChanged()

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

@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;

    if (mLastLocation != null) {
        lat = mLastLocation.getLatitude();
        lng = mLastLocation.getLongitude();

        Toast.makeText(getApplicationContext(), String.valueOf(lat) + "/" + String.valueOf(lng), Toast.LENGTH_LONG).show();

        serverUrl = "http://(my server)/offers?lat=" + String.valueOf(mLastLocation.getLatitude())
                            + "&lng=" + String.valueOf(mLastLocation.getLongitude()) + "&distance=1";
        // save
        makeTag(serverUrl);

        // after getting location data - unregister listener
                    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mFusedLocationCallback);
        new GetBackgroundUpdate().execute();
    } else {
        // get data from server and update GridView
        new GetBackgroundUpdate().execute();
        Toast.makeText(getApplicationContext(), R.string.no_location_detected, Toast.LENGTH_LONG).show();

     }
/**
Location …
Run Code Online (Sandbox Code Playgroud)

android android-location samsung-mobile

6
推荐指数
1
解决办法
392
查看次数

Android Google Places API - PlaceAutocompleteFragment清除按钮监听器

我在我的项目中使用Google Places API -我的项目中的PlaceAutocompleteFragment API来搜索位置,当用户选择位置然后使用位置lat-long获取记录并将其显示到ListView.

现在问题是每当用户点击PlaceAutocompleteFragment的清除按钮(X)时,我想要清除ListView项目,我需要听清楚按钮onClick吗?怎么做?

在此输入图像描述
任何帮助将不胜感激.

autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(Place place) {
        LatLng latLng=place.getLatLng();
        //hit web service, response parsing and add it to listview.
    }

    @Override
    public void onError(Status status) {
        Log.i(TAG, "An error occurred: " + status);
    }
});
Run Code Online (Sandbox Code Playgroud)

android android-location android-fragments google-places google-maps-android-api-2

6
推荐指数
1
解决办法
5019
查看次数

android:location api要求wifi

这很奇怪.我的意思是,我有强大的网络,GPS打开,仍然要求Wifi!

我在课堂上使用新的位置Api FusedLocationProviderClient.

这是我检查位置设置是否启用的方式:

private void checkIfLocationSettingsAreEnabled() {
    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    locationRequest.setInterval(10000);
    locationRequest.setFastestInterval(5000);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);

    SettingsClient client = LocationServices.getSettingsClient(context);
    Task<LocationSettingsResponse> locationSettingsResponseTask = client.checkLocationSettings(builder.build());
    locationSettingsResponseTask.addOnSuccessListener(locationSettingsResponse -> {
        getLastKnownLocation();
    });
    locationSettingsResponseTask.addOnFailureListener(e -> {
        if (e instanceof ResolvableApiException) {
            myLocationListener.onResolutionNeeded(e);
        } else {
            myLocationListener.onLocationFailure(e);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

通过上面的代码,我可以得到以下图像:

在此输入图像描述

但是,在点击"确定"之后,我再次打电话给checkIfSettingsAreEnabled()这个显示另一个弹出窗口,如下所示:

在此输入图像描述

我想知道为什么启用Wifi是必须的,即使我在沙漠野生动物园,没有无线连接!

有没有办法跳过Wifi选项,并像谷歌地图一样正常工作?

事实上,谷歌地图使用优先级PRIORITY_HIGH_ACCURACY仍然一次,第一次设置对话框已经显示,它不会要求第二次打开Wifi.

android android-location

6
推荐指数
1
解决办法
446
查看次数