通过Handler更新UI不在工作线程内部工作

时间:2013-07-05 13:11:04

标签: android multithreading gps location handler

我试图通过处理程序从线程发送的消息更新TextView但它无法正常工作。有什么想法吗?

我的活动:

package com.parspake.kookojapost;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.location.*;
import android.os.*;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class TextShare extends Activity {

    ConnectionHandler textPageConn;
    TextView dt2;
    String mAddress;
    Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_share);

        textPageConn = new ConnectionHandler(this);
        dt2 = (TextView) findViewById(R.id.show_location_text_page);

        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                dt2.setText(mAddress);
                return true;
            }
        });

        LocationListener locLis = new LocationListener() {
            @Override
            public void onLocationChanged(final Location location) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {

                        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
                        List<Address> addresses;
                        try {
                            addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                            if (addresses.size() > 0) {
                                String currCity = addresses.get(0).getLocality();
                                String currCountry =  addresses.get(0).getCountryName();
                                mAddress = currCity + " - " + currCountry;
                                Log.d("omid debug","ok location was " + mAddress);
                                Message msg = mHandler.obtainMessage();
                                mHandler.dispatchMessage(msg);
                                // here i also tried mHandler.sendEmptyMessage(0); but didnt work either.
                            }
                        } catch (IOException e) {
                            Log.d("omid debug", e.getMessage());
                        }
                    }
                }).start();
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
            }

            @Override
            public void onProviderEnabled(String s) {
            }

            @Override
            public void onProviderDisabled(String s) {
            }
        };

        textPageConn.mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (textPageConn.locationSourceEnabled()) {
            if (textPageConn.mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                textPageConn.mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locLis);
            }
            if (textPageConn.mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                textPageConn.mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locLis);
            }
        }
    }
}

0 个答案:

没有答案