【问题标题】:LocationListener is not working (inside intentService) but getLastKnownLocation does (return different gps coordinates)LocationListener 不工作(intentService 内部),但 getLastKnownLocation 工作(返回不同的 gps 坐标)
【发布时间】:2016-08-03 00:07:08
【问题描述】:

我没有输入 onLocationChanged,onStatusChanged,onProviderEnabled,onProviderDisabled 我检查了关闭并打开gps 我在办公室外面试过(在这个例子中你看到的那个叫 contador 的柜台)

这是我的代码

import android.Manifest;
import android.app.IntentService;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;


/**
 * An {@link IntentService} subclass for handling asynchronous task requests in
 * a service on a separate handler thread.
 * <p>
 * TODO: Customize class - update intent actions, extra parameters and static
 * helper methods.
 */
public class ISSubProcesoGps extends IntentService {
    // TODO: Rename actions, choose action names that describe tasks that this
    // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS

    public static int hacambiado=0;

    LocationManager lm;

    double longitude;
    double latitude;

    Location location=null;

    public static volatile boolean shouldContinue = true;
    int contador = 0;


    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
            //mLocation = location;
            //altitude = location.getAltitude();
            hacambiado++;
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
String x=s;
        }

        @Override
        public void onProviderEnabled(String s) {
            String x=s;
        }

        @Override
        public void onProviderDisabled(String s) {
            String x=s;
        }
    };


    // TODO: Rename parameters


    public ISSubProcesoGps() {

        super("ISSubProcesoGps");


    }

    /**
     * Starts this service to perform action Foo with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see IntentService
     */


    @Override
    protected void onHandleIntent(Intent mIntent) {

        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);



        latitude = 0;
        longitude = 0;



        if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);


            Intent intent = new Intent(getBaseContext(), MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);

            Notification notification = new Notification.Builder(this)
                    .setSmallIcon(R.drawable.ic_menu_gallery)  // the status icon
                    .setTicker("hola muy myintentservice")  // the status text
                    .setWhen(System.currentTimeMillis())  // the time stamp
                    .setContentTitle("content title")  // the label of the entry
                    .setContentText("content teext")  // the contents of the entry
                    .setContentIntent(pendIntent)  // The intent to send when the entry is clicked
                    .build();

            startForeground(1, notification);

            while(true)
            {
                try {
                    location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    Thread.sleep(1000);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(!shouldContinue)
                {
                    if(lm!=null)
                        lm.removeUpdates(locationListener);
                    lm=null;
                    break;
                }
            }


        }
    }

}

【问题讨论】:

    标签: android gps location locationmanager locationlistener


    【解决方案1】:

    您的 Intent 服务在从其位置侦听器获得正确读数之前已从内存中删除。这是由于 Intent 服务在调用 onHandleIntent(Intent intent) 方法后被销毁。尝试使用 Service 不断监听位置更新,并根据需要使用单独的 Intent Service 检索这些更新。

    这是一个探索其他用户如何做到这一点的链接。 Location listener works from a Service but not an IntentService

    在收到其他用户的提示和反馈后,请参阅“Edit3”以了解该用户的解决方案。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 2017-07-15
      相关资源
      最近更新 更多