Android:无法在未调用Looper.prepare()的线程内创建处理程序

时间:2011-09-03 16:12:22

标签: java android unity3d

我正在尝试运行以下内容但收到错误

  

无法在未调用Looper.prepare()

的线程内创建处理程序

这是我的代码

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 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());
    }

}

如果我将Looper.prepare()放在requestLocationUpdates()之前,则错误消失,但不会触发任何事件。

如果我将Looper.loop()放在暂停requestLocationUpdates()之后,则不会调用“完成”日志。

如果在runOnUiThread内调用此函数,请执行以下操作:

activity.runOnUiThread(new Runnable() {
    public void run() 
    {
        getLocations(); 
    }
});

没有发生任何事件。

我将此作为Unity游戏引擎的插件运行,因此我无法直接访问主线程。

我尝试在Untiy之外运行此代码并且运行正常。

我非常坚持这一点,所以任何帮助都会受到赞赏。

干杯


更新

所以我现在正在尝试检索单独的Activity中的位置。但是,事件仍未被调用。

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");
    }
} 

视图正确打开,GPS和网络均可用。仍然没有射击事件

我正在使用这些权限

<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"/> 

1 个答案:

答案 0 :(得分:0)

尝试mView.post(new Runnable() { getLocations() };