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 8817295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:48:42+00:00 2026-06-14T04:48:42+00:00

Is it possible, to use getSystemService in service? My service class: private LocationManager locationManager;

  • 0

Is it possible, to use getSystemService in service? My service class:

private LocationManager locationManager;
private LocationListener locationListener;

IBinder mBinder = new LocalBinder();      

public interface interfaceLocationService {     
      public Location currentBestLocation = null;
      public void startListenLocation();
      public void stopListenLocation();
      public Location getUserLocation();
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public class LocalBinder extends Binder implements interfaceLocationService{

    public void StopListenLocation(){
        locationManager.removeUpdates(locationListener);
        locationListener = null;
        stopSelf();
    }

    public void startListenLocation()
    { 
        locationListener = new LocationListener() {

            public void onStatusChanged(String provider, int status, Bundle extras) {    
                switch (status) {

                // Provider out of service
                case LocationProvider.OUT_OF_SERVICE:
                    Log.v(TAG, "Provider status changed: Out of Service");
                    break;

                // Provider temporarily unavailable
                case LocationProvider.TEMPORARILY_UNAVAILABLE:
                    Log.v(TAG, "Provider status changed: Temporarily Unavailable");
                    break;

                // Provider available again
                case LocationProvider.AVAILABLE:
                    Log.v(TAG, "Provider status changed: Available");
                    break;
                }
            }       

            public void onProviderEnabled(String provider) {    
                Log.v(TAG, "Provider enabled");
            }

            public void onProviderDisabled(String provider) {    
                Log.v(TAG, "Provider disabled");

                // Settings brought up to connect
                Intent intent = new Intent(
                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }

            public void onLocationChanged(Location location) {                  
                Log.v(TAG, "Location Changed");
                if (isBetterLocation(location, currentBestLocation)) {
                    currentBestLocation.set(location);
                }
            }
        };
        //if I have only one requestLocationUpdates situation is the same
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
                    400, 1, locationListener);
        }

    public Location getUserLocation(){
        return currentBestLocation;
    }

    @Override
    public void stopListenLocation() {
        locationManager.removeUpdates(locationListener);
        locationListener = null;
        stopSelf();
    }
}

@Override
public void onCreate() {
    // Start thread  
    locationManager = (LocationManager)SendService.this.getSystemService(Context.LOCATION_SERVICE);
    super.onCreate();
    Log.d(TAG, "SendService created");
}

@Override
public void onDestroy() {
    // Just destroy thread
    super.onDestroy();
    Log.d(TAG, "SendService destroyed");
}

And in activity I got code starting this service:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = new Intent(this, SendService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className,
            IBinder service) {
        mService = (interfaceLocationService) service; 
    }

    public void onServiceDisconnected(ComponentName arg0) {
        Log.d("LOG","Service disconnected");
        mService.stopListenLocation();
    }
};

public void startClicked(View v) {
    mService.startListenLocation();   
}

This code gives an errors in LogCat:

11-12 17:01:02.171: E/AndroidRuntime(2719): FATAL EXCEPTION: main
11-12 17:01:02.171: E/AndroidRuntime(2719): android.util.AndroidRuntimeException:      Calling startActivity() from outside of an Activity  context requires the      FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.app.ContextImpl.startActivity(ContextImpl.java:657)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at com.example.gps.data.SendService$LocalBinder$1.onProviderDisabled(SendService.java:78)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:204)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.os.Looper.loop(Looper.java:143)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at android.app.ActivityThread.main(ActivityThread.java:4717)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at java.lang.reflect.Method.invoke(Method.java:521)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-12 17:01:02.171: E/AndroidRuntime(2719):     at dalvik.system.NativeStart.main(Native Method)

Simply: Could I use LocationManager to listen to location in service while in background? If yes, then where is my mistake?

  • 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-14T04:48:44+00:00Added an answer on June 14, 2026 at 4:48 am

    Yes of course you can use the LocationManager in a service. for instance I am doing this in the onCreate method of the background service :

    public void onCreate() {
          mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    
          Criteria criteria = new Criteria();
          criteria.setAccuracy(Criteria.ACCURACY_COARSE);
          criteria.setAltitudeRequired(false);
          criteria.setBearingRequired(false);
          criteria.setCostAllowed(true);
    
          String provider = mLocationManager.getBestProvider(criteria, true);
          mLocationManager.requestLocationUpdates(provider, LOCATION_UPDATE_INTERVAL_MILLIS, LOCATION_UPDATE_INTERVAL_METERS, this);
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to use django class based generic views with a ManyToManyField relation?
Possible Duplicate: Use 'class' or 'typename' for template parameters? I see two different template
Is it possible use mod_rewrite to resolve addresses hosted on another server? Say I
Is it possible use a MySQL query to perform this kind of check? If
I need a control having these features: It should be possible use it in
Is possible to use ArcSDE API in .NET(C#) ? http://edndoc.esri.com/arcsde/9.2/api/capi/dbconnects/dbconnects.htm Thanks
Is this possible to use Ajax.Beginform with update target inside of ajax form. like
Is it possible to use JMF in Android? JMF has good functionality? I'm basically
Is it theoretically possible to use in-app purchases to update an iOS app automatically?
is it possible to use chaplin with meteor? Any idea, advice, pointers or guidance

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.