Android: Can't create handler inside thread that has not called Looper.prepare()
I am trying to run the following but am getting the error
Can't create handler inside thread that has not called Looper.prepare()
Here is my code
private static void getLocations()
{
try
{
m_locationManager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
m_locationListener = new LocationListener() {
public void onLocationChanged(Location location)
{
Log.d("Output", "onLocationChanged");
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.d("Output", "onStatusChanged");
}
public开发者_JAVA百科 void onProviderEnabled(String provider)
{
Log.d("Output", "onProviderEnabled");
}
public void onProviderDisabled(String provider)
{
Log.d("Output", "onProviderDisabled");
}
};
m_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1000, m_locationListener);
Log.d("Output", "done");
}
catch(Exception ex)
{
Log.d("Output", ex.getMessage());
}
}
If I put Looper.prepare()
before the requestLocationUpdates()
the error goes away but no events are fired.
If I then put Looper.loop()
after the the requestLocationUpdates()
it pauses and the "done" log is not called.
If if call the function within runOnUiThread
like so:
activity.runOnUiThread(new Runnable() {
public void run()
{
getLocations();
}
});
no events are fired.
Im running this as a plugin for the Unity game engine so I don't have direct access to the main thread.
I have tried running this code outside of Untiy and it runs fine.
I'm pretty stuck on this one so any help would be appreciated.
Cheers
Update:
So I'm now trying to retrieve the locations inside a separate Activity. However, the events are still not being called.
public class LocationActivity extends Activity implements LocationListener
{
private LocationManager m_locationManager;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout content = new LinearLayout(this);
content.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
content.setOrientation(LinearLayout.VERTICAL);
content.setBackgroundColor(Color.TRANSPARENT);
TextView infoLabel = new TextView(this);
infoLabel.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
infoLabel.setTextSize(20.0f);
infoLabel.setText("Initializing...");
content.addView(infoLabel);
try
{
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = null;
if (m_locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER))
{
provider = LocationManager.GPS_PROVIDER ;
Log.d("Unity", "Using GPS");
m_locationManager.requestLocationUpdates(provider, 1000, 100, this);
}
else if(m_locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
provider = LocationManager.NETWORK_PROVIDER;
Log.d("Unity", "Using Netword");
m_locationManager.requestLocationUpdates(provider, 1000, 100, this);
}
else
{
Log.d("Unity", "Provider Not available");
}
}
catch(Exception ex)
{
Log.d("Unity", "locatons error " + ex.getMessage());
}
setContentView(content);
}
public void onLocationChanged(Location location)
{
Log.d("Unity", "UserLocation Lat:" + location.getLatitude() + " Long:" + location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.d("Unity", "onStatusChanged");
}
public void onProviderEnabled(String provider)
{
Log.d("Unity", "onProviderEnabled");
}
public void onProviderDisabled(String provider)
{
Log.d("Unity", "onProviderDisabled");
}
}
The view is opened correctly and and Both GPS and Network are available. Still no firing events
Im using these permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Try mView.post(new Runnable() { getLocations() };
精彩评论