单击小组件按钮时执行多个功能

时间:2018-06-13 13:37:42

标签: android android-widget android-pendingintent android-music-player

我正在尝试为我的音乐播放器创建一个小部件。窗口小部件布局由三个imageButtons和一个ImageView组成。这三个按钮执行以下任务:跳到下一首歌曲,播放/暂停歌曲,跳到上一首歌曲。现在我想要的是当我点击跳到上一首歌曲按钮/跳到下一首歌曲按钮时,我希望按钮发送广播以及用当前歌曲专辑艺术更新ImageView

我的问题:按下按钮时,按钮执行单个功能。我希望他们执行多种功能。

 private PendingIntent getArtUpdatePendingIntent(Context context, int appWidgetId){

        Intent intentUpdate = new Intent(context, getClass());
        intentUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        int[] idArray = new int[]{appWidgetId};
        intentUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, idArray);

        PendingIntent pendingUpdate = PendingIntent.getBroadcast(context, appWidgetId, intentUpdate, PendingIntent.FLAG_UPDATE_CURRENT);

        return pendingUpdate;
    }

上面的代码有助于在点击按钮时更新ImageView。但后来我无法发送广播。

代码:

private static final String previousSongOnClick = "previousSongOnClick";
    private static final String playPauseSongOnClick = "playPauseSongOnClick";
    private static final String nextSongOnClick = "nextSongOnClick";
    private StorageUtil storageUtil;


     void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
                          final int appWidgetId) {

        Log.e("Widget","UpdateAppWidget");
        // Construct the RemoteViews object
        final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.musicana_widget);

        storageUtil = new StorageUtil(context);//storageUtil.loadAudio().get(storageUtil.loadAudioIndex()).getAlbumIDArtwork()


     remoteViews.setOnClickPendingIntent(R.id.previousSongButton, getPendingSelfIntent(context, previousSongOnClick,appWidgetId));
     remoteViews.setOnClickPendingIntent(R.id.playPauseButton, getPendingSelfIntent(context, playPauseSongOnClick,appWidgetId));
     remoteViews.setOnClickPendingIntent(R.id.nextSongButton,getPendingSelfIntent(context, nextSongOnClick,appWidgetId));


     Glide.with(context).asBitmap()
             .load(storageUtil.loadAudio().get(storageUtil.loadAudioIndex()).getAlbumIDArtwork())
             .into(new SimpleTarget<Bitmap>() {
                 @Override
                 public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                     remoteViews.setImageViewBitmap(R.id.albumArt,resource);
                     appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
                     Log.e("Widget","Inside glide");
                 }
             });



     // Instruct the widget manager to update the widget

}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    Log.e("Widget","onUpdate");

    // There may be multiple widgets active, so update all of them

    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}

@Override
public void onEnabled(Context context) {
    // Enter relevant functionality for when the first widget is created
}

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

@Override
public void onReceive(final Context context, Intent intent) {
    super.onReceive(context, intent);

    if (intent.getAction().equals(previousSongOnClick)) {

        Log.e("Widget", "previous Song button clicked");
        context.sendBroadcast(new Intent(ConstantsForBroadCast.ACTION_PLAY_PREVIOUS));
        updateAppWidget(context,AppWidgetManager.getInstance(context),R.id.albumArt);

    } else if (intent.getAction().equals(playPauseSongOnClick)) {

        Log.e("Widget", "play/pause Song button clicked");

    } else if (intent.getAction().equals(nextSongOnClick)) {

        Log.e("Widget", "next Song button clicked");
        context.sendBroadcast(new Intent(ConstantsForBroadCast.ACTION_PLAY_NEXT));
        updateAppWidget(context,AppWidgetManager.getInstance(context),R.id.albumArt);


    }
    else if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {

        Log.e("Widget", "Album art changing");



    }
}

protected PendingIntent getPendingSelfIntent(Context context, String action, int appWidgetId) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

0 个答案:

没有答案