Can't receive LocationClient's location updates after screen gone off

本秂侑毒 提交于 2019-12-04 08:05:34

Use WakeLock

in onStartCommand

wl = ((PowerManager)getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, yourTAG);
wl.acquire();

in onDestroy

if (wl != null && wl.isHeld()) {
    wl.release();
}

Update:--

Firstly you need to define LocationClient in your OnCreate() which will call your onConnected() ...

Define you locationClient like this ..

locationClient = new LocationClient(this, this, this);
        locationClient.connect();

Now in your onConnected() .. Just request for Location update like this..

@Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnected(Bundle arg0) {
        src = locationClient.getLastLocation();
        System.out.println("======================location 1==" + src);


        LocationRequest lrequest = new LocationRequest();
        lrequest.setInterval(0);
        lrequest.setSmallestDisplacement(0);
        lrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        locationClient.requestLocationUpdates(lrequest, new LocationListener() {

            @Override
            public void onLocationChanged(Location arg0) {
                 System.out.println("======================location 1233==" +
                 arg0);
                /*Toast.makeText(getApplicationContext(),
                        "Location is 12" + arg0.getLatitude(),
                        Toast.LENGTH_SHORT).show();*/


            }
        });


    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub

    }

*************** BEFORE ************

If your main concern is finding your location update continually.. then U can use undermentioned.

Use this :: ( This will periodically update user's location based on GPS )

For OnCreate()::
----------------------

@Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.speed);


            initui();

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

            if(!provider){
                String text = "ENABLE GPS TO ACCESS SPEEDO METER!";
                Toast.makeText(Speedometer.this, text, Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);

            }

            if (location != null) {

                /*lat = (int) (location.getLatitude() * 1E6);
                longi = (int)(location.getLongitude() * 1E6);*/
                String text = "Got Coordinates";
                Toast.makeText(Speedometer.this, text, Toast.LENGTH_SHORT).show();

            }
            //lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 2, this);
        }


Now use onLocationChanged()
---------------------------


@Override
        public void onLocationChanged(Location loc) {
            // TODO Auto-generated method stub

            float distance = 0;
            //float prevDis = 0;

            try{
            if(prevLoc==null)
            {
                prevLoc = loc;
                //Toast.makeText(MainActivity.this, "PrevLOC" + prevLoc, Toast.LENGTH_SHORT).show();
                Log.i("Main Activity", "Prev LOC" + prevLoc);
            }
            else {
                try{
                Runtime r = Runtime.getRuntime();
                r.gc();
                }catch(Exception e){
                    e.printStackTrace();
                }
                // When prevLoc is not null
                Log.i("Main Activity", "Prev LOC in new LOC BLAH BLAH BLAH" + prevLoc);
                newLoc = loc;
                //Toast.makeText(MainActivity.this, "NewLoc" + newLoc, Toast.LENGTH_SHORT).show();
                Log.i("Main Activity", "New LOC" + newLoc);
                distance = prevLoc.distanceTo(newLoc);
                Log.i("Main Activity", "New DISTANCE DISTANCE DISTANCE DISTANCE DISTANCE DISTANCE " + distance);

                distance = (float) (3.6*distance); 
                speed = distance;
                prevLoc = newLoc;
                Log.i("Main Activity", "New Coordinates set to PrevLoc" + prevLoc);
            }

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


            if(speed <= 160){

                try
                {
                mView.calculateAngleOfDeviation(speed);
                }
                catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }else
            {
                Toast.makeText(Speedometer.this, "CONTROL SPEED", Toast.LENGTH_SHORT).show();
            }

        }

You can also utilize same in your activity ::
-----------------------------------------------

@Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            stopListening();
            super.onPause();
        }
        private void stopListening() {
            // TODO Auto-generated method stub
            try{
            if(lm != null){
                lm.removeUpdates(this);
            }else{
                lm.removeUpdates(this);
                 }
            }catch(Exception e){
                e.printStackTrace();
            }
        }



        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            startListening();
        }

        private void startListening() {
            // TODO Auto-generated method stub
            try{
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            lm.removeUpdates(Speedometer.this);
            //System.exit(0);
            finish();
        } 



Finally there's always a lot of way to do single thing.
Hope it helps... 
Cheers!

I'm not sure where your problem is. I've made a project with your code and it seems to be working as desired here. I received updates even when the screen was off. In any case here are the steps that I took, perhaps your problem isn't in the service, but in how you start it, or somewhere else:

  1. I created a default android project with a single main activity.
  2. I included your code in a separate file named "LocationService"
  3. I added the google play service library to the project.
  4. I added the following permissions to the manifest:

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

and in the application tag I declared the service:

   <service
        android:name=".LocationService"
        android:process=":Location_Service_Test" />

finally I added the following to the onCreate of the main activity:

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

    //Start the service
    Intent service = new Intent(this, LocationService.class);
    startService(service);
} 

If this doesn't solve your problem: please include the rest of your code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!