英文:
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
<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>
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启动一个服务
- 创建一个继承自
BroadcastReceiver的类,并将其添加到AndroidManifest文件中。 - 创建一个
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)
- 在你的
BroadcastReceiver中启动服务。 
class MyBroadcastReceiver : BroadcastReceiver() {
  override fun onReceive(context: Context?, intent: Intent?) {
      // 创建新的Intent来启动你的服务。
  }
}
- 由于后台限制,服务可能会被停止,你可能需要将其调至前台。
 
注意:这段代码是用Kotlin编写的,但在Java中也类似。
英文:
Starting a service from Android
- 
Create a class that extends
BroadcastReceiverand add it to the AndroidManifest file. - 
Create a.
PendingIntentto 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) - 
In your
BroadcastReceiver, start the service.class MyBroadcastReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { // Create new Intent to start your service. } } - 
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论