在安卓小部件上的图片按钮点击后显示一个简单的弹窗提示。

huangapple go评论71阅读模式
英文:

show a simple toast after click on imagebutton on android widget

问题

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

这是 Java 类的翻译:

public class wow_shortcut extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {

        CharSequence widgetText = wow_shortcutConfigureActivity.loadTitlePref(context, appWidgetId);
        // 构建 RemoteViews 对象
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.wow_shortcut);

        // 指示小部件管理器更新小部件
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // 可能有多个小部件处于活动状态,因此更新所有小部件
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // 当用户删除小部件时,删除与之关联的首选项
        for (int appWidgetId : appWidgetIds) {
            wow_shortcutConfigureActivity.deleteTitlePref(context, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) {
        // 创建第一个小部件时的相关功能
    }

    @Override
    public void onDisabled(Context context) {
        // 禁用最后一个小部件时的相关功能
    }
}

希望对你有所帮助!祝你有美好的一天!

英文:

I need help in adding action to a homescreen widget I want to add to my app.
I've searched for a solution on many thread, but none of them worked for me.

All I want to do is toast a simple message after a click on an image button on my widget.
If this works, I want to run a service intent after a click on this button.

I would be wonderful if you could add the code that you wrote in java.

This is the code of the xml widget

&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:background=&quot;#09C&quot;
    android:padding=&quot;@dimen/widget_margin&quot;&gt;

    &lt;ImageButton
        android:id=&quot;@+id/widget_btn&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        app:srcCompat=&quot;@drawable/on&quot; /&gt;
&lt;/RelativeLayout&gt;

This is the class itself:

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


Thank you and have a good day!

答案1

得分: 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)
  1. 在你的BroadcastReceiver中启动服务。
class MyBroadcastReceiver : BroadcastReceiver() {
  override fun onReceive(context: Context?, intent: Intent?) {
      // 创建新的Intent来启动你的服务。
  }
}
  1. 由于后台限制,服务可能会被停止,你可能需要将其调至前台。

注意:这段代码是用Kotlin编写的,但在Java中也类似。

英文:

Starting a service from Android

  1. Create a class that extends BroadcastReceiver and add it to the AndroidManifest file.

  2. Create a. PendingIntent to start a Broadcast. And attach it to the widget view.

     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. In your BroadcastReceiver, start the service.

     class MyBroadcastReceiver : BroadcastReceiver() {
       override fun onReceive(context: Context?, intent: Intent?) {
           // Create new Intent to start your service.
       }
     }
    
  4. The Service may be stopped due to background restrictions and you may need to bring it in the foreground.

Note: the code is in Kotlin, but it should be similar in Java.

huangapple
  • 本文由 发表于 2020年4月8日 20:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61101111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定