繁体   English   中英

Android小部件-使用setOnClickPendingIntent和remoteviews无法接收到Intent

[英]Android widget - Intent not received using setOnClickPendingIntent with remoteviews

我目前正在使用三个ImageButtons创建我的第一ImageButtons部件。 我已经非常严格地遵循android中Clickable小部件描述的答案,但是我无法使其正常工作。

我定义了三个字符串,它们是我的动作:

private String playAction = "playService";
private String stopAction = "stopService";
private String resetAction = "resetService";

接下来,在我的OnUpdate()函数中,添加setOnclickPendingIntent

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

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

    remoteViews.setOnClickPendingIntent(R.id.widget_play, getPendingSelfIntent(context, playAction));
    remoteViews.setOnClickPendingIntent(R.id.widget_pause, getPendingSelfIntent(context, stopAction));
    remoteViews.setOnClickPendingIntent(R.id.widget_reset, getPendingSelfIntent(context, resetAction));

    manager = appWidgetManager;

    // Build the intent to call the service
    Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);

    // Update the widgets via the service
    context.startService(intent);
}

getPendingSelfIntent函数的定义如下:

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

但是, onReceive函数不会接收到以上定义的操作。 目前,我记录了所有传入意图的动作,但是唯一通过的动作是android.appwidget.action.APPWIDGET_UPDATE动作。

为了完整起见,这就是我在清单中定义动作的方式:

    <receiver
        android:name="MyWidgetProvider"
        android:icon="@drawable/ic_launcher"
        android:label="My Widget">

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_info" />

        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="playService" />
            <action android:name="stopService" />
            <action android:name="resetService" />
        </intent-filter>
    </receiver>

我已经在Android文档中了解到,不能使用setOnClickPendingIntent函数设置集合中的元素( ListView等)。 我的布局中有三个按钮。 为确保这不会引起错误,XML如下所示:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/widget_play"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:layout_marginRight="5dp"
            android:src="@drawable/run"/>

        <ImageButton
            android:id="@+id/widget_pause"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:layout_marginRight="5dp"
            android:src="@drawable/pause"/>

        <ImageButton
            android:id="@+id/widget_reset"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/reset"/>

    </LinearLayout>

感谢我的全力以赴,对此事的任何帮助都将受到赞赏。 我已经搜索了类似的问题,但是它们都描述了我已经尝试过的问题。

我认为您在设置待处理的意图后,缺少对窗口小部件的更新:

appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

如果这样做没有帮助,您也应该发布UpdateWidgetService.class。

顺便说一句,UpdateWidgetService.class是做什么的?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM