2015-05-09 51 views
0

我有一個音樂應用程序需要下一個簡單的播放暫停...你知道的小部件。下面我有代碼,我已經創建了小部件,我遇到的問題是更新函數被調用,但文本視圖不更新。控件只更新某些值

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; 
    } 
} 

Widget類

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類?

回答

0

我發現我的小部件沒有正確更新,但只有當我創建它的新實例時...將以更深入的示例重新發布問題

相關問題