Android:可点击的imageview小部件

Android:可点击的imageview小部件,android,android-widget,Android,Android Widget,我想制作一个非常简单的小部件: 它必须仅从一个图像视图组成 1) 在收到短信时,应更改图像 2) 单击它也会更改图像 我尝试使用ImageButton进行设置,但失败:在sms接收事件中更改图像时出现问题:新图像的比例错误 不管怎样,现在我想做一个ImageView,不做任何其他事情 问题是我无法处理onClick事件: 我有一个正在运行的服务,可以处理所有事件:收到短信并单击: 小部件提供商: public class MyWidgetProvider extends AppWidgetPro

我想制作一个非常简单的小部件:

它必须仅从一个图像视图组成

1) 在收到短信时,应更改图像

2) 单击它也会更改图像

我尝试使用ImageButton进行设置,但失败:在sms接收事件中更改图像时出现问题:新图像的比例错误

不管怎样,现在我想做一个ImageView,不做任何其他事情

问题是我无法处理onClick事件:

我有一个正在运行的服务,可以处理所有事件:收到短信并单击:

小部件提供商:

public class MyWidgetProvider extends AppWidgetProvider {
    @Override
    public void onDisabled(Context context) {
        context.stopService(new Intent(context, UpdateService.class));
        super.onDisabled(context);
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Intent intent = new Intent(context, UpdateService.class);
        context.startService(intent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                     int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        for (int i = 0; i < appWidgetIds.length; i++) {
            int appWidgetId = appWidgetIds[i];

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
            Intent widgetClickIntent = new Intent(context, UpdateService.class);
            widgetClickIntent.setAction(UpdateService.ACTION_ON_CLICK);
            PendingIntent pendingIntentViewClick = PendingIntent.getService(context, 0, widgetClickIntent, 0);
            remoteViews.setOnClickPendingIntent(R.id.widget_imageview, pendingIntentViewClick);
            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }
    }
}
舱单:

    <receiver a:name="..."...>
        <intent-filter>
            <action a:name="android.provider.Telephony.SMS_RECEIVED"/>
            <action a:name="android.MyWidget.ACTION_ON_CLICK"/>
        </intent-filter>
        <meta-data a:name="android.appwidget.provider"
                   a:resource="@xml/my_widget_provider_info"/>
    </receiver>
    <service a:name=".UpdateService"
            a:label="UpdateService">

        <intent-filter>
            <action a:name="android.provider.Telephony.SMS_RECEIVED"/>
            <action a:name="android.MyWidget.ACTION_ON_CLICK"/>
        </intent-filter>
    </service>

我找到了解决办法

对于你们中读到这个问题的人,我很抱歉。我明白,里面的代码太多了

问题是UpdateService不是广播意图的真正处理程序。BroadcastReceiver的匿名实现完成了所有工作

所以问题出在这段代码中(widgetProvider):

@覆盖
public void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,
int[]appWidgetIds){
onUpdate(上下文、appWidgetManager、AppWidgetId);
for(int i=0;i
    <receiver a:name="..."...>
        <intent-filter>
            <action a:name="android.provider.Telephony.SMS_RECEIVED"/>
            <action a:name="android.MyWidget.ACTION_ON_CLICK"/>
        </intent-filter>
        <meta-data a:name="android.appwidget.provider"
                   a:resource="@xml/my_widget_provider_info"/>
    </receiver>
    <service a:name=".UpdateService"
            a:label="UpdateService">

        <intent-filter>
            <action a:name="android.provider.Telephony.SMS_RECEIVED"/>
            <action a:name="android.MyWidget.ACTION_ON_CLICK"/>
        </intent-filter>
    </service>
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                 int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    for (int i = 0; i < appWidgetIds.length; i++) {
        int appWidgetId = appWidgetIds[i];

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        // wrong:
        // Intent widgetClickIntent = new Intent(context, UpdateService.class);
        // widgetClickIntent.setAction(UpdateService.ACTION_ON_CLICK);
        // PendingIntent pendingIntentViewClick = PendingIntent.getService(context, 0, widgetClickIntent, 0);
        // correct:
        Intent widgetClickIntent = new Intent(UpdateService.ACTION_ON_CLICK);
        PendingIntent pendingIntentViewClick = PendingIntent.getBroadcast(context, 0, widgetClickIntent, 0);
        ///////
        remoteViews.setOnClickPendingIntent(R.id.widget_imageview, pendingIntentViewClick);
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
    }
}