gpt4 book ai didi

Android:可点击的 ImageView 小部件

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:54 24 4
gpt4 key购买 nike

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

它必须只包含一个 ImageView

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
}

list :

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

我试过这个 Clickable widgets in android

最佳答案

我找到了解决方案。

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

问题在于 UpdateService 不是广播 Intent 的真正处理程序。 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);
}
}

关于Android:可点击的 ImageView 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7547219/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com