在服务中注册的LocationListener永远不会收到onLocationChanged

问题描述

当我在MainActivity中注册LocationListener时,一切正常,但是当我尝试在服务中注册同一LocationListener时,虽然注册成功,但没有任何反应 这是我在MainActivity中注册LocationListener的方式,服务中的相同代码不起作用

    public LocationManager locationManager;
    public MyLocationListener mylocationListener;
    final String LOG_TAG = "MyLogsMainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mylocationListener = new MyLocationListener();

        try {
            Context context = getApplicationContext();
            locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            Log.d(LOG_TAG,"getSystemService(Context.LOCATION_SERVICE)" + getSystemService(Context.LOCATION_SERVICE).toString());
          
            if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,mylocationListener);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,mylocationListener);
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,mylocationListener);
            // getting GPS status
            boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            // getting network status
            boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            Log.d(LOG_TAG,"isGPSEnabled     " + isGPSEnabled);
            Log.d(LOG_TAG,"isNetworkEnabled " + isNetworkEnabled);
        } catch (Exception e) {
            Log.d(LOG_TAG,"catch exception " + e.getMessage());
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)