如何将活动挂钩到小部件?

时间:2013-10-22 16:13:03

标签: android android-widget android-activity

我制作了小部件,它运行得很好,按钮按下就像魅力,但我需要将活动连接到这个小部件。我还需要在这些按钮事件上调用活动方法。我搜索了许多帖子,每个人都在制作简单的小部件,只显示一些文字。

如果没有运行,则需要启动活动,如果已经运行,则需要挂钩。

这是AppWidgetProvider类:

import android.app.PendingIntent;
import android.appwidget.*;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera.PreviewCallback;
import android.widget.RemoteViews;

public class MusicWidget extends AppWidgetProvider {
    private static final String PLAY_CLICKED = "MusicWidget.PLAY";
    private static final String PREV_CLICKED = "MusicWidget.PREV";
    private static final String NEXT_CLICKED = "MusicWidget.NEXT";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        RemoteViews remoteViews;
        ComponentName watchWidget;

        remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);
        watchWidget = new ComponentName(context, MusicWidget.class);
        remoteViews.setOnClickPendingIntent(R.id.wbtnPlay,
                getPendingSelfIntent(context, PLAY_CLICKED));
        remoteViews.setOnClickPendingIntent(R.id.wbtnPrevious,
                getPendingSelfIntent(context, PREV_CLICKED));
        remoteViews.setOnClickPendingIntent(R.id.wbtnNext,
                getPendingSelfIntent(context, NEXT_CLICKED));
        appWidgetManager.updateAppWidget(watchWidget, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        System.err.println("RECEIVE!");
        if (PLAY_CLICKED.equals(intent.getAction())) {

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

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

        }
    }

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

1 个答案:

答案 0 :(得分:1)

您可以通过窗口小部件发送的Intent与您的活动进行通信:

// WidgetProvider.class

Intent launchIntent = new Intent(context, YourActivity.class);
launchIntent.setAction(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchIntent.putExtra("com.Company.App.LaunchWidget", true);    // an extra to tell your activity its being launced from the widget
.
. // set additional extras to pass data to your activity
.
context.startActivity(launchIntent);

CATEGORY_LAUNCHER和ACTION_MAIN将确保您的主要活动在未运行时启动。

在您的活动中,您将收到onCreate(如果需要创建活动)或onNewIntent(如果活动已经运行)的意图

// YourActivity.class

@Override
public void onCreate(Bundle savedInstanceState)
{
  .
  .
  .
  myIntentHandler(getIntent());
}

@Override 
void onNewIntent(Intent intent)
{
  myIntentHandler(intent);
}

void myIntentHandler(Intent intent)
{
 Bundle extras = intent.getExtras();
 if ((extras != null) && (extras.getBoolean("com.Company.App.LaunchWidget") == true))
 {
  // this intent is from the widget
 }
}