按下音量减小或向上按钮时,VOLUME_CHANGED_ACTION会因同一状态被多次触发。
添加androidmanifest.xml:
<receiver android:name=".VolumeSateReceiver">
<intent-filter>
<actionandroid:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
VolumeSateReceiver.java中的:
public class VolumeSateReceiver extends BroadcastReceiver {
Context pcontext;
@Override
public void onReceive(Context context, Intent intent) {
pcontext = context;
//check the intent something like:
if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {
int newVolume = intent.getIntExtra("android.media.EXTRA_VOLUME_STREAM_VALUE", 0);
int oldVolume = intent.getIntExtra("android.media.EXTRA_PREV_VOLUME_STREAM_VALUE", 0);
if (newVolume != oldVolume) {
//Toast.makeText(pcontext ,"newVolume" +newVolume + " oldVolume" + oldVolume, Toast.LENGTH_SHORT).show();
System.out.println("In onReceive" + "newVolume" +newVolume + " oldVolume" + oldVolume );
}
}
}
}
答案 0 :(得分:0)
这是一个老问题,我仍将尝试为有兴趣的人提供答案。
可能是因为Android一次更改了多个流的音量。
有关VOLUME_CHANGED_ACTION的文档明确指出它包括流类型:
@media screen and(min-width:599px) {
form {
display: grid;
width:600px;
margin: 0 auto;
position: relative;
grid-template-columns:600px;
}
}
@media screen and (max-width:601px){
form {
display: grid;
position: relative;
grid-template-columns:600px;
width:100%;
}
}
Android可以根据某个状态(通话,听音乐等),在一个侧面按钮事件中缩放多个流。 因此,您还必须过滤掉您感兴趣的所有流,例如在您的onReceive函数中:
/**
* @hide Broadcast intent when the volume for a particular stream type changes.
* Includes the stream, the new volume and previous volumes
*
* @see #EXTRA_VOLUME_STREAM_TYPE
* @see #EXTRA_VOLUME_STREAM_VALUE
* @see #EXTRA_PREV_VOLUME_STREAM_VALUE
*/