【发布时间】:2020-04-08 12:51:02
【问题描述】:
在向要添加到我的应用的主屏幕小部件添加操作时,我需要帮助。 我已经在许多线程上搜索了解决方案,但没有一个对我有用。
我要做的就是在单击小部件上的图像按钮后发送一条简单的消息。 如果可行,我想在单击此按钮后运行服务意图。
如果你能添加你用java编写的代码,我会很棒。
这是xml小部件的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#09C"
android:padding="@dimen/widget_margin">
<ImageButton
android:id="@+id/widget_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/on" />
</RelativeLayout>
这是类本身:
public class wow_shortcut extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = wow_shortcutConfigureActivity.loadTitlePref(context, appWidgetId);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wow_shortcut);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
wow_shortcutConfigureActivity.deleteTitlePref(context, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}
谢谢你,祝你有美好的一天!
【问题讨论】:
-
请发布您想要实现的代码,以便社区可以帮助您
-
我自己添加了代码
-
这能回答你的问题吗? Clickable widgets in android
-
不完全是,我不知道将“user2742371”写的代码放在哪里,我尝试了“Mike”的建议,虽然我在点击按钮后设法开始了一个活动,但我没有启动服务(没有突然弹出的 Winodw)
标签: java android widget imagebutton toast