Question

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). The problem is i need to update the widget really often(every second for showing progress) and i often get 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. The main question - How to improve perfomance and avoid 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;

Was it helpful?

Solution

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).

How to avoid this problem? You should to every time create new instance of RemoteViews.

How to improve perfomance? You should to put into RemoteViews only what you want to change and call wm.partiallyUpdateAppWidget(ids,views);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top