簡體   English   中英

如何將活動連接到小部件?

[英]How to hook up activity to widget?

我制作了whit小部件,它運行完美,按下按鈕的操作就像是一種魅力,但是我需要將活動掛接到此小部件上。 我還需要在這些按鈕事件上調用活動方法。 我搜索了很多帖子,每個人都在制作簡單的小部件,只顯示一些文本。

如果此類未運行,則需要啟動活動;如果此類已在運行,則需要將其鈎住。

這是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);
    }
}

您可以通過小部件發送的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
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM