Met this Android code:

/** * This listener gets triggered whenever the audio focus changes * (ie, we gain or lose audio focus because of another app or device). */ private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int focusChange) { if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) { // The AUDIOFOCUS_LOSS_TRANSIENT case means that we've lost audio focus for a // short amount of time. The AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK case means that // our app is allowed to continue playing sound but at a lower volume. We'll treat // both cases the same way because our app is playing short sound files. // Pause playback and reset player to the start of the file. That way, we can // play the word from the beginning when we resume playback. mMediaPlayer.pause(); mMediaPlayer.seekTo(0); } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { // The AUDIOFOCUS_GAIN case means we have regained focus and can resume playback. mMediaPlayer.start(); } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) { // The AUDIOFOCUS_LOSS case means we've lost audio focus and // Stop playback and clean up resources releaseMediaPlayer(); } } }; 

that it is the interface, but I can not understand how it works. It seems that it is impossible to create an interface object using new , and why is there a semicolon after the curly bracket at the end? ))) And yet, the overridden method is automatically called ?? If so, why not understand how it works!

Closed due to the fact that off-topic participants are Roman C , Enikeyschik , 0xdb , aleksandr barakin , Dmitry Kozlov 24 Nov '18 at 15:49 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - aleksandr barakin, Dmitry Kozlov
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • google "java anonymous classes" - Astend Sanferion

1 answer 1

This is an interface variable.

A simple example:

 TextView tv = (TextView) this.findViewById(R.id.hello_world); //Interface View.OnClickListener m_click_itf = new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }; // сам по себе работать не будет tv.setOnClickListener(m_click_itf); 

A source


At the expense of a semicolon, well, that's how it is laid at the interfaces, accept.

It seems you can't create an interface object with new

Where did you read this?

Now, directly, let's go to your code. This is also called callback . It does not work by itself. It is necessary to subscribe to this callback , as well as be sure to unsubscribe in onStop , in order to avoid memory leaks.

If you looked at the code further, well, or used ctrl+f and would hit mOnAudioFocusChangeListener there, you would see that this interface is signed in this way directly on onCreate :

 // Request audio focus so in order to play the audio file. The app needs to play a // short audio file, so we will request audio focus with a short amount of time // with AUDIOFOCUS_GAIN_TRANSIENT. int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); 

And also unsubscribe from this, directly, in onStop :

 // Regardless of whether or not we were granted audio focus, abandon it. This also // unregisters the AudioFocusChangeListener so we don't get anymore callbacks. mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener); 

@Override is a method redefinition for your needs.

For example:

Initially, this method is used to return to the previous activation or closing of the application, not the essence.

 @Override public void onBackPressed() { super.onBackPressed(); } 

You can change it to your needs:

 @Override public void onBackPressed() { Toast.makeText(getApplicationContext(),"Hello World!",Toast.LENGTH_SHORT).show(); } 

And now, instead of exiting activation or transition, a message will be displayed with information.

  • Thanks, understood !! - rarity-
  • And what about the @Override method is it automatically called ?? And why is that? - rarity-
  • @ rarity- updated answer - iFr0z
  • Well, I understood that, I meant, for example, when we set the OnClickListenre event handler, let's say the button overrides the OnClick method, and this method is automatically called when the button is clicked? So I wanted to know how it works? - rarity- Nov.
  • @ rarity- well, yes, every time when you press a button, certain actions that are written inside onClick () - iFr0z will be performed