Back Swipe not working in Google Glass Activity

有些话、适合烂在心里 提交于 2020-01-05 08:21:41

问题


I've created a basic activity for Google Glass, but it will not allow me to use the standard down swipe to go back in the stack to the previous activity. I've tried giving the activity focus with this code:

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.layout_orienation_detection_activity);

    mView = getWindow().getDecorView().findViewById(android.R.id.content);
    mView.setFocusable(true);
    mView.setFocusableInTouchMode(true);
}

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

    mGestureDetector = createGestureDetector(this);

    serviceIntent = new Intent(this, SensorService.class);
    startService(serviceIntent);

    mView.requestFocus();
}

I've also tried creating my own down swipe features using the onKeyDown() method, and I've tried creating a Gesture detector for both TAP and SWIPE_DOWN, and then adding finish() to their respective callbacks. My gesture and key detector code looks exactly the same as the code on the Google Developers site at https://developers.google.com/glass/develop/gdk/touch, but still nothing works. I figure this shouldn't be necessary anyways, as Glass's standard back swipe ought to work anyways.

I don't know if this has anything to do with it, but my activity starts a service.

Also, sometimes it works the first time after I install the application, but then it stops working immediately after that.

Does anyone have any ideas as to what the problem is? It's very frustrating having to turn off the glass device or wait for the screen to timeout in order to exit the activity.

EDIT: The problem is with starting a service from the activity, as when I deleted the startService(intent) call from my activity, the down swiping worked fine. So how can I still run the background service, but also be able to down swipe?


回答1:


Welcome to doing things on Google Glass -- where not everything works as it should. I've had a slew of issues, but have found some workarounds for some. Either way, setting up the first gesture detection was tricky when I did it to. I'll provide you two working options I have right now running in a glass app I made.

Via onKeyDown():

    import android.view.KeyEvent;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if( keyCode == KeyEvent.KEYCODE_BACK ) {
            // handles swipe down
            return true;
        }
        else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            // handles tap
            return true;
        }
        return false;
    }

Setting up gesture detection is trickier, but it looks like you're missing a lot of code from what you posted. Here's what I have working right now in the app for gesture detection, Ill include just the relevant bits:

    import android.view.MotionEvent;

    import com.google.android.glass.touchpad.Gesture;
    import com.google.android.glass.touchpad.GestureDetector;

    private GestureDetector mGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //instantiate the gesture detector
        mGestureDetector = createGestureDetector(this);
    }

    // Actually create the gesture detector
    private GestureDetector createGestureDetector(Context context) {
        GestureDetector gestureDetector = new GestureDetector(context);
        gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
            @Override
            public boolean onGesture(Gesture gesture) {
                if (gesture == Gesture.SWIPE_RIGHT) { 
                    // pickup right swipe
                    return true;
                } else if (gesture == Gesture.SWIPE_LEFT) {                         
                    // pickup left swipe
                    return true;
                } else if (gesture == Gesture.SWIPE_DOWN) {
                    // pickup down swipe
                    return true;
                } else if (gesture == Gesture.TAP) {
                    // pickup tap
                    return true;
                }
                return false;
            }
        });
        return gestureDetector;
    }

    //Send generic motion events to the gesture detector
    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        if (mGestureDetector != null) {
            return mGestureDetector.onMotionEvent(event);
        }
        return false;
    }

As far as you saying this:

    Also, sometimes it works the first time after I install the application, but then it stops working immediately after that.

I have experienced similar problems that I'm still currently trying to resolve. Its hard to troubleshoot that though without knowing more. Have you tried updating the firmware on the Glass? Check that its up to date, otherwise what else are you doing in the code? Maybe you have an issue juggling state information or something if you're switching between activities? Just a guess..

Note: Gesture detection and Key Events for left and right swipes are broken on Glass if you're doing osmething with a MediaPlayer for example. We create a VideoView in a piece of code, and can only detect swipe down and tap and nothing else but we experience this ONLY when in a block of code that is utilizing VideoView/MediaPlayer.



来源:https://stackoverflow.com/questions/28092614/back-swipe-not-working-in-google-glass-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!