小部件仅更新某些值

时间:2015-05-09 22:07:43

标签: android android-appwidget

我有一个音乐应用程序需要一个简单的播放暂停...你知道小部件。下面我有代码我已经创建了小部件我遇到的问题是正在调用更新函数,但文本视图没有更新。

MusicManager Singleton

    private static MusicManager myInstance;

private boolean songHasChanged;
public MusicManager(MediaplayerUpdateInterface inter) {
    this.player = new MediaPlayer();
    this.uiUpdateInterface = inter;
    executorService.submit(new Runnable() {
        @Override
        public void run() {
 // this thread calls for updates by sending it to mainactivity
            while (true) {
                if(songHasChanged) {
                    uiUpdateInterface.updateUI(10);  
// this is where the request for widget update is called
                    songHasChanged = false;
                }
                if (player.isPlaying()) {
                    uiUpdateInterface.updateUI(0);
                }
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
}

public static MusicManager getInstance(MediaplayerUpdateInterface inter) {
    if (myInstance == null)
        return myInstance = new MusicManager(inter);
    else
        return myInstance;
}

MainActivity MusicManager初始化

public MusicManager musicManager = MusicManager.getInstance(new MusicManager.MediaplayerUpdateInterface() {

    public void updateUI(int type) {
        updateMediaplayerViews(type);
    }
});
// the above should be the first creation and only creation of MusicManager

public void updateMediaplayerViews(int type) {
    switch(type) {
        case 0:
            this.runOnUiThread(new Runnable() {
                public void run() {
                    System.out.println("value? "+(musicManager.getCurrentTimeBAR()/musicManager.getSongLengthBAR()));
                    SongListFragment.musicManagerProgress.setProgress((int)((musicManager.getCurrentTimeBAR()/(double)musicManager.getSongLengthBAR())*100));
                    SongListFragment.musicManagerCurrent.setText(musicManager.getCurrentTime());
                    if(!SongListFragment.hasSetImage) {
                        SongListFragment.musicManagerTotal.setText(musicManager.getSongLength());
                        SongListFragment.musicManagerSongName.setText(musicManager.getCurrentSongInfo().getName().length() > 22 ? musicManager.getCurrentSongInfo().getName().substring(0, 19) + "..." : musicManager.getCurrentSongInfo().getName());
                        new DownloadListAdapter.DownloadImageTask(SongListFragment.musicManagerImageView).execute(musicManager.getCurrentSongInfo().getImgurl());
                        SongListFragment.hasSetImage = true;
                    }
                }
            });
            break;
        case 10:
//the below is the thread that send the intent to update widgets
            this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                   Intent intent = new Intent(getBaseContext(), TrinityWidget.class);
                    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                    int[] ids = {TrinityWidget.widgetId};
                    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
                    getBaseContext().sendBroadcast(intent);
                }
            });
            break;
    }
}

小工具类

public class TrinityWidget extends AppWidgetProvider {

public static boolean needsImage;
public static final int widgetId = 188772;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
    }
}

// the method below is called when widget is created and when called to update
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                   int appWidgetId) {
    MusicManager manager = MusicManager.getInstance();


    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.trinity_widget);

    if(manager != null) {
        views.setTextViewText("random text");
        views.setTextViewText(R.id.widgetartisttext, "random");
// ok so the above two statements dont set the text at all
    } else
    System.out.println("was null");

// the above was null is never printed ...

    appWidgetManager.updateAppWidget(appWidgetId, views);
}

@Override
public void onEnabled(Context context) {
}

@Override
public void onDisabled(Context context) {
    // Enter relevant functionality for when the last widget is disabled
}


public static class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }
}
}

所以我的主要关注点是如何链接我的小部件以完全访问我的MusicManager类?

1 个答案:

答案 0 :(得分:0)

我发现我的小部件确实正确更新,但只有在我创建它的新实例时......才会以更深入的示例重新发布问题