LocationListener - 未调用实现的方法

时间:2014-07-07 13:12:16

标签: android location

我试图在android中实现一个基于位置的基本程序,并且卡在第一页本身。

这是使用LocationManager和LocationListener接口的典型示例完成的 这是代码

    public class MainActivity extends ActionBarActivity 
{
MyLocationListner networkListner,gpsListner;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button start = (Button) findViewById(R.id.btn_start);
    start.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        startListening();

        }
    });

    Button stop = (Button) findViewById(R.id.btn_stop);
    stop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v)
        {
            stopListning();

        }
    });

}   

public void startListening()
{
    try {

        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
        Log.i("LocationApp","Started Listning");
        networkListner = new MyLocationListner(this);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, networkListner);
    } catch (Exception e) {
        Log.i("myException",""+e);
    }
}

public void stopListning()
{
    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (networkListner == null)
    {
        lm.removeUpdates(networkListner);
        networkListner = null;
    }
}

public void myToast (String message)
{
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

}

实现LocationListner的类

    public class MyLocationListner implements LocationListener 
{
 Context ctx;



public MyLocationListner(Context context) 
{
    this.ctx = context;
}

private static String LOG_TAG;

@Override
public void onLocationChanged(Location location) 
{
    Log.i(LOG_TAG,"lattitude");
    MyToast("OnLocation changeD");
    String provider = location.getProvider();
    double lat = location.getLatitude();
    double lon = location.getLongitude();
    float accuracy = location.getAccuracy();
    long time = location.getTime();

    MyToast(lat+"X"+lon+"Accuracy :"+accuracy);
    Log.i(LOG_TAG,"lattitude :"+lat+"\nLongitude :"+lon+"\nAccuracy :"+accuracy);

}

@Override
public void onProviderDisabled(String arg0) {
    Log.i(LOG_TAG,"Provider Disabled");

}

@Override
public void onProviderEnabled(String arg0) {
    Log.i(LOG_TAG,"Provider Enabled");
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) 
{

}

public void MyToast(String message)
{
    Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show();
}
}

并没有一个方法在上面的类中调用,我已经经历了三次tutuorial但仍然没有希望!我知道我做错了什么但找不到。

我也试过在真实设备上运行它。 需要的建议和答案 Tahnks

0 个答案:

没有答案