【问题标题】:Android toast not showing安卓吐司不显示
【发布时间】:2013-07-04 09:04:54
【问题描述】:

我有一个使用用户位置的活动。这是我目前写的代码:

public class VolSaveLocation extends Activity implements LocationListener {

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 0) {
                useLocation();    
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.volsavelocaction);

        LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

        TelephonyManager phoneManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        String phoneNumber = phoneManager.getLine1Number();
        Toast.makeText(getApplicationContext(), phoneNumber, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if (location != null) {
            Toast.makeText(getApplicationContext(), "Location not null!", Toast.LENGTH_SHORT).show();
            Message msg = new Message();
            msg.what = 0;
            msg.obj = location;
            handler.sendMessage(msg);
        }
        else
            Toast.makeText(getApplicationContext(), "Location is null!", Toast.LENGTH_SHORT).show();
    }
}

问题是,onLocationChanged 方法中的 Toast 均未显示。我正在使用地理修复来更改位置。我实现了 LocationListener 的其他方法,但它们是空的。显示电话号码的 toast 工作正常。有人可以帮帮我吗?

【问题讨论】:

  • 在你的“onLocationChanged”中写一个日志,看看它是否被调用了。
  • 没有 100% 的保证 onLocationChanged 每次调用。

标签: android android-location


【解决方案1】:

一个建议:

Timeinterval(requestLocationUpdates() 的第二个参数) 选择一个合理的值。延长电池寿命也很重要。每次位置更新都需要来自 GPS、WIFI、Cell 和其他无线电的电源。

选择一个尽可能高的时间值。

【讨论】:

    【解决方案2】:

    oncreate 定义一个类似 getLocation() 的方法,并在该函数中编写以下代码

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);      
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        if(provider!=null && !provider.equals("")){
            Location location = locationManager.getLastKnownLocation(provider);         
            locationManager.requestLocationUpdates(provider, 40000, 1, this);
            if(location!=null) {
                onLocationChanged(location);
            } else {
    
            }
        }else{
            Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
        }
    

    【讨论】:

    • 它显示三个错误:1)类型不匹配:无法从 String 转换为 Provider(在 getBestProvider 行上) 2)类型 LocationManager 中的方法 getLastKnownLocation(String) 不适用于参数( Provider)和3)LocationManager类型中的方法requestLocationUpdates(String,long,float,LocationListener)不适用于参数(Provider,int,int,VolSaveLocation)我认为如果我们修复第一个错误,所有这些都将得到修复.你能帮帮我吗?我是 android 的初学者。
    • 您为提供者定义的数据类型??并在 onCreate 部分定义 getLocation()。
    • 我已经定义了 getLocation 并从 onCreate 方法中调用它。我只是将提供者声明为提供者。我不知道有不同的类型。
    • 提供者提供者 = locationManager.getBestProvider(critera,false);
    • 没关系,当我将 NETWORK_PROVIDER 更改为 GPS_PROVIDER 时,它起作用了。还是谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    相关资源
    最近更新 更多