Search code examples
androidgoogle-glassgoogle-gdk

In Google Glass, Menu Items are not shown after XE 17.2 Update, any Solutions?


This worked when the Glass in on XE12, I have opened the solution after about 2 Months and now with XE17 the menu items are not shown when tapped on the Live card, instead the live card is disappearing.
I have updated the GDK, I have changed the code to support the latest GDK sneak peek version 2 changes according to this (https://developers.google.com/glass/release-notes#xe12)

This is the code

public class MenuActivity extends Activity {

private static final String TAG = MenuActivity.class.getSimpleName();

private VisionService.VisionBinder mVisionService;
private ServiceConnection mConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        if (service instanceof VisionService.VisionBinder) {
            mVisionService = (VisionService.VisionBinder) service;
            openOptionsMenu();
        }
        // No need to keep the service bound.
        unbindService(this);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
};
private boolean mResumed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    bindService(new Intent(this, VisionService.class), mConnection, 0);
}

@Override
protected void onResume() {
    super.onResume();
    mResumed = true;
    openOptionsMenu();
}

@Override
protected void onPause() {
    super.onPause();
    mResumed = false;
}

@Override
public void openOptionsMenu() {
    if (mResumed && mConnection != null) {
        super.openOptionsMenu();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_send) {
        mVisionService.requestWorkOrderCard();
        finish();
        return true;
    } else if (id == R.id.action_refresh) {
        mVisionService.requestTopWorkOrders();
        finish();
        return true;
    } else if (id == R.id.action_finish) {
        stopService(new Intent(this, VisionService.class));
        finish();
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }
}

@Override
public void onOptionsMenuClosed(Menu menu) {
    super.onOptionsMenuClosed(menu);
}
}

This is the error I am getting

.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

And this Log(not error) too

Rebinding view with mis-matched type: com.google.glass.home.timeline.TimelineApiAdapter

It would be great if any body could help on this. Thank You


Solution

  • You need to change the way menu gets opened for XE 16 or 17. Below is my code that works:

        public class MenuActivity extends Activity {
            private boolean mAttachedToWindow;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            }
    
            @Override
            public void onAttachedToWindow() {
                super.onAttachedToWindow();
                mAttachedToWindow = true;
                openOptionsMenu();
            }
    
            @Override
            public void onDetachedFromWindow() {
                super.onDetachedFromWindow();
                mAttachedToWindow = false;
            }   
    
            @Override
            public void openOptionsMenu() {
                if (mAttachedToWindow) {
                    super.openOptionsMenu();
                }
            }   
    
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.main, menu);
    
                return true;
            }
        ...
        }
    
    
    
        public class AppService extends Service {
            private static final String TAG = "AppService";
            private static final String LIVE_CARD_ID = "HelloGlass";
    
            private AppDrawer mCallback;
            private LiveCard mLiveCard;
    
            @Override
            public void onCreate() {
                super.onCreate();
            }
    
            @Override
            public IBinder onBind(Intent intent) {
                return null;
            }
    
            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
                if (mLiveCard == null) {
                    mLiveCard = new LiveCard(this, LIVE_CARD_ID);
    
                    mCallback = new AppDrawer(this);
                    mLiveCard.setDirectRenderingEnabled(true).getSurfaceHolder().addCallback(mCallback);
    
                    Intent menuIntent = new Intent(this, MenuActivity.class);
                    mLiveCard.setAction(PendingIntent.getActivity(this, 0, menuIntent, 0));
    
                    mLiveCard.publish(PublishMode.REVEAL);
                } 
    
                return START_STICKY;
            }
    ...
    }
    

    Also, the GDK samples Stopwatch and Timer (https://developers.google.com/glass/samples/gdk) have menu-related code that works perfectly well.