Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7810857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:56:52+00:00 2026-06-02T03:56:52+00:00

Somewhat new to android, need some help with services. I have a service which

  • 0

Somewhat new to android, need some help with services. I have a service which polls for current location at interval X. I want to bind to that service and pass getLastKnownLocation from the service to my Activity A. I am not sure exactly how the information is passed from the bound service to the activity, if its through the binder or what. Anyways, here is my code where I am at thus far.

Service:

public class LocationService extends Service implements LocationListener {


    LocationManager myLocationManager;
    public Location myLocation;
    LocationListener myLocationListener;
    public static final String TAG = LocationService.class.getSimpleName();
    MyDB db;
    double latitude,longitude;
    Cursor c;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Log.d(TAG, "service started (onCreate)");
        db = new MyDB(getApplicationContext());
        myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAccuracy(Criteria.ACCURACY_LOW);
        String locationProvider = myLocationManager.getBestProvider(criteria, true);
        myLocationManager.requestLocationUpdates(locationProvider, 1000*60*2, 100, this);
        myLocation = myLocationManager.getLastKnownLocation(locationProvider);


    }
 public class MyBinder extends Binder {
            LocationService getService() {
                return LocationService.this;
            }
        }

Activity A:

public class myActivity extends Activity {
    LocationManager myLocationManager;
    Location myLocation;

    boolean isBound = false;

    private LocationService mBoundService;

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


        bindLocationService();

}
private void bindLocationService() {
        try {
            isBound = getApplicationContext().bindService( new Intent(getApplicationContext(), LocationService.class), mConnection, BIND_AUTO_CREATE );
            bindService(new Intent(this, LocationService.class), mConnection, BIND_AUTO_CREATE);
        } catch (SecurityException e) {
            // TODO: handle exception
        }
    }
private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((LocationService.MyBinder)service).getService();
            Log.d(LocationService.TAG, "activity bound to service");

        }

        public void onServiceDisconnected(ComponentName className) {

            mBoundService = null;
            Log.d(LocationService.TAG, "activity unbound to service");
        }
    };
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:56:53+00:00Added an answer on June 2, 2026 at 3:56 am

    Send a Broadcast from your service like this:

    Intent i = new Intent(NEW_MESSAGE);  
    Bundle bundle = new Bundle();
    bundle.putString("yourvalue", value);
    i.putExtras(bundle);
    sendBroadcast(i);
    

    and register for receiver in your activity like this:

    newMessage messageReceiver = new newMessage();
    registerReceiver(messageReceiver, new IntentFilter(NEW_MESSAGE));
    

    This is your receiver in your activity class:

    public class newMessage extends BroadcastReceiver 
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {    
            String action = intent.getAction();
            if(action.equalsIgnoreCase(IMService.NEW_MESSAGE)){    
            Bundle extra = intent.getExtras();
            String username = extra.getString("yourvalue");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am somewhat new to R, and i have this piece of code which
I'm somewhat new to SQL and need help with query syntax. My issue involves
Somewhat new to SQL queries and I need a little help with my join.
I am somewhat new to Android and have been struggling for hours on how
I am somewhat new to Dependency Injection. I have set it up for some
I am somewhat new to LINQ and have a quick question regarding deleting. Say,
I'm somewhat new to OOP programming so it's very likely I'm making some stupid
I am somewhat new to jQuery and I have a problem with something I
I everyone. I'm very new to Android development and I want to develop an
I am somewhat new to JavaScript, and have been having an issue with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.