Android:可点击的图像视图小部件

发布于 12-06 13:06 字数 3855 浏览 0 评论 0原文

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

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

1)在传入短信时它应该更改图像

2)在单击时它也应该更改图像

我尝试使用 ImageButton 制作它但失败了:存在问题更改短信上的图像收到的事件:新图像的比例错误。

无论如何,现在我想制作一个 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);
        }
    }
}

服务:

public class UpdateService extends Service {
    static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    static final String ACTION_ON_CLICK = "android.MyWidget.ACTION_ON_CLICK";

    private final static IntentFilter intentFilter;

    static {
        intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_SMS_RECEIVED);
        intentFilter.addAction(ACTION_ON_CLICK);
    }


    public final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(ACTION_SMS_RECEIVED)) {
                reactOnSms(context);
            }
            if (action.equals(ACTION_ON_CLICK)) {
                onCLick(context);
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        registerReceiver(broadcastReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void reactOnSms(Context context) {
        // doSomething
    }

    public void onCLick(Context context) {
        // doSomething
    }

清单:

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

我尝试了这个 android 中的可点击小部件

I want to make a very simple widget:

It must consist from just one image view

1) on incoming sms it should change the image

2) on click it also should change the image

I tried to make it using ImageButton but failed: there were problems with changing the image on sms received event: new image had wrong scale.

Anyway now I want to make an ImageView without anything else.

The problem is that I can't handle onClick event:

I've got a running service which should handle all events: sms received and click:

widget provider:

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

service:

public class UpdateService extends Service {
    static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
    static final String ACTION_ON_CLICK = "android.MyWidget.ACTION_ON_CLICK";

    private final static IntentFilter intentFilter;

    static {
        intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_SMS_RECEIVED);
        intentFilter.addAction(ACTION_ON_CLICK);
    }


    public final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(ACTION_SMS_RECEIVED)) {
                reactOnSms(context);
            }
            if (action.equals(ACTION_ON_CLICK)) {
                onCLick(context);
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        registerReceiver(broadcastReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void reactOnSms(Context context) {
        // doSomething
    }

    public void onCLick(Context context) {
        // doSomething
    }

Manifest:

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

I tried this Clickable widgets in android

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌吟2024-12-13 13:06:46

我找到了解决方案。

对于那些读到这个问题的人,我们深表歉意。里面的代码太多了,我明白了。

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

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

@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);
    }
}

I found the solution.

Sorry for those of you who read the question. Too much code inside, I understand.

The problem was that UpdateService was not the real handler of the broadcast intent. Anonymous implementation of BroadcastReceiver made all the work.

So the problem was in this code (widgetProvider):

@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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文