Code Monkey home page Code Monkey logo

Comments (3)

Kujbor avatar Kujbor commented on September 26, 2024

If someone needs, I added "import android.os.Looper" in the top of file BackgroundServicePlugin.java and then I wrapped the code inside new Runnable() with Looper.prepare() and Looper.loop() like this:

            cordova.getThreadPool().execute(new Runnable() {

                @Override
                public void run() {

                    Looper.prepare();
                    .......
                    .......
                    Looper.loop();
                }
            });

And it worked!

from bgs-core.

Red-Folder avatar Red-Folder commented on September 26, 2024

@Kujbor - Thanks for updating the question with a response.

I had a similar problem with the looper when I wrote a GPS plugin (a lot of people use the BGS for this). Apologies for not being able to help at the time.

For reference, I created a Work In Progress GPS plugin - which while not complete - may help if you need further examples. It can be found here -> https://github.com/Red-Folder/GPS-Service-Plugin

from bgs-core.

veernimbus avatar veernimbus commented on September 26, 2024

hey Kujbor, I am trying to do the same thing but i don't the exact code to put inside Looper.prepare() and Looper.loop(). Can you share with me the exact code that you used.

I tried it in a little different way

try{ // Identify a listener that responds to location updates LocationListener

            LocationListener locationListener = new LocationListener() {
                Handler MAIN_HANDLER = new Handler(Looper.getMainLooper()); 
                public void onLocationChanged(Location location) 
                { 
                    final Location result = location;
                    // Called when a new location is found by the network location provider.                     
                     MAIN_HANDLER.post( new Runnable() { 
                        @Override
                        public void run() {
                           Log.d(TAG, "Location Changed-15");
                           Log.d(TAG, "Latitude : " + Double.toString(result.getLatitude()));
                           Log.d(TAG, "Longitude : " + Double.toString(result.getLongitude()));
                           Log.d(TAG, "Time : " + Long.toString(result.getTime()));
                           Log.d(TAG, "Accuracy : " + Float.toString(result.getAccuracy()));
                        }
                     });
                } 
                public void onStatusChanged(String provider, int status, Bundle extras) {}
                public void onProviderEnabled(String provider) {}
                public void onProviderDisabled(String provider) {} 
            };

            // Register the listener with the Location Manager to receive location updates
            HandlerThread t = new HandlerThread("my handler thread");
            t.start();
            try{
                if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
                 {
                    Log.d(TAG, "Location Fetching from GPS");
                    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener,t.getLooper());      
                 }
                 else
                 {
                    Log.d(TAG, "Location Fetching from Network");
                    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener,t.getLooper());
                 }
             }
             catch (SecurityException e) {
                lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener,t.getLooper());
                 Log.d(TAG, "This is a Security Exception");            
                    e.printStackTrace();
                }
                catch(IllegalArgumentException e){
                lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,  0, 0, locationListener,t.getLooper());
                Log.d(TAG, "This is an Illegal Argument Exception");            
                    e.printStackTrace();
                }                               
        }
        catch(Exception e){
            Log.d(TAG,"An error occured in forced location fetching");
            e.printStackTrace();
        }   

from bgs-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.