【问题标题】:show a simple toast after click on imagebutton on android widget单击 android 小部件上的 imagebutton 后显示一个简单的吐司
【发布时间】: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


【解决方案1】:

从 Android 启动服务

  1. 创建一个扩展 BroadcastReceiver 的类并将其添加到 AndroidManifest 文件中。
  2. 创建一个。 PendingIntent 开始广播。并将其附加到小部件视图。

    val intent = Intent(context, MyBroadcast::class.java)
    
    val pendingIntent = PendingIntent.getBroadcast(
        context,
        MY_REQUEST_CODE,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT
    )
    
    setOnClickPendingIntent(R.id.widget_btn, pendingIntent)
    
  3. 在你的BroadcastReceiver,启动服务。

    class MyBroadcastReceiver : BroadcastReceiver() {
      override fun onReceive(context: Context?, intent: Intent?) {
          // Create new Intent to start your service.
      }
    }
    
  4. 服务可能因后台限制而停止,您可能需要将其置于前台。

注意:代码在 Kotlin 中,但在 Java 中应该类似。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-29
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多