-1

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
    Please post code what you are trying to achieve so that community can help you – Android_id Apr 08 '20 at 12:53
  • I've add the code itself – Matan Issler Apr 08 '20 at 13:59
  • 1
    Does this answer your question? [Clickable widgets in android](https://stackoverflow.com/questions/2748590/clickable-widgets-in-android) – Froyo Apr 08 '20 at 14:18
  • Not excactly, I don't know where to place the code that "user2742371" wrote, and I tried "Mike" suggestion, and while I manged to start an activity after I clicked the button, I didn't manged to start a service (With no Winodw that suddenly pop up) – Matan Issler Apr 08 '20 at 14:48

1 Answers1

1

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.

Froyo
  • 17,947
  • 8
  • 45
  • 73