Android App Widget广播不起作用

时间:2012-06-28 18:15:14

标签: java android android-widget

我有一个包含1个图像视图和1个文本视图的小部件。

on off

我想在点击时更改图片。考虑以下代码没有捕获触摸事件

package com.sigrlami.rixvpn.widget;

import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

import com.sigrlami.rixvpn.R;

public class VpnWidgetProvider extends AppWidgetProvider {

private boolean currentStatus = false;
// log tag
private static final String LOG = "com.sigrlami.rixvpn.widget.VpnWidgetProvider";

//public static final String CLICK = "com.sigrlami.rixvpn.CLICK";

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

    // Get all ids
    ComponentName thisWidget = new ComponentName(context, VpnWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    for (int widgetId : allWidgetIds) {

        // Create some random data
        int number = (new Random().nextInt(100));

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.vpn_widget);
        Log.w("WidgetExample", String.valueOf(number));

        // Set the text
        remoteViews.setTextViewText(R.id.layout_vpn_widget_tv_Check, String.valueOf(number));

        // Register an onClickListener
        Intent intent = new Intent(context, VpnWidgetProvider.class);

        intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        remoteViews.setOnClickPendingIntent(R.id.layout_vpn_widget_iv_Check, pendingIntent);

        if (currentStatus = false) {
            remoteViews.setImageViewResource(R.id.layout_vpn_widget_iv_Check, R.drawable.on);

        } else {
            remoteViews.setImageViewResource(R.id.layout_vpn_widget_iv_Check, R.drawable.off);
        }

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }

}

@Override
public void onReceive(Context context, Intent intent) {

    final String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
        if (currentStatus == false) {

            currentStatus = true;

        } else {
            currentStatus = false;
        }

    }
    super.onReceive(context, intent);
}

}

通过调试器查看此部分并不奇怪,但是当我更改为getActivity()时,它表明我正在尝试启动一个不存在的活动。我发现事情正在发生,但这对我来说毫无用处。

// Register an onClickListener
Intent intent = new Intent(context, VpnWidgetProvider.class);

intent.setAction(appWidgetManager.ACTION_APPWIDGET_UPDATE);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

remoteViews.setOnClickPendingIntent(R.id.layout_vpn_widget_iv_Check, pendingIntent);

但是当应用程序部署到设备时,它可以工作一次。 有什么建议我做错了吗?

1 个答案:

答案 0 :(得分:4)

要处理AppWidgetProvider中的点击,您需要使用自定义广播接收器:

<receiver android:name="VpnWidgetProvider"
            android:label="@string/widget_title" android:exported="false" android:icon="@drawable/volume">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.xxxx.xxxx.VpnWidgetProvider.ACTION_WIDGET_REFRESH"/>
                <action android:name="android.appwidget.action.APPWIDGET_DELETED"/>
                <action android:name="android.media.RINGER_MODE_CHANGED"/>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />

并在AppWidgetProvider类中:

public class VpnWidgetProvider extends AppWidgetProvider {
public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh";
//your code here...
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//you code here...

Intent intent = new Intent(context, VpnWidgetProvider.class);
intent.setAction(appWidgetManager.ACTION_WIDGET_REFRESH);//get action here
//you code here...

在主屏幕AppWidget上查看这个完整的工作示例:

Silenttoggle Home Screen Widget