Search code examples
androidandroid-activityoptionmenu

Open option menu from service?


I am developing android application in which i have to open option menu from service without any button press. I have tried this

Activity.openOptionsMenu();

But it is showing error "Cannot make a static reference to the non-static method openOptionsMenu() from the type Activity "

Any help !! Thank you .


Solution

  • First, this is not directly possible. A service does not have access to an activity's options menu. There might not even be an activity of yours in the foreground.

    Second, not all devices will have an options menu, as you move into Android 3.0+.

    Third, even for Android 1.x and 2.x devices, the only time the options menu should be opened is because the user presses the MENU button. Anything else is highly suspect from an UX standpoint.

    You are welcome to have your service attempt to contact a foreground activity of yours (e.g., via a BroadcastReceiver hooked up to LocalBroadcastManager) to tell that activity that something happened in the background. That activity is welcome to inform the user of this event in some fashion that makes sense to the user and will work on today's and tomorrow's devices.