gpt4 book ai didi

android - 在可点击小部件中获取点击事件的问题

转载 作者:太空狗 更新时间:2023-10-29 12:55:49 25 4
gpt4 key购买 nike

我编写了一个可点击的小部件。我可以看到小部件。但我无法在该小部件中检测到点击事件。如果我单击小部件,则应调用 onReceive 方法。但它没有被调用。谁能指导我可能是什么问题。

源代码

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

public class MyWidget extends AppWidgetProvider {
public static int IDs[];
Context context;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);

System.out.println("...inside receive11....");

if (intent.getAction().equals("com.fedorvlasov.lazylist.NEXT_CLICK")) {
System.out.println("Back Button Clicked :::::::::::: ");

onUpdate(context, AppWidgetManager.getInstance(context), IDs);
// views = new RemoteViews(context.getPackageName(), R.layout.main);
}
if (intent.getAction().equals(
"com.fedorvlasov.lazylist.NEXT_CLICK_NEXT")) {
System.out.println("Onion Button Clicked Button:::::::::::: ");

onUpdate(context, AppWidgetManager.getInstance(context), IDs);
// views = new RemoteViews(context.getPackageName(), R.layout.main);
}
if (intent.getAction()
.equals("com.fedorvlasov.lazylist.TEXTVIEW_CLICK")) {
System.out.println("Next Button Clicked :::::::::::: ");

onUpdate(context, AppWidgetManager.getInstance(context), IDs);
}
System.out.println("...out to receive11....");

}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
IDs = appWidgetIds;
this.context = context;
System.out.println("...inside update....");
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.mywidget);

Intent buttonPrevious = new Intent(context, MyWidget.class);
buttonPrevious.setAction("com.fedorvlasov.lazylist.NEXT_CLICK");
PendingIntent pendingIntentButtonP = PendingIntent.getBroadcast(
context, 0, buttonPrevious, 0);
remoteViews.setOnClickPendingIntent(R.id.mywidget_left,
pendingIntentButtonP);

Intent buttonMiddle = new Intent(context, MyWidget.class);
buttonMiddle.setAction("com.fedorvlasov.lazylist.TEXTVIEW_CLICK");
PendingIntent pendingIntentButtonM = PendingIntent.getBroadcast(
context, 0, buttonMiddle, 0);
remoteViews.setOnClickPendingIntent(R.id.mywidget_middle,
pendingIntentButtonM);

Intent buttonNext = new Intent(context, MyWidget.class);
buttonNext.setAction("com.fedorvlasov.lazylist.NEXT_CLICK_NEXT");
PendingIntent pendingIntentButtonN = PendingIntent.getBroadcast(
context, 0, buttonNext, 0);
remoteViews.setOnClickPendingIntent(R.id.mywidget_right,
pendingIntentButtonN);


}
}

在 list 文件中

<receiver android:name=".MyWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.fedorvlasov.lazylist.NEXT_CLICK" />
<action android:name="com.fedorvlasov.lazylist.NEXT_CLICK_NEXT" />
<action android:name="com.fedorvlasov.lazylist.TEXTVIEW_CLICK" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_info" android:process=":remote" />
</receiver>

最佳答案

您忘记了在 onUpdate() 方法中更新小部件。

在 onUpdate() 方法的末尾写入 appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

关于android - 在可点击小部件中获取点击事件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6983686/

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