3

I want to wrap a real listener to Observable object. For starters here is a test case, with him everything is fine.

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    getObservablePhoneState()
        // Run on a background thread
        .subscribeOn(Schedulers.io())
        // Be notified on the main thread
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(integer -> Log.i(TAG, "----- subscribe onNext = " + integer));
}

private Flowable<Integer> getObservablePhoneState() {
    return Flowable.create(emitter -> {

        Log.i(TAG, "Emitting 1");
        emitter.onNext(1);

        Log.i(TAG, "Emitting 2");
        emitter.onNext(2);

    }, BackpressureStrategy.BUFFER);
}



*** logcat ***
Emitting 1
Emitting 2
----- subscribe onNext = 1
----- subscribe onNext = 2

This code generates an error:

private Flowable<Integer> getObservablePhoneState() {
    return Flowable.create(emitter -> {

        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                Log.i(TAG, "onCallStateChanged = " + state);
                emitter.onNext(state);
            }
        };
        TelephonyManager telephonyManager = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    }, BackpressureStrategy.BUFFER);
}

*** logcat ***
io.reactivex.exceptions.OnErrorNotImplementedException: 
Attempt to read from field 'android.os.MessageQueue 
android.os.Looper.mQueue' on a null object reference

With Observable.create() the same error. Perhaps this is due to the fact that RxJava2 does not support emitting a null value.

How to do it right?

1
  • Is it possible you're calling thing before onCreate() in your activity? I tried your code and it worked fine for me. Commented Sep 20, 2017 at 22:13

1 Answer 1

3

You should remove the subscribeOn(Schedulers.io()) to avoid creating the PhoneStateListener in a different thread, because under the hood is trying to send messages using a Handler which mQueue is null. Just call

getObservablePhoneState()
     .subscribe { integer -> Log.i("", "----- subscribe onNext = " + integer) }
Sign up to request clarification or add additional context in comments.

6 Comments

Thx, your comment was useful. the code worked. but... onNext occurs only 1--3 times, and further - nothing. I'm testing under Android 8, and I'm using the latest versions of the libraries. I will post the complete code for tests later. Perhaps I chose the too complicated Listener.
onNext is being called 1-3 times but your listener was called more times?
Listener dead and (of course) onNext. Android 4.2 - all OK. Android 8 - :( Native Listener working fine. Full code here github.com/tim4dev/dirty_code/tree/master/…
Native phoneStateListener() working OK everywhere. On Android 4.2 "Observable Listener" (Observable, Flowable) worked fine. On Android 8.0 "Observable Listener" (Observable, Flowable) called 2--3 times and dead. Steps to reproduce the problem: - set PHONE_NUMBER to your own phone (yes, you will phone call to yourself) - run app on Android 8 - call - press disconnect (off hook) - if you do it fast enough - everything will be ok - if you listen to short beeps and wait, the Listener dies and does not react any more.
I'm not sure but, have you tried passing a different state to the listen method? check the changelog for 8.0 the API could have changed
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.