how to access interfaces's methods...please help "Android"
Hallo, I know this forum is not for Android Applications but i have faced a problem concerning java programming and i just need the concept and guidance to fix my problem.
my question is: how to access getRegisteredLocationLat() and getRegisteredLocationLong methods
note that: getRegisteredLocationLat() and getRegisteredLocationLong() I have implemented them inside the interface
Code:
package com.androidbook.CarPosition;
import android.app.Activity;
import android.content.Context;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class CarPositionActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
try
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 2, locationListener);
}
catch(IllegalArgumentException e)
{
TextView tv1 = new TextView(this);
tv1.setText("Provider or Lisetner is null");
setContentView(tv1);
}
catch(RuntimeException e)
{
TextView tv2 = new TextView(this);
tv2.setText("No suitable permission or the calling thread has no looper");
setContentView(tv2);
}
double latitude = ((MyLocationListener) locationListener).getRegisteredLocationLat();
TextView tv3 = new TextView(this);
tv3.setText(" "+latitude);
setContentView(tv3);
}
}
-------------------------------------------------------------------------------------------------------------------------------------
Code:
package com.androidbook.CarPosition;
import android.content.Context;
import android.content.ContextWrapper;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MyLocationListener implements LocationListener {
double longitude, latitude;
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
loc.getLatitude();
loc.getLongitude();
Context context = null;
String text = "current location is: " + "latitude = " +loc.getLatitude() +
"longitude = " +loc.getLongitude();
setRegisteredLocationLat(loc.getLatitude());
setRegisteredLocationLong(loc.getLongitude());
Toast.makeText(context.getApplicationContext(), "location changed", Toast.LENGTH_SHORT).show();
}
public void setRegisteredLocationLat(double lat) {
// TODO Auto-generated method stub
latitude=lat;
}
public void setRegisteredLocationLong(double longi) {
// TODO Auto-generated method stub
longitude=longi;
}
public double getRegisteredLocationLong() {
// TODO Auto-generated method stub
return longitude;
}
public double getRegisteredLocationLat() {
// TODO Auto-generated method stub
return latitude;
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Re: how to access interfaces's methods...please help "Android"
Your question is quite chaotic, here's a question for you: are those getLat( ... ) and getLong( ... ) methods defined in the LocationListener interface? If they are, I don't see the problem? If they aren't, why not?
kind regards,
Jos
Re: how to access interfaces's methods...please help "Android"
the originally LocationListener interface has:
public void onLocationChanged(Location loc)
public void onProviderDisabled(String arg0)
public void onProviderEnabled(String arg0)
public void onStatusChanged(String provider, int status, Bundle extras)
but I added the following methods to it:
getLat() and getLong
and I want to call these two
is it possible to call them?and how
Re: how to access interfaces's methods...please help "Android"
Quote:
Originally Posted by
amrmb09
the originally LocationListener interface has:
public void onLocationChanged(Location loc)
public void onProviderDisabled(String arg0)
public void onProviderEnabled(String arg0)
public void onStatusChanged(String provider, int status, Bundle extras)
but I added the following methods to it:
getLat() and getLong
and I want to call these two
is it possible to call them?and how
If those methods were added to the interface definition then every class that implements the interface has to implement those methods. There wouldn't be any problem; I suspect you did something else but you are still being unclear ...
kind regards,
Jos
Re: how to access interfaces's methods...please help "Android"
in the last line in the first java code...there are some comments to show how i have been calling thoses methods getLat() and getLong()
Please let me know wheather the way i followed to call them is right or wrong
thnks
Re: how to access interfaces's methods...please help "Android"
Quote:
Originally Posted by
amrmb09
in the last line in the first java code...there are some comments to show how i have been calling thoses methods getLat() and getLong()
Please let me know wheather the way i followed to call them is right or wrong
thnks
Well, if those methods were defined in that interface there wouldn't be any need to cast the object to a MyLocationListener (mind the capitalization). I still don't know what you did ...
kind regards,
Jos
Re: how to access interfaces's methods...please help "Android"
Quote:
Originally Posted by
JosAH
Well, if those methods were defined in that interface there wouldn't be any need to cast the object to a MyLocationListener (mind the capitalization). I still don't know what you did ...
kind regards,
Jos
the interface (LocationListener) basically doesnt has these 2 methods getLat() and getLong()
but because i need them i implemented them inside the class (MyLocationListener)that implemets LocationListener
Re: how to access interfaces's methods...please help "Android"
Quote:
Originally Posted by
amrmb09
the interface (LocationListener) basically doesnt has these 2 methods getLat() and getLong()
but because i need them i implemented them inside the class (MyLocationListener)that implemets LocationListener
Read my previous reply again: you need to cast your object to a MyLocationListener type/class because that type/class implements those methods; the interfaces doesn't. I consider it bad design.
kind regards,
Jos
Re: how to access interfaces's methods...please help "Android"
Quote:
Originally Posted by
JosAH
Read my previous reply again: you need to cast your object to a MyLocationListener type/class because that type/class implements those methods; the interfaces doesn't. I consider it bad design.
kind regards,
Jos
thanks alot ...Solved
Re: how to access interfaces's methods...please help "Android"
Quote:
Originally Posted by
amrmb09
thanks alot ...Solved
I still don't know what your original issue was; was it just a matter of capitalization of the class name?
kind regards,
Jos
Re: how to access interfaces's methods...please help "Android"
i was just type casting with the wrong class
please see the above code
i modified it
regards,