Android - Unsupported Service: audio

后端 未结 5 1230
挽巷
挽巷 2021-01-04 05:42

I am trying to understand and resolve and error I am seeing in the Eclipse workspace log while working on an Android app that implements an IME. I am new to Android and Ecli

5条回答
  •  遥遥无期
    2021-01-04 06:34

    I improved the solution by @Enyvy by extending ContextWrapper instead of Context (far less code). Uses ContextWrapper class with delegation of all methods to the base Context, except for the getService() method that is forbid to ask for "audio":

    public class ContextWrapperFix extends ContextWrapper {
        private boolean editMode;
    
        public ContextWrapperFix(Context context, boolean editMode) {
            super(context);
            this.editMode = editMode;
        }
    
        public Object getSystemService(String name) {
            if (editMode && Context.AUDIO_SERVICE.equals(name)) {
                return null;
            }
            return super.getSystemService(name);
        }
    }
    

    Next step is create own class extends KeyboardView:

    public class KeyboardViewFix extends KeyboardView {
        public static boolean inEditMode = true;
    
        @TargetApi(21) // Build.VERSION_CODES.L
        public KeyboardViewFix(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(new ContextWrapperFix(context, inEditMode), attrs, defStyleAttr, defStyleRes);
        }
    
        public KeyboardViewFix(Context context, AttributeSet attrs, int defStyleAttr) {
            super(new ContextWrapperFix(context, inEditMode), attrs, defStyleAttr);
        }
    
        public KeyboardViewFix(Context context, AttributeSet attrs) {
            super(new ContextWrapperFix(context, inEditMode), attrs);
        }
    
    }
    

    And use KeyboardViewFix as replace KeyboardView.

    One note: On start your app, before any other code you need set KeyboardViewFix.inEditMode = false; or you can get some errors.

提交回复
热议问题