片段中的LocationListener [英] LocationListener in fragment

查看:101
本文介绍了片段中的LocationListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在片段管理器中使用LocationListener吗?实际上,当我使用它时,在这一行中会给我错误:

Can I use LocationListener with LocationManager in fragment? Actually when I use it, it gives me error in this line:

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, (android.location.LocationListener) this);

当我不将第4个参数强制转换为android.location.LocationListener时,会给我错误...

and when I do not casts the 4th parameter to android.location.LocationListener it gives me error...

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class Speedometer extends Fragment implements LocationListener {

    TextView txt;
    public Speedometer(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.speedometer, container, false);

        txt=(TextView)rootView.findViewById(R.id.speedometer);

        LocationManager lm= (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, (android.location.LocationListener) this);

        return rootView;
    }

    @Override
    public void onLocationChanged(Location location) {

        if(location==null){
           txt.setText("-.- m/s");
        } else {
            float nCurrentSpeed=location.getSpeed();
            txt.setText(nCurrentSpeed+"m/s");
        }   
    }
}

推荐答案

我认为您错误地实现了

I think you have mistakenly implemented com.google.android.gms.location.LocationListener interface that has just one 1 abstract method onLocationChanged

您应该实施具有4种抽象方法的 android.location.LocationListener

You should rather implement android.location.LocationListener that has 4 abstract methods

onLocationChanged(Location location)

onProviderDisabled(String provider)

onProviderEnabled(String provider)

onStatusChanged(String provider, int status, Bundle extras)

这篇关于片段中的LocationListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆