How to execute a method of an Activity from a service, in Android?

-1

Hi guys and girls (of course)

I have this situation: I have running a service in the background and a foreground activity, and I hope that at a specific time the activity executes a method on demand of the service.

What I have done: Well the solution I have given is something almost to brute force. The service writes a value in the preferences, and the activity that is controlling that value every 100 mS, because I need it to be the instantaneous update; When you see the change in value, execute the method

Problem with this solution is that it consumes enough resources and does not always run as it should

Any ideas?

EDITED

This is part of the code I've done

1-in the service

//...
if (needAct)
    PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("needAct", true).commit();
///...

2-in the onCreate of the activity

///...
timerUiUpdate.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("needAct", false)){
                        //hacer lo que quiero hacer para actualizar la Activity
                    }
                }
            });
        }
    }, 100, 100);
///...
    
asked by San Juan 10.11.2017 в 19:42
source

2 answers

0

I'm back with what I did. Use a BroadcastReceiver for believing that it would be better for the project if I keep it with the simplest (KISS)

What I did was:

  • Within the activity that I want to update from the service, create a BroadcastReceiver with the procedure I want to perform and add it as an object of the class. Then in the constructor register the BroadcastReceiver and in the destructor deactivate it.

    public class Acty_Main extends Acty_Base {
        public class UpdateReceiver extends BroadcastReceiver {
            public UpdateMailListReceiver(){
                 super();
            }
            @Override
            public void onReceive(Context context, Intent intent) {
                 metodoQueQuieroEjecutar();//en la activity
            }
        }
        private BroadcastReceiver receiver = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                IntentFilter filter = new IntentFilter();
                filter.addAction("test.sanjuan.UPDATE");
                receiver = new UpdateReceiver ();
                registerReceiver(receiver, filter);
        }
        @Override
        protected void onDestroy() {
                if (receiver != null) {
                    unregisterReceiver(receiver);
                    receiver = null;
                }
                super.onDestroy();
        }
    
        public vois metodoQueQuieroEjecutar(){
               Toast.makeText(context, "NEED TO UPDATE", Toast.LENGTH_LONG).show();
        }
    }
    
  • And then to call him from the service I have to call the boardcast

    Intent intent = new Intent();
    intent.setAction("test.sanjuan.UPDATE");
    context.sendBroadcast(intent);
    
  • Clarification should not declare it in the manifest because this would cause an error in the execution time of "no empty constructor"

  • answered by 11.11.2017 / 21:28
    source
    2

    1. Broadcast

    You can do it with a Broadcast , you register the Broadcast in the Activity where you want to receive the in intent and then send it to execute the business logic with the necessary methods, and from the service you send the intent by sendBroadcast(intent)

    2. EventBus

    You can use the greenrobot library to send a evento from the service to your Activity.

    In the Activity you wait for the event

    @Subscribe(threadMode = ThreadMode.MAIN)  
    public void onMessageEvent(MessageEvent event) {/* Logica de negocio */};
    

    And from the Service you send the event

    EventBus.getDefault().post(new MessageEvent());
    

    Do not enter in code details because my answer is an opinion. My recommendation is that you use EventBus and you can investigate further to implement it in your project.

        
    answered by 10.11.2017 в 23:21