Results 1 to 11 of 11
  1. #1
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default 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
    Java 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);
            
            
        }
        
    }
    -------------------------------------------------------------------------------------------------------------------------------------
    Java 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
    
    	}
    
    }
    Last edited by amrmb09; 10-28-2011 at 09:09 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default 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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to access interfaces's methods...please help "Android"

    Quote Originally Posted by amrmb09 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default 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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to access interfaces's methods...please help "Android"

    Quote Originally Posted by amrmb09 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: how to access interfaces's methods...please help "Android"

    Quote Originally Posted by JosAH View Post
    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

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to access interfaces's methods...please help "Android"

    Quote Originally Posted by amrmb09 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default Re: how to access interfaces's methods...please help "Android"

    Quote Originally Posted by JosAH View Post
    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

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default Re: how to access interfaces's methods...please help "Android"

    Quote Originally Posted by amrmb09 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default 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,

Similar Threads

  1. "Access is denied" error while file creation
    By Bharath_M in forum Advanced Java
    Replies: 4
    Last Post: 04-10-2011, 12:23 PM
  2. "Best" implementation of math operator methods?
    By rgrant222 in forum New To Java
    Replies: 2
    Last Post: 09-01-2010, 07:47 AM
  3. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •