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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:48:27+00:00 2026-06-12T22:48:27+00:00

I have Application that contains main Activity, Service and several Threads run on this

  • 0

I have Application that contains main Activity, Service and several Threads run on this Service. The Activity has 1 button ‘Start service‘ and other UI. When user press on button, the name of button changes to ‘Stop service‘, the program creates new Service that stores information about CPU usage and pastes stored information to TextVeiw with frequency every 1 second.

Lets say I stopped main Activity. The Service continues to run but there is no any Activity that Service can pass data over.

After 1 min. I launch main Activity again. First of all I want to see above mentioned button state (name) ‘Stop service‘ and I want to see the old service which continue to talk with my new Activity.

I don’t intend to use LocalBinder and bindService(...); method since my Service stays in my own Application.

Any solutions?

  • 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-12T22:48:28+00:00Added an answer on June 12, 2026 at 10:48 pm

    Here is an extensive code example.

    On start, Activity renders the instance to Application. Our Application will store instance of main Activity (or in other words Interface of Activity)

    see GuiServiceBridgeItf gsb

    snippets Application class

    public class MyApplication extends Application{ 
    
        private static mYApplication mSingleton;
    
        public GuiServiceBridgeItf gsb = null;
    
     @Override
     public void onCreate() {
        super.onCreate();
        mSingleton = this;
     } 
    
     public mYApplication getApp(){
        return mSingleton;
     }
    ....
    }
    

    GuiServiceBridgeItf interface

    public interface GuiServiceBridgeItf {
       public void onEventReceived(String str);
    
       /** Notify activity about service run */
       public void imHere();
    ....
    }
    

    Here is our Service that implements the same Interface GuiServiceBridgeItf.
    MyService runs 2 Threads:

    One Thread runs some logic (in our case notify Activity on CPU every second)

    Second one notifies main Activity if Service runs (maybe better to provide this task to Activity to ask if Service runs)

    public class MyService extends Service implements GuiServiceBridgeItf {
    
    ....
    
    private RunnerThread runner = null;
    private CheckAliveThread checkalive = null;
    
    private MyApplication mMyApp = null;
    
    public void onCreate() {
        super.onCreate();
    
        mMyApp  = (MyApplication)getApplicationContext();
    }
    
    
    public int onStartCommand(Intent intent, int flags, int startId) {
    
        GuiServiceBridgeItf gui2ServiceTalker = this;
    
        checkalive = new CheckAliveThread();
        checkalive.init(mMyApp);
        checkalive.start();
    
    
        runner = new RunnerThread(this.getApplicationContext(), gui2ServiceTalker);
        runner.start();
    
        ...
    
        return START_STICKY;
    }
    
    public void onDestroy() {   
        super.onDestroy();
    
        ...
    
        runner.doDestroy();     
        runner = null;  
    
        checkalive.doDestroy();
        checkalive = null;
    
        stopSelf();
    }
    
    
    
    private boolean isInstanceActive(){
        if(mMyApp.gsb == null){
            return false;
        }
    
        return true;
    }
    
    
    public void imHere() {}
    
    public void onEventReceived(String str) {
        if(isInstanceActive()){
            mMyApp.gsb.onEventReceived(str);
        }
    }
    
    ...
    
    }
    

    The main method here is: boolean isInstanceActive() that returns true or false on
    Activity state. In other words if we down Activity, we unregister it’s instance from Application. Because onDestroy method in main Activity resets MyApplication.gsb.

    In other hand when we relaunch new Activity, we register it to Application again, (see below)

    public class LauncherUI extends Activity implements GuiServiceBridgeItf{
    
         ....
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
                // register Activity to Application    
                GuiServiceBridgeItf gts = this;     
        mApplicationApp = (MyApplication)mContext.getApplicationContext();
        mApplicationApp .gsb = gts;
    
                ....
       }
    
       ....
    
        @Override
    protected void onDestroy() {
       super.onDestroy();
           ....
           // Unregister Activity to prevent to Service to talk with   
           mMyApplication.gsb = null;
    }
    
    }
    

    the Service, by using his Thread, validates that Activity registered to Application and therefore continues to notify newborn Activity about CPU change. For now Activity can update button name to ‘Stop service‘ and do other stuff

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that contains a main Activity, which has a very simple
I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
I have an app which has a main activity that calls other activities on
My application contains a main activity A and several other activities B, C, D,
I'm developing an iOS 4 application. I have a main view that contains another
I have my main application delegate which contains a method that returns an object.
I have an application that contains two Activities with <intent-filter> <action android:name=android.intent.action.MAIN/> <category android:name=android.intent.category.LAUNCHER/>
I have an application with a main JFrame that contains a default list model.
I have an application (in MS Visual Studio) that contains 3 projects: main (the
I have an application with a form that has a main menu. Now I

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.