java编码:requestfocus()_java – requestaudiofocus的正确参数是什么?

我是Android和Java的新手.我一直在使用Android Developer和其他网站提供的MediaPlayer和AudioManager示例.

AudioManager audioManager = (AudioManager) getSystemService(Context.AUdio_SERVICE);

int result = audioManager.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUdioFOCUS_GAIN);

if (result != AudioManager.AUdioFOCUS_REQUEST_GRANTED) {

// could not get audio focus.

}

使用以下文本:

“requestAudioFocus()的第一个参数是AudioManager.OnAudioFocusChangeListener,只要音频焦点发生变化,就会调用其onAudioFocusChange()方法.因此,您还应该在服务和活动上实现此接口.例如:”

(使用以下代码:)

class MyService extends Service

implements AudioManager.OnAudioFocusChangeListener {

// ....

public void onAudioFocusChange(int focusChange) {

// Do something based on focus change...

}

}

OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {

public void onAudioFocusChange(int focusChange) {

if (focusChange == AUdioFOCUS_LOSS_TRANSIENT) {

// Pause playback

} else if (focusChange == AudioManager.AUdioFOCUS_GAIN) {

// Resume playback

} else if (focusChange == AudioManager.AUdioFOCUS_LOSS) {

am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);

am.abandonAudioFocus(afChangeListener);

// Stop playback

}

}

};

AudioManager am = mContext.getSystemService(Context.AUdio_SERVICE);

...

// Request audio focus for playback

int result = am.requestAudioFocus(afChangeListener,// Use the music stream.

AudioManager.STREAM_MUSIC,// Request permanent focus.

AudioManager.AUdioFOCUS_GAIN);

if (result == AudioManager.AUdioFOCUS_REQUEST_GRANTED) {

am.registerMediaButtonEventReceiver(RemoteControlReceiver);

// Start playback.

}

我已经在众多网站上看到了这种二分法,为处理音频焦点的变化提供了示例代码.

我的理解是“this”提供了应用程序当前状态的上下文.

我不明白为什么在某些情况下“this”是正确的参数,而在其他情况下,在调用requestAudioFocus()时,更改侦听器的句柄是正确的参数.

实际上我提供的第一个例子说明第一个参数应该是AudioManager.OnAudioFocusChangeListener.但是使用“这个”.

如果您可以解释为什么使用“this”而不是AudioManager.OnAudioFocusChangeListener用作参数,我们将不胜感激.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入姓名" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:ems="10" android:hint="在这里输入姓名" > <requestFocus /> </EditText> <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="93dp" android:layout_toRightOf="@+id/button1" android:ems="10" android:inputType="date" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/editText3" android:layout_centerHorizontal="true" android:layout_marginBottom="41dp" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/editText2" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:text="按下" /> <EditText android:id="@+id/editText4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignRight="@+id/editText2" android:layout_marginBottom="59dp" android:ems="10" android:inputType="date" /> </RelativeLayout>
07-22

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值