从主屏应用小部件启动服务的更好方法

时间:2013-12-26 16:54:36

标签: android android-appwidget

我的示例应用中有以下代码。单击按钮时,我正在从应用程序小部件启动服务。该服务的工作是播放一个短的音频剪辑,取决于单击了哪个按钮。

app小部件包含两个按钮(PREV和NEXT)。点击事件在onUpdate()中处理。

从我在网上找到的不同指南中,我可以通过两种方式启动服务:

  1. 通过在onUpdate()方法中创建广播并稍后在扩展的AppWidgetProvider类的onReceive()方法中处理它(如在PREV按钮的示例代码中)。
  2. 或通过onUpdate()中的PendingIntent调用startService()(作为NEXT按钮)。
  3. 哪一项更好或更常用?谢谢

    <!-- widget_player.xml -->
    
    <LinearLayout
        android:id="@+id/player_controls"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal" >
    
        <ImageButton
            android:id="@+id/btn_player_prev"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@android:drawable/ic_media_previous" />
    
        <ImageButton
            android:id="@+id/btn_player_next"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@android:drawable/ic_media_next" />
    
    </LinearLayout>
    

    WidgetPlayer类

    public class WidgetPlayer extends AppWidgetProvider {
    
    public static String ACTION_WIDGET_PREV = "action.WIDGET_PREV";
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    
        final int nPlayerWidgets = appWidgetIds.length;
    
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_player);
    
        for (int i = 0; i < nPlayerWidgets; i++) {
            int appWidgetId = appWidgetIds[i];
            updateWidgetPlayer(context, appWidgetManager);
    
            Intent intent;
            PendingIntent actionPendingIntent;
    
            // PREV button
            intent = new Intent(context, WidgetPlayer.class);
            intent.setAction(ACTION_WIDGET_PREV);
            actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            remoteViews.setOnClickPendingIntent(R.id.btn_player_prev, actionPendingIntent);
    
            // NEXT button (not using a broadcast)
            intent = new Intent(context, PlayerService.class);
            intent.setAction(PlayerService.ACTION_NEXT);
            actionPendingIntent = PendingIntent.getService(context, 0, intent, 0);
            remoteViews.setOnClickPendingIntent(R.id.btn_player_next, actionPendingIntent);
    
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
    
        if (intent.getAction().equals(ACTION_WIDGET_PREV)) {
            Intent iPrev = new Intent(PlayerService.ACTION_PREV);
            iPrev.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetMetaPlayerIds);
            context.startService(iPrev);
            updateWidgetPlayer(context, manager);
        }
    
        // handle more actions here
    
        else {
            super.onReceive(context, intent);
        }
    }
    }
    

1 个答案:

答案 0 :(得分:0)

NEXT按钮代码看起来更简洁,因此如果它符合您的要求,它是更好的选择。使用PREVIOUS代码,您可以获得额外的接收广播的步骤,这似乎是不必要的。

如果您需要,可以将意图中的id传递给服务。