接收卷更改通知时长时间延迟



我有一个应用程序,正在从非常旧的版本更新到Android Pie。当用户按下侧键时,它会对手机铃声音量的变化做出响应。我的代码在下面。我的targetSdkVersion是28

我有两个硬件设备。在棉花糖Galaxy S3上,一切都很好,但在Pie Pixel 2设备上,有时从我更改铃声音量到我的内容观察者收到onChange呼叫之间会有很长的延迟。当将振铃器从打开切换到关闭时,延迟通常约为5秒,但有时可能为30秒或更长。一般来说,从振铃器关闭到振铃器打开,速度更快。

这是什么原因造成的?

public class VolumeService extends Service
{
private VolumeService.VolumeContentObserver observer = null;
private static Notification notification = null;
private static int notificationID = 1;
@Override
public void onCreate()
{
super.onCreate();
observer = new VolumeService.VolumeContentObserver( this );
Intent mainIntent = new Intent( this, MainActivity.class );
mainIntent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK );
Notification.Builder builder = new Notification.Builder( this )
.setContentTitle( getString( R.string.notification_title ) )
.setContentText( getString( R.string.notification_text ) )
.setSmallIcon( R.drawable.ic_audio_vol )
.setContentIntent( PendingIntent.getActivity( this, 0, mainIntent, 0 ) );
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O )
builder.setChannelId( getString( R.string.channel_id ) );
notification = builder.build();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
observer.register();
startForeground( notificationID, notification );
return START_STICKY;
}
@Override
public IBinder onBind( Intent intent )
{
return null;
}
private class VolumeContentObserver extends ContentObserver
{
private Context context = null;
VolumeContentObserver( Context context )
{
super( new Handler() );
this.context = context;
}
void register()
{
context.getApplicationContext().getContentResolver()
.registerContentObserver( android.provider.Settings.System.CONTENT_URI, true, this );
}
@Override
public void onChange(boolean selfChange)
{
super.onChange( selfChange );
Log.d("VolumeService", "volume changed");
}
}
}

我发现在某些设备中,您在ContentObserver中没有收到volume_ringvolume_music的更改。您需要使用带有intentFilterRINGER_MODE_CHANGED_ACTION的广播接收器。我认为pixel也使用这个广播接收器。RINGER_MODE_CHANGED_ACTION还用于识别静音、大声等音量模式。

另请参阅此链接

最新更新