Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does getSystemService() work exactly?
    primarykey
    data
    text
    <p>At first glance in the code below the <code>mLocationManager</code> object should go out of scope after <code>onCreate(...)</code> is finished, and the expected behaviour is that <code>onLocationChanged</code> is never called or called a few times until the object is garbage collected. However the object returned by the <code>getSystemService</code> seems to be singleton which lives outside the scope of <code>MainActivity</code> (appropriately so since it is a system service :) ) </p> <p>After taking a heap dump and going through it with the Eclipse Memory Analyzer it seems that ContextImpl keeps a reference to a LocationManager instance. In the memory dump there were two references to a LocationManager object while in the code there is clearly only one, which means that another reference is created somewhere else. </p> <p>My questions are: </p> <p>Does someone have a complete description of what is exactly happening when calling the implementation of: </p> <pre><code>public abstract Object getSystemService(String name); </code></pre> <p>is the object returned a singleton lazily created and where exactly is the reference created/kept ? </p> <pre><code>package com.neusoft.bump.client.storage; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.v("TAG", "STARTED"); LocationManager mLocationManager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { Log.v("TAG", "onLocationChanged"); Log.v("TAG", "Latitude: " + location.getLatitude() + "Longitude: " + location.getLongitude()); } 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 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600, 0, locationListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } </code></pre> <p><strong>Update1</strong></p> <p>The <code>LocationManager</code> is created as singleton</p> <pre><code>private LocationManager getLocationManager() { synchronized (sSync) { if (sLocationManager == null) { IBinder b = ServiceManager.getService(LOCATION_SERVICE); ILocationManager service = ILocationManager.Stub.asInterface(b); sLocationManager = new LocationManager(service); } } return sLocationManager; } </code></pre> <p>but I have trouble understanding what happens when calling <code>ServiceManager.getService(LOCATION_SERVICE);</code> even after reading the <code>ServiceManager</code> code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload