[英]Update widget on Volume changes
My app has 1 activity and 1 widget. 我的应用程序有1个活动和1个小部件。 I need to update my widget UI when users change the devices volume
用户更改设备音量时,我需要更新我的小部件UI
In activity I can do it by registering a content observer: 在活动中,我可以通过注册内容观察者来做到这一点:
public class SettingsContentObserver extends ContentObserver {
private AudioManager audioManager;
public SettingsContentObserver(Context context, Handler handler) {
super(handler);
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
@Override
public boolean deliverSelfNotifications() {
return false;
}
@Override
public void onChange(boolean selfChange) {
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
// Do something
}
}
But with widget I do not know what to do. 但是用小部件我不知道该怎么办。 Can any help?
可以帮忙吗? Thanks in advance!
提前致谢!
To achieve that, you need to make a Service with your ContentObserver , and onChange() just make a new RemoteViews. 为此,您需要使用ContentObserver创建服务 ,而onChange()仅创建一个新的RemoteViews。
So in OnChange() something like that: 因此在OnChange()中类似这样:
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
int widgetIds[] = appWidgetManager.getAppWidgetIds(new ComponentName(context,
WidgetClass.class));
RemoteViews views = new RemoteViews(context, R.id.widget_layout);//configure here your remoteViews
appWidgetManager.updateAppWidget(widgetIds, views);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.