【问题标题】:How to hook up activity to widget?如何将活动连接到小部件?
【发布时间】:2013-10-31 15:07:30
【问题描述】:

我制作了 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);
    }
}

【问题讨论】:

    标签: android android-widget android-activity


    【解决方案1】:

    您可以通过小部件发送的 Intent 与您的 Activity 进行通信:

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

    【讨论】:

    • 所以基本上我需要在活动本身的这个 onNewIntent 方法中识别意图?
    • 是的,通过 extras 传递数据并在 myIntentHandler 中执行某些操作,如果需要,您甚至可以在其中启动另一个活动
    • 我得到运行时异常,无法实例化接收器 MusicPlayerActivity,java.lang.ClassCastException,类无法转换为 BroadcastReceiver :P 但我不认为它与您的代码有关,只是我不知道在哪里寻找错误。
    • 最终发现错误,但每当我按下小部件活动中的任何按钮时,它的 GUI 都会在小部件上启动。有没有办法在后台运行活动?
    • 你已经掌握了基本的机制,但听起来你真的想使用 SERVICE,而不是 ACTIVITY。见developer.android.com/guide/components/services.html
    猜你喜欢
    • 2011-02-01
    • 2021-12-01
    • 1970-01-01
    • 2014-02-25
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多