0

我需要一些指导;我试图通过 AsyncTask 在后台获取 gps/geo 位置;所以这是我的场景。我有一个类可以进行网络/gps 检查和 lat+long 获取。在我的主要活动中,我想调用它并抑制这些信息。但是此代码不起作用。有任何想法吗?

ps

进度条圈也没有出现!

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

和我的网络/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());
        }
    }
 }
}
4

0 回答 0