Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by getmetotheforums · Jan 16, 2015 at 04:19 PM · androidpluginlocation

Android onLocationChange callback not? triggering

I have created a Unity plugin to utilize Android's location services. For all intents and purposes it should be working. I subscribe listeners to location updates, but the LocationListeners' onChangeLocation callback never gets triggered. Recently, I noticed this print out from unity in logcat: Unknown event structure (2). I believe that this is the LocationManager attempting to notify the LocationListener of a location change, but obviously there is some issue. I've searched the net for an answer to this problem, but as the log file gives me little to no information, I was unable to find much information. I've added permissions to the manifest file. Below is the initialization and registration code for the listeners.

Initialization:

 public static void Init(Context context)
 {
     if(_looper == null)
     {
         Looper.prepare();
         _looper = Looper.myLooper();
     }
     Log.d("LocationServices", "Initializing Location Services");
     try
     {
         _locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
 
         _netProviderIsEnabled = _locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
         _gpsProviderIsEnabled = _locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
 
         Log.d("LocationServices", "NetworkProviderEnabled: " + _netProviderIsEnabled);
         Log.d("LocationServices", "GPSProviderEnabled:" + _gpsProviderIsEnabled);
 
         _netLocationListener = new LocationListener()
         {
             public void onLocationChanged(Location location)
             {
                 Log.d("LocationServices", "got a new location");
                 makeUseOfNewLocation(location);
             }
 
             public void onStatusChanged(String provider, int status, Bundle extras) 
             {
                 Log.d("LocationServices", "NetStatus: " + status);
                 Map<String, String> statusMap = new HashMap<String, String>();
 
                 statusMap.put("Provider", provider);
                 statusMap.put("Status", "" + status);
 
                 JSONObject json = new JSONObject(statusMap);
 
                 //UnityPlayer.UnitySendMessage("RunView", "GetProviderStatus", json.toString());
             }
 
             public void onProviderEnabled(String provider) 
             {
                 _netProviderIsEnabled = true;
                 _locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, _netLocationListener);
             }
 
             public void onProviderDisabled(String provider) 
             {
                 _netProviderIsEnabled = false;
                 _locationManager.removeUpdates(_netLocationListener);
             }
         };
 
         _gpsLocationListener = new LocationListener()
         {
             public void onLocationChanged(Location location)
             {
                 Log.d("LocationServices", "got a new location");
                 makeUseOfNewLocation(location);
             }
 
             public void onStatusChanged(String provider, int status, Bundle extras) 
             {
                 Log.d("LocationServices", "GPSStatus: " + status);
                 Map<String, String> statusMap = new HashMap<String, String>();
 
                 statusMap.put("Provider", provider);
                 statusMap.put("Status", "" + status);
 
                 JSONObject json = new JSONObject(statusMap);
 
                 //UnityPlayer.UnitySendMessage("RunView", "GetProviderStatus", json.toString());
             }
 
             public void onProviderEnabled(String provider) 
             {
                 _gpsProviderIsEnabled = true;
                 _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, _gpsLocationListener);
             }
 
             public void onProviderDisabled(String provider) 
             {
                 _gpsProviderIsEnabled = false;
                 _locationManager.removeUpdates(_gpsLocationListener);
             }
         };
     }
     catch(Exception ex)
     {
         Log.d("LocationServices", "Error: " + ex.getMessage());
     }
     _isTracking = false;
 }

Registration:

 public static void StartTracking()
 {
     if(_isTracking)
         return;
 
     Log.d("LocationServices", "Starting Location Services");
     try
     {
         if(_netProviderIsEnabled)
             _locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, _netLocationListener);
         /*else
             UnityPlayer.UnitySendMessage("RunView", "GetNetProviderStatus", "False");*/
 
         if(_gpsProviderIsEnabled)
             _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, _gpsLocationListener);
         /*else
             UnityPlayer.UnitySendMessage("RunView", "GetGpsProviderStatus", "False");*/
 
         _currentLocation = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
 
         String json = LocationToJson(_currentLocation);
         UnityPlayer.UnitySendMessage("RunView", "GetLocationData", json);
     }
     catch(Exception ex)
     {
         Log.d("LocationServices", "Error: " + ex.getMessage());
     }
 
     _isTracking = true;
 }

If you have any idea what could be causing this, any help would be appreciated. If you have any questions let me know and I'll update the post. Thanks all for you help!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by getmetotheforums · Jan 28, 2015 at 04:48 PM

I was not able to resolve this issue, but was able to get past it by using GooglePlayServices Location API: https://developer.android.com/google/play-services/location.html

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getmetotheforums · Jan 28, 2015 at 04:41 PM 0
Share

If you want to have the gps run in the background be sure to use the overloaded version of requestLocationUpdates that takes a PendingIntent as a parameter and be sure to create that PendingIntent with PendingIntent.getService().

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to pass accurate GPS location from android studio to unity? 0 Answers

Child location problem 1 Answer

Where can i find readymade AI scrips for free? 0 Answers

How do I move the Asset Folder to another drive? 0 Answers

Instantiate a random prefab at an objects location 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges