简体   繁体   English

如何有效地更新应用小部件

[英]How to efficiently update app widget

I'm developing a music player .I want to efficiently update widget and avoid java.lang.IllegalArgumentException: RemoteViews for widget update exceeds maximum bitmap memory usage My widget has three buttons(previous,play,next) ,progress bar(show current position of track) and image view (show cover of current track). 我正在开发一个音乐播放器。我想有效地更新小部件并避免java.lang.IllegalArgumentException: RemoteViews for widget update exceeds maximum bitmap memory usage我的小部件有三个按钮(上一个,播放,下一个),进度条(显示当前位置) (轨道)和图像视图(显示当前轨道的封面)。 The problem is i need to update the widget really often(every second for showing progress) and i often get IllegalArgumentException. 问题是我需要经常更新窗口小部件(每秒显示进度)并且我经常得到IllegalArgumentException。 See my code below. 请参阅下面的代码。 Brief explanation : I store widget view in global variable (for better perfomance). 简要说明:我将小部件视图存储在全局变量中(为了更好的性能)。 startAlarm() - starting alarm manager to send broadcast every second. startAlarm() - 启动警报管理器每秒发送一次广播。 The main question - How to improve perfomance and avoid IllegalArgumentException? 主要问题 - 如何提高性能并避免IllegalArgumentException?

private static RemoteViews views;
@Override
    public void onReceive(Context context, Intent intent)
    {
        super.onReceive(context, intent);

        String action = intent.getAction();
        AppWidgetManager wm = AppWidgetManager.getInstance(context);
        ComponentName cm = new ComponentName(context,SmallBiggerWidget.class);
        int[] ids = wm.getAppWidgetIds(cm);
        if(ids.length < 1)
            return;

        if(views == null){
            views = getViews(context);
        }else if(action.equals(ACTION_TRACK_CHANGED)){
            setNewAudioToView(context,views);
            setIconPlayToViews(views);
            setProgressTotView(views);
        }else if(action.equals(ACTION_PAUSE) || action.equals(ACTION_PLAY)){
            setIconPlayToViews(views);
        }else if(action.equals(ACTION_PROGRESS_UPDATE)){
            setProgressTotView(views);
        }else if(action.equals(PlayingService.ACTION_RELOAD_IMAGE)){
            setNewImageToView(PlayingService.audios.get(PlayingService.indexTrack),context,views);
        }
       for(int id : ids){
                wm.updateAppWidget(id,views);
            }
        if(isPlaying){
            startAlarm(context);
        }else{
            cancelAlarm(context);
        }
    }

The couse of IllegalArgumentException isn't big cover (i scale cover for small size) ,also i get this error even without imageView; IllegalArgumentException的大小不是很大的封面(我为小尺寸缩放封面),即使没有imageView我也会得到这个错误;

I have to answer on my own question. 我必须回答我自己的问题。 The cause of java.lang.IllegalArgumentException was static variable RemoteViews views because when i call views.setTextViewText(R.id.text,text) this put text into RemoteViews but previous text isn't deleted ( RemoteViews contains all texts that i put to him). java.lang.IllegalArgumentException的原因是静态变量RemoteViews views因为当我调用views.setTextViewText(R.id.text,text)这会将文本放入RemoteViews但之前的文本不会被删除( RemoteViews包含我放入的所有文本他)。

How to avoid this problem? 如何避免这个问题? You should to every time create new instance of RemoteViews . 您应该每次都创建RemoteViews新实例。

How to improve perfomance? 如何提高性能? You should to put into RemoteViews only what you want to change and call wm.partiallyUpdateAppWidget(ids,views); 您应该只将要更改的内容放入RemoteViews并调用wm.partiallyUpdateAppWidget(ids,views);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM