0

I have a fragmentActivity (public class WearRunActivity extends FragmentActivity) that I want to be always on on the screen.

I can't setAmbientEnabled(); because it is not an wearableActivity....

How can i keep my app on

cdlc
  • 323
  • 1
  • 8

2 Answers2

1

You can set a flag to keep your activity on:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

However, be very careful and mindful of the user's battery consumption. You can clear this flag as soon as you don't need your activity to stay on by calling

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Ali Naddaf
  • 16,951
  • 2
  • 21
  • 28
0

You should implement the AmbientMode.AmbientCallbackProvider callback instead.

It is the new preferred method and it still gives you all stuff WearableActivity gave you but also lets you use Activity (or any sub classes... FragementActivity, etc.).

Official docs call out the details (and example code):

public class MainActivity extends Activity implements AmbientMode.AmbientCallbackProvider {

    /*
     * Declare an ambient mode controller, which will be used by
     * the activity to determine if the current mode is ambient.
     */
    private AmbientMode.AmbientController mAmbientController;
    …
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    ...
        mAmbientController = AmbientMode.attachAmbientSupport(this);
    }
    ...

    …
    @Override
    public AmbientMode.AmbientCallback getAmbientCallback() {
        return new MyAmbientCallback();
    }
    …

private class MyAmbientCallback extends AmbientMode.AmbientCallback {
    @Override
    public void onEnterAmbient(Bundle ambientDetails) {
             // Handle entering ambient mode
    }

    @Override
    public void onExitAmbient() {
      // Handle exiting ambient mode
     }

    @Override
    public void onUpdateAmbient() {
      // Update the content
    }
}
codingjeremy
  • 5,215
  • 1
  • 36
  • 39