Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 小部件只更新某些值_Android_Android Appwidget - Fatal编程技术网

Android 小部件只更新某些值

Android 小部件只更新某些值,android,android-appwidget,Android,Android Appwidget,我有一个音乐应用程序需要一个简单的播放暂停下。。。你知道的。下面是我创建小部件的代码。我遇到的问题是正在调用更新函数,但文本视图没有更新 MusicManager Singleton private static MusicManager myInstance; private boolean songHasChanged; public MusicManager(MediaplayerUpdateInterface inter) { this.player = new Med

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

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;
}
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;
    }
}
main活动MusicManager初始化

    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;
}
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;
    }
}
}
公共类TrinityWidget扩展了AppWidgetProvider{
公共静态布尔需求;
公共静态最终int widgetId=188772;
@凌驾
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
//可能有多个窗口小部件处于活动状态,因此请更新所有窗口小部件
final int N=appWidgetIds.length;
对于(int i=0;i

因此,我主要关心的是如何将我的小部件链接到我的MusicManager类以获得完全访问权限?

我发现我的小部件确实更新正确,但只有在我创建它的新实例时。。。将以更深入的例子重新提出问题