-1

我试图在应用程序启动时获取用户的 GPS/GeoLocation,但有时它会导致应用程序 UI 没有响应,然后著名的 Android 提示应用程序没有响应!所以我决定尝试 AsyncTask 来获取用户在主活动中的位置信息;但有时 UI 仍会挂起(通常是在禁用 gps 设置时,所以我转到系统设置以启用它,当我按下返回以返回我的应用程序时,它变得如此缓慢/无响应。)那我在这里做错了什么?任何帮助表示赞赏。

我应该获取位置信息的网络类:

package com.parspake.kookojapost;

import android.content.Context;
import android.location.*;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

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

public class ConnectionHandler {

private Context ctx;
double mLat;
double mLong;
String currCity;
String currCountry;
String mAddress;
LocationManager currentLoc;
LocationListener locLis;

public ConnectionHandler(Context context) {
    this.ctx = context;
}

public boolean internetAccess() {
    ConnectivityManager con = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = con.getActiveNetworkInfo();
    // Simplified: return netInfo != null && netInfo.isConnected();
    if (netInfo != null && netInfo.isConnected()) {
        return true;
    } else {
        return false;
    }
}

public boolean locationSourceEnabled() {
    currentLoc = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
    boolean isInternetLocationAvailable = currentLoc.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    boolean isGpsAvailable = currentLoc.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (isInternetLocationAvailable) {
        Log.d("kookojaPost", "internet location avaiable");
        return true;
    } else if (isGpsAvailable) {
        Log.d("kookojaPost", "gps location avaiable");
        return true;
    }
    return false;
}

public void getLocation() {

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


            Geocoder gcd = new Geocoder(ctx, Locale.getDefault());
            List<Address> addresses;
            try {
                addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                if (addresses.size() > 0) {
                    currCity = addresses.get(0).getLocality();
                    currCountry =  addresses.get(0).getCountryName();
                    mAddress = currCity + " - " + currCountry;
                }
            } catch (IOException e) {
//                    e.printStackTrace();
                    Log.d("kookojaPost", e.getMessage());
                }
                String s = "Loction: \n" + mAddress;
            Toast.makeText(ctx, s, Toast.LENGTH_LONG).show();
            Toast.makeText(ctx, "Location GPS: " + location.getLatitude() + " - " + location.getLongitude(), Toast.LENGTH_LONG).show();
            mLat = location.getLatitude();
            mLong = location.getLongitude();
        }

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

        @Override
        public void onProviderEnabled(String s) {
        }

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

    currentLoc = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
    if (locationSourceEnabled()) {
        if (currentLoc.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            currentLoc.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 50, locLis, Looper.getMainLooper());
        }
        if (currentLoc.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            currentLoc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 50, locLis, Looper.getMainLooper());
        }
    }
 }
}

这是我的主要活动,我在这里使用 AsyncTask 来做背景工作,这显然没有做它应该做的事情!

package com.parspake.kookojapost;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Calendar;

public class MainAct extends Activity {

ProgressBar pg;
ConnectionHandler mNet;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    pg = (ProgressBar) findViewById(R.id.progressBar);
    mNet = new ConnectionHandler(this);

//        pg.setIndeterminate(true);
//        pg.setVisibility(View.VISIBLE);


//        new FetchLocation().execute();

}

@Override
protected void onResume() {
    super.onResume();

    new FetchLocation().execute();

}


private class FetchLocation extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pg.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... params) {
        if (mNet.locationSourceEnabled()) {
            Log.d("kookojaPost","mLat is: " + mNet.mLat);
//               while (mNet.mLat == 0.0) {
//                   mNet.getLocation();
//               }
            if (mNet.mLat == 0.0) {
                mNet.getLocation();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        pg.setVisibility(View.INVISIBLE);
    }
 }
}
4

1 回答 1

2

我用这段代码解决了它,而不是使用 AsyncTask:

希望它对其他人有用。

    Runnable runThis = new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            if (mNet.locationSourceEnabled()) {
                mNet.getLocation();
            }
            Looper.loop();
        }
    };

    Thread th = new Thread(runThis);
    th.start();
于 2013-06-29T14:02:19.933 回答