You should implement the AmbientMode.AmbientCallbackProvider
callback instead.
It is the new preferred method and it still gives you the onEnterAmbient()
, onAmbientUpdate()
, and onExitAmbient()
but also lets you use Activity
(or any sub classes... FragementActivity, etc.). It also allows you to support the Architecture components.
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
}
}