Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在splashscreen期间获取用户的地理位置_Android_Geolocation_Android Asynctask_Splash Screen_Android Location - Fatal编程技术网

Android 在splashscreen期间获取用户的地理位置

Android 在splashscreen期间获取用户的地理位置,android,geolocation,android-asynctask,splash-screen,android-location,Android,Geolocation,Android Asynctask,Splash Screen,Android Location,我正在尝试在应用程序启动时获取用户的地理位置,即在启动屏幕期间 我的方法是在主活动中使用asynctask从单独的类获取地理位置。我是android新手,所以我可能错过了一些东西。以下是我为获取用户的地理位置而编写的示例代码: package work.office; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.cont

我正在尝试在应用程序启动时获取用户的地理位置,即在启动屏幕期间

我的方法是在主活动中使用asynctask从单独的类获取地理位置。我是android新手,所以我可能错过了一些东西。以下是我为获取用户的地理位置而编写的示例代码:

package work.office;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;

public class SplashScreenActivity extends Activity {

    private static final String TAG = "SplashScreenActivity";
    private ProgressDialog pd = null;
    private Object data = null;
    GeoLocationFinder.LocationResult locationResult;
    Location newLocation = null;

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

        setupLocation();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splash_screen, menu);
        return true;
    }

    private void setupLocation() {
        pd = new ProgressDialog(SplashScreenActivity.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setTitle("Loading...");
        pd.setMessage("Finding your geo location... Please wait");
        pd.setCancelable(Boolean.FALSE);
        pd.setIndeterminate(Boolean.TRUE);
        pd.setMax(100);
        pd.setProgress(0);
        pd.show();

        GeoLocationFinder.LocationResult locationResult = new GeoLocationFinder.LocationResult() {

            @Override
            public void gotLocation(Location location) {
                if (location != null) {

                    newLocation = new Location(location);
                    newLocation.set(location);

                    Log.d(TAG,
                            "Got coordinates, congratulations. Longitude = "
                                    + newLocation.getLongitude() + " Latitude = "
                                    + newLocation.getLatitude());
                    pd.dismiss();
                } else {
                    Log.d(TAG, "Location received is null");
                }

            }

        };
        GeoLocationFinder geoLocationFinder = new GeoLocationFinder();
        geoLocationFinder.getLocation(this,
                locationResult);
    }
}
SplashScreenActivity.java(获取地理位置而无需意向服务)

当我在手机上安装此apk时,出现以下错误:原因:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序(在SplashScreenActivity.java中,当我在后台线程中尝试获取位置时)


也提到了错误,但在我的上下文中,我无法链接它。可能是因为我在后台线程中使用了SplashScreenActivity上下文。但如何纠正呢?另外,在启动屏幕期间找到地理位置的最佳方法是什么?请帮忙。

你做得太过分了。在这种情况下,AsyncTask无法完成任何任务。以下是简化但仍异步获取位置的代码:

package work.office;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;

public class SplashScreenActivity extends Activity {

    private static final String TAG = "SplashScreenActivity";
    private ProgressDialog pd = null;
    private Object data = null;
    GeoLocationFinder.LocationResult locationResult;
    Location newLocation = null;

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

        setupLocation();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splash_screen, menu);
        return true;
    }

    private void setupLocation() {
        pd = new ProgressDialog(SplashScreenActivity.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setTitle("Loading...");
        pd.setMessage("Finding your geo location... Please wait");
        pd.setCancelable(Boolean.FALSE);
        pd.setIndeterminate(Boolean.TRUE);
        pd.setMax(100);
        pd.setProgress(0);
        pd.show();

        GeoLocationFinder.LocationResult locationResult = new GeoLocationFinder.LocationResult() {

            @Override
            public void gotLocation(Location location) {
                if (location != null) {

                    newLocation = new Location(location);
                    newLocation.set(location);

                    Log.d(TAG,
                            "Got coordinates, congratulations. Longitude = "
                                    + newLocation.getLongitude() + " Latitude = "
                                    + newLocation.getLatitude());
                    pd.dismiss();
                } else {
                    Log.d(TAG, "Location received is null");
                }

            }

        };
        GeoLocationFinder geoLocationFinder = new GeoLocationFinder();
        geoLocationFinder.getLocation(this,
                locationResult);
    }
}

我还没有尝试过这种方法。但出于好奇,我也在使用网络作为提供商之一。因此,如果一个人没有打开GPS,但连接到了wi-fi,在这种情况下,主活动线程将调用网络操作,这是现在不允许的(应用程序将在主活动线程上崩溃,原因是网络)。我为此编写了asynctask。通常,您可以使用LocationManager.requestLocationUpdates()方法设置LocationListener。它将异步地为您提供位置更新。
package work.office;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;

public class SplashScreenActivity extends Activity {

    private static final String TAG = "SplashScreenActivity";
    private ProgressDialog pd = null;
    private Object data = null;
    GeoLocationFinder.LocationResult locationResult;
    Location newLocation = null;

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

        setupLocation();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splash_screen, menu);
        return true;
    }

    private void setupLocation() {
        pd = new ProgressDialog(SplashScreenActivity.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setTitle("Loading...");
        pd.setMessage("Finding your geo location... Please wait");
        pd.setCancelable(Boolean.FALSE);
        pd.setIndeterminate(Boolean.TRUE);
        pd.setMax(100);
        pd.setProgress(0);
        pd.show();

        GeoLocationFinder.LocationResult locationResult = new GeoLocationFinder.LocationResult() {

            @Override
            public void gotLocation(Location location) {
                if (location != null) {

                    newLocation = new Location(location);
                    newLocation.set(location);

                    Log.d(TAG,
                            "Got coordinates, congratulations. Longitude = "
                                    + newLocation.getLongitude() + " Latitude = "
                                    + newLocation.getLatitude());
                    pd.dismiss();
                } else {
                    Log.d(TAG, "Location received is null");
                }

            }

        };
        GeoLocationFinder geoLocationFinder = new GeoLocationFinder();
        geoLocationFinder.getLocation(this,
                locationResult);
    }
}