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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:22:24+00:00 2026-06-07T07:22:24+00:00

How to call a method at the time From the service to activity. I

  • 0

How to call a method at the time From the service to activity. I want to cal only particular method From the service .am using timer function and handler.

In my Activity the method name is savedata(), i want to cal this function

service

public class MyService extends Service 
{
    @Override
    public IBinder onBind(Intent intent) 
    {
        return null;
    }

    @Override
    public void onCreate() 
    {
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onDestroy() 
    {
        Log.d(TAG, "onDestroy");
    }

    public void onStart(Intent intent, int startid) 
    {
        Timer mTimer = new Timer(user);
        mTimer.scheduleAtFixedRate(new mainTask(), 5000,60000);//1 hour=3600 s

    }

    private class mainTask extends TimerTask 
    { 
        public void run() 
        {
            toastHandler.sendEmptyMessage(0);
        }
    }  


    private final Handler toastHandler = new Handler() 
    {
        public void handleMessage(Message msg) 
        {
                Intent myIntent = new Intent(getBaseContext(),StorageHelper.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(myIntent);//here i start the activity but i need to acces the particular method only.

        }
    };    

 }

update

Activity

public class StorageHelper extends Activity
{
    final DBAdapter1 database=new DBAdapter1(this);

MessageCount objmsgCount=new MessageCount();
String msgCount;
int count;
String []tokens=null;
String notify=null;
int userid=71; 


 public  String savedata()
    {
        msgCount=objmsgCount.getMessageCount();
        {
            try {
                database.open();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
                    long id=database.insert(71,4,"yes"); 
                    database.close();

        return "success";
    }
}
  • 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-07T07:22:26+00:00Added an answer on June 7, 2026 at 7:22 am

    Its a good question, probably been asked many times before but one that stumped me to begin with.

    You have several options, the easiest of which is to register a listener with your activity, but this would require you to implement onBind(Intent) so you can connect from the activity to the service so you can register the listener.

    The following example shows you how to do this, once you have registered the activity as a listener with setServiceClient(ExampleServiceClient) the service can then invoke the method exampleServiceClientMethod() on the activity.

    You will notice I use a WeakReference when registering the client, always make sure you check to see you still have the reference when invoking any methods you add to ExampleServiceClient.

    public class ExampleService extends Service {
    
        public interface ExampleServiceClient {
            void exampleServiceClientMethod();
        }
    
        private WeakReference<ExampleServiceClient> mClient;
    
        public void setServiceClient(ExampleServiceClient client) {
            if(client == null) {
                mClient = null;
                return;
            }
    
            mClient = new WeakReference<ExampleServiceClient>(client);
        }
    
        public class ExampleBinder extends Binder {
            ExampleService getService() {
                return ExampleService.this;
            }
        }
    
        private IBinder mBinder = new ExampleBinder();
    
        @Override
        public IBinder onBind(Intent intent) {
            return mBinder;
        }
    }
    

    Your activity:

    public class ExampleServiceActivity extends Activity implements ExampleServiceClient {
    
        private ExampleServiceConnection mServiceConnection = new ExampleServiceConnection();
        private ExampleService mService = null;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            bindService(new Intent(this, ExampleService.class), mServiceConnection, BIND_AUTO_CREATE);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
    
            unbindService(mServiceConnection);
        }
    
    
        class ExampleServiceConnection implements ServiceConnection {
    
            public void onServiceConnected(ComponentName name, IBinder service) {
                mService = ((ExampleBinder)service).getService();
    
                mService.setServiceClient(ExampleServiceActivity.this);
            }
    
            public void onServiceDisconnected(ComponentName name) {
                mService.setServiceClient(null);
                mService = null;
    
            }
        }
    
    
        public void exampleServiceClientMethod() {
            // TODO Auto-generated method stub
        }
    }
    

    Hope that helps.

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

Sidebar

Related Questions

How to call method of my Service each time Activity unbinds it? It's desirable
Need to call web method, from web service. but name of method is unknown
In the client side every time I call a function from my WCF service,
I'm trying to call method from .jar But when I want to declare array
I have a requirement: I want to call method every day at any particular
I call a WCF service from multiple threads at the same time, but the
How do I call a stored procedure from a web service method without having
I want to call another intent from ArrayAdapter class using startActivityForResult but i cann't
Hi I was wondering how can I call a method just for one time
I have 2 classes 'Main' and 'FOR'. From 'Main' I will call method 'display'

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.