iGio90
iGio90

Reputation: 3301

Service callback throws: Uncaught remote exception! (Exceptions are not yet supported across processes.)

I'm having some issues with my callbacks. Here is my code:

Activity:

private ICallback callback = new ICallback.Stub() {
    @Override
    public void fire() throws RemoteException {
        mTextView.setText("fired");
    }
};

//then in onCreate i add:
mManger.registerCallback(callback);

ICallback (AIDL)

interface ICallback {
    void fire();
}

Manager:

public void registerCallback(ICallback callback) {
    try {
        mService.registerCallback(callback);
    } catch (RemoteException e) {
        Log.e(TAG, "Service is dead");
    }
}

private void notifyCallbacks() {
    try {
        mService.notifyCallbacks();
    } catch (RemoteException e) {
        Log.e(TAG, "Service is dead");
    }
}

Service:

public void registerCallback(ICallback callback) {
    if (callback != null) {
         mCallbacks.register(callback);
    }
}

public void notifyCallbacks() {
    final int N = mCallbacks.beginBroadcast();

    for (int i=0;i<N;i++) {
        try {
            mCallbacks.getBroadcastItem(i).fire();
        } catch (RemoteException e) {
        }
    }
    mCallbacks.finishBroadcast();
}

My callbacks get notified but I run into this when it try to set the textview text:

E/JavaBinder﹕ * Uncaught remote exception! (Exceptions are not yet supported across processes.) android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Upvotes: 0

Views: 16609

Answers (1)

tknell
tknell

Reputation: 9047

Like the error message says, you try to update a View from the wrong Thread. As the fire() method is called from the remote Service that runs in another Thread, you need to make sure, that the code that updates the UI runs in the UI Thread. To achieve this, try the following:

public void fire() throws RemoteException {

    //the activity provides this method to be able to run code in the UI Thread
    runOnUiThread(new Runnable(){

        @Override
        public void run(){
            mTextView.setText("fired");
        }
    })
}

Upvotes: 0

Related Questions