N
N
Nicholas Unknown2020-01-13 20:45:59
Java
Nicholas Unknown, 2020-01-13 20:45:59

How can a service track activity bindings?

Good day to all.
The essence is this:
There is a service, it works with my server.
When a message comes from the server, the service checks the boolean isBound variable;
If true - there is an activity subscribed to the service and the service sends a broadcast.
If it fails - there are no activities subscribed to the service, the service makes a notification.
When the activity goes to onStop(), then in the body of the method there is a decoupling from the service.
When the activity goes to onResume() - binding to the service.
The service itself is started by the startService() method when the program is started in the App class.
And so we approached the problem.
When the service is running and the activity is subscribed to it, everything is fine.
I press the home button and there is a decoupling from the service.
The first time the connection is successful.
The onUnBind() method is called in the service;
I open activity - there is a binding.
The onBind() method should be called, but there is no call.
There is no entry in the logs with a call to the OnBind() method from the service.
And then, when opening an activity, there is also no call to OnBind () I
re-read the documentation. I sit and think.
Why is this and what could I have done wrong?
According to my logic, onBind() should work on the first binding in the service, onUnBind() on unbinding, and onReBind() on rebinding, but they are silent.
The essence of the idea is to let the service understand what it should do, namely broadcasting or notification.
It is very important that in the onResume() activity state, the service would only do broadcasts, and when the activity in onStop() or does not exist at all, there would be a notification.
Below are the activity code snippets.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }


//Начало метода init()
    private void init() {
        viewModel = ViewModelProviders.of(this).get(MainViewModel.class);

        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d(DebugUtils.TAG, "onReceive: ");
                Message message = (Message) intent.getExtras().getSerializable(App.MESSAGE_EXTRA);
                outputText.setText(message.toString());
            }
        };


        intentFilter = new IntentFilter(App.ACTION_MESSAGE_RECEIVED);

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d(DebugUtils.TAG, "onServiceConnected: ");

                registerReceiver(receiver, intentFilter);

                ServiceBinder binder = (ServiceBinder) service;
                mService = (MessageService) binder.getService();
                isBound= true;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d(DebugUtils.TAG, "onServiceDisconnected: ");
                unregisterReceiver(receiver);

                isBound = false;
            }
        };

        messageEdit = findViewById(R.id.message_editText);
        outputText = findViewById(R.id.output_textview);
        loadingProgress = findViewById(R.id.loading_progress);
        loadButton = findViewById(R.id.load_button);
    }
//Конец метода init()

@Override
    protected void onResume() {
        super.onResume();

        Intent intent = new Intent(this, MessageService.class);

        startService(intent);
        bindService(intent, serviceConnection, 0);
        Log.d(DebugUtils.TAG, "Activity: onResume: service binded");
    }


 @Override
    protected void onStop() {
        super.onStop();
        Log.d(DebugUtils.TAG, "Activity: onStop: service unbinded");
        unbindService(serviceConnection);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmtm, 2020-01-14
@Dmtm

it's better to use ActivityLifecycleCallbacks

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question