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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:42:23+00:00 2026-05-24T16:42:23+00:00

I am all confused about Service , Activity , Intent concepts. What I am

  • 0

I am all confused about Service, Activity, Intent concepts. What I am trying to do is TrackerMapService to work in background every 5 sec check location, and if it is changed TextView of the Activity will update number of locations tracked.

public class TrackerService extends IntentService implements LocationListener {

private static final String LOGTAG = "TrackerService";

private LocationManager manager;
private ArrayList<Location> storedLocations;

private boolean isTracking = false;

public TrackerService() {
    super("TrackerServiceName");
}

/* Service Setup Methods */
@Override
public void onCreate() {
    manager = (LocationManager)getSystemService(LOCATION_SERVICE);
    storedLocations = new ArrayList<Location>();
    Log.i(LOGTAG, "Tracking Service Running...");
}

public void startTracking() {
    if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        return;
    }
    Toast.makeText(this, "Starting Tracker", Toast.LENGTH_SHORT).show();
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);

    isTracking = true;
}

public void stopTracking() {
    Toast.makeText(this, "Stopping Tracker", Toast.LENGTH_SHORT).show();
    manager.removeUpdates(this);
    isTracking = false;
}

public boolean isTracking() {
    return isTracking;
}

@Override
public void onDestroy() {
    manager.removeUpdates(this);
    Log.i(LOGTAG, "Tracking Service Stopped...");
}

/* Service Access Methods */
public class TrackerBinder extends Binder {
    TrackerService getService() {
        return TrackerService.this;
    }
}

private final IBinder binder = new TrackerBinder();

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

public int getLocationsCount() {
    return storedLocations.size();
}

public ArrayList<Location> getLocations() {
    return storedLocations;
}

/* LocationListener Methods */
@Override
public void onLocationChanged(Location location) {
    Log.i("TrackerService", "Adding new location");
    storedLocations.add(location);
}

@Override
public void onProviderDisabled(String provider) { }

@Override
public void onProviderEnabled(String provider) { }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }

@Override
protected void onHandleIntent(Intent intent) {
    int updatedLocNumber = getLocationsCount();
    Log.d("IntentService", "logged" + updatedLocNumber + "location in intent");

    // TODO Auto-generated method stub
    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction("LOCATION_UPDATED");
    getBaseContext().sendBroadcast(broadcastIntent);
}

}

public class ServiceActivity extends Activity implements View.OnClickListener {

Button enableButton, disableButton;
TextView statusView;

TrackerService trackerService;
Intent serviceIntent;

IntentFilter intentFilter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    enableButton = (Button)findViewById(R.id.enable);
    enableButton.setOnClickListener(this);
    disableButton = (Button)findViewById(R.id.disable);
    disableButton.setOnClickListener(this);
    statusView = (TextView)findViewById(R.id.status);

    serviceIntent = new Intent(this, TrackerService.class);


    //intent to filter for updates
    intentFilter = new IntentFilter();
    intentFilter.addAction("LOCATION_UPDATED");
    registerReceiver(intentReceiver, intentFilter);
}

@Override
public void onResume() {
    super.onResume();
    //Starting the service makes it stick, regardless of bindings
    startService(serviceIntent);
    //Bind to the service
    bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}

@Override
public void onPause() {
    super.onPause();
    if(!trackerService.isTracking()) {
        //Stopping the service let's it die once unbound
        stopService(serviceIntent);
    }
    //Unbind from the service
    unbindService(serviceConnection);
}

@Override
public void onClick(View v) {
    switch(v.getId()) {
    case R.id.enable:
        trackerService.startTracking();
        break;
    case R.id.disable:
        trackerService.stopTracking();
        break;
    default:
        break;
    }
    updateStatus();
}

private void updateStatus() {
    if(trackerService.isTracking()) {
        statusView.setText(String.format("Tracking enabled.  %d locations logged.",trackerService.getLocationsCount()));
    } else {
        statusView.setText("Tracking not currently enabled.");
    }
}

private ServiceConnection serviceConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        trackerService = ((TrackerService.TrackerBinder)service).getService();
        updateStatus();
    }

    public void onServiceDisconnected(ComponentName className) {
        trackerService = null;
    }
};

private BroadcastReceiver intentReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(getBaseContext(), "AAAAA", Toast.LENGTH_LONG);

    }
};

}
Edit LogCat:
08-15 14:40:36.982: ERROR/ActivityManager(67): ANR in com.tugce.TrackerService
08-15 14:40:36.982: ERROR/ActivityManager(67): Reason: Executing service com.tugce.TrackerService/.TrackerService
08-15 14:40:36.982: ERROR/ActivityManager(67): Load: 0.63 / 0.19 / 0.15
08-15 14:40:36.982: ERROR/ActivityManager(67): CPU usage from 10147ms to 0ms ago:
08-15 14:40:36.982: ERROR/ActivityManager(67): 1.1% 133/com.android.launcher: 0.5% user + 0.5% kernel / faults: 31 minor
08-15 14:40:36.982: ERROR/ActivityManager(67): 0.4% 67/system_server: 0.2% user + 0.1% kernel
08-15 14:40:36.982: ERROR/ActivityManager(67): 0.1% 229/com.android.defcontainer: 0.1% user + 0% kernel / faults: 33 minor
08-15 14:40:36.982: ERROR/ActivityManager(67): 0% 40/adbd: 0% user + 0% kernel
08-15 14:40:36.982: ERROR/ActivityManager(67): 8.5% TOTAL: 5.9% user + 2.5% kernel
08-15 14:40:36.982: ERROR/ActivityManager(67): CPU usage from 988ms to 1576ms later:
08-15 14:40:36.982: ERROR/ActivityManager(67): 12% 67/system_server: 7% user + 5.2% kernel
08-15 14:40:36.982: ERROR/ActivityManager(67): 8.7% 80/ActivityManager: 5.2% user + 3.5% kernel
08-15 14:40:36.982: ERROR/ActivityManager(67): 1.7% 68/HeapWorker: 1.7% user + 0% kernel
08-15 14:40:36.982: ERROR/ActivityManager(67): 1.7% 229/com.android.defcontainer: 0% user + 1.7% kernel
08-15 14:40:36.982: ERROR/ActivityManager(67): 1.7% 230/HeapWorker: 0% user + 1.7% kernel
08-15 14:40:36.982: ERROR/ActivityManager(67): 15% TOTAL: 12% user + 3.4% kernel

PS: Please do not write any other ways that are not appropriate but “save the day” solutions such as sharedPreferences. I want to do this in best way..

  • 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-05-24T16:42:24+00:00Added an answer on May 24, 2026 at 4:42 pm

    You can broadcast an intent from the Service.

       Intent i = new Intent(NEW_UPDATE);  
       sendBroadcast(i);
    

    In activity call updateStatus() in BroadcastReceiver

    public class Receiver extends BroadcastReceiver 
    {
    @Override
       public void onReceive(Context context, Intent intent) 
       {    
        String action = intent.getAction();
           if(action.equalsIgnoreCase(NEW_UPDATE)){  
               updateStatus();
           }
       }
    }
    

    Create an object of the broadcast receiver class

    private Receiver mUpdateReceiver= new Receiver();
    

    and register the broadcastreceiver by calling

    registerReceiver(mUpdateReceiver, new IntentFilter(NEW_UPDATE));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit confused about the use of all the IEnumerable<T> extension methods, intellisense
I´m a little bit confused by reading all the posts and tutorials about starting
I'm writing my first location based android app, but got confused about some of
I am a little confused about how to handle a web service result in
I'm kind of confused about setting up standard authentication for my SSL protected service.
I'm a little confused about all the different ways to create a new jQuery
First time using JSF, and I'm sort of confused about how getRowData() works. All
I'm a bit confused about the use of the Elastic IP service offered by
Here, I am confused about stored procedure. HERE, we apply all the data base
I'm confused as hell with all the bazillion ways to read/write/create excel files. VSTO,

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.