应用程序已在Android模拟器中意外停止

时间:2011-04-26 08:12:06

标签: android location

我正在寻找获得用户位置的优化方法,我在这里找到了示例,这是Fedor先生于2010年6月30日回答的

我按照他在代码中解释的方式做了同样的事情,唯一的区别是我使用了抽象类结果的gotLocation回调方法。在这个方法中,我想使用Toast.makeText将Provider名称显示为msg。当我运行此代码时,我的模拟器上没有显示任何内容,几秒钟后它显示消息“应用程序已意外停止android模拟器”。我增加了在timer1.schedule方法中设置的时间,但没有运气。

我只是在Android平台上说明开发,所以我对此没有足够的了解,所以任何人都可以帮我解决这个问题。

以下是我的代码

文件名:UserLocation.java

package com.ideafarms.android.mylocation;

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class UserLocation{
    Timer timer1;
    LocationManager locMgr;
    LocationResult locationResult;
    boolean gps_enabled = false;
    boolean network_enabled = false;

    LocationListener locationListenerGps = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            timer1.cancel();
            locationResult.gotLocation(location);
            locMgr.removeUpdates(this);
            locMgr.removeUpdates(locationListenerNetwork);
        }
    };

    LocationListener locationListenerNetwork = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            timer1.cancel();
            locationResult.gotLocation(location);
            locMgr.removeUpdates(this);
            locMgr.removeUpdates(locationListenerGps);          
        }
    };

    public boolean getLocation(Context context, LocationResult result){
        // Use LocationResult callback class to pass location value from UserLocation to User code
        locationResult = result;
        if(locMgr == null){
            locMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            // Handle exception if provider is not permitted
            try{
                gps_enabled = locMgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
            }catch(Exception ex){

            }
            try{
                network_enabled = locMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            }catch(Exception ex){

            }
            // don't start listeners if no provider is enabled
            if(!gps_enabled && !network_enabled){
                return false;
            }

            if(gps_enabled){                
                locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
            }
            if(network_enabled){                
                locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
            }
            timer1 = new Timer();
            timer1.schedule(new GetLastLocation(), 20000);
            return true;
        }
        return true;
    }
    class GetLastLocation extends TimerTask {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            locMgr.removeUpdates(locationListenerGps);
            locMgr.removeUpdates(locationListenerNetwork);
            Location net_loc=null, gps_loc=null;
            if(gps_enabled){
                gps_loc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            }
            if(network_enabled){
                net_loc=locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            }
            // if there are both values use the latest one
            if(gps_loc!=null && net_loc!= null){
                if(gps_loc.getTime()>net_loc.getTime()){
                    locationResult.gotLocation(gps_loc);
                }else{
                    locationResult.gotLocation(net_loc);
                    }
                return;
            }
            if(gps_loc!=null){
                locationResult.gotLocation(gps_loc);
                return;
            }
            if(net_loc!=null){
                locationResult.gotLocation(net_loc);
                return;
            }
            locationResult.gotLocation(null);
        }

    }
    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }
}

package com.ideafarms.android.mylocation;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.ideafarms.android.mylocation.UserLocation.LocationResult;

public class MyLocation extends Activity {

    /** Called when the activity is first created. */
    TextView myLoc ;
    public LocationResult locResult = new LocationResult(){
        @Override
        *public void gotLocation(Location location) {
            // TODO Auto-generated method stub

            Toast msg = Toast.makeText(MyLocation.this, location.getProvider(), Toast.LENGTH_LONG);
            //msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
            msg.show();
        }*      
    };


    boolean loc;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        UserLocation usrLocation = new UserLocation();
        myLoc = (TextView)findViewById(R.id.myLocation);
        loc = usrLocation.getLocation(this, locResult);

    }

}

我用斜体标记了我遇到问题的代码。

由于

1 个答案:

答案 0 :(得分:0)

看看这里:http://developer.android.com/guide/developing/tools/emulator.html

您需要模拟gps设备才能从模拟器中获取位置数据。

相关问题