How to trigger an action with a button on a widget in Android?

I am working on a toggle widget consisting of a button. When clicked, I would like it to launch the activity without opening anything (just saying on the desktop as usual). Is there a way to directly launch an activity by clicking a button on the desktop? Thank you! UPDATE: I am now trying to switch quiet mode from code without starting a new activity. Here is my current code (the buttons do nothing when I click on them for some reason)

package toggleHD.widget;

import android.app.Activity;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.media.AudioManager;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;


    public class ButtonWidget extends AppWidgetProvider {

        public static String ACTION_WIDGET_CLICK_RECEIVER = "ActionReceiverWidget";

        public static int appid[];
        public static RemoteViews rview;
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
                int[] appWidgetIds){
            updateWidgetState(context, ""); 
        }
        @Override
        public void onReceive(Context paramContext, Intent paramIntent)
          {
             String str = paramIntent.getAction();
            if (paramIntent.getAction().equals(ACTION_WIDGET_CLICK_RECEIVER)) {
                updateWidgetState(paramContext, str);   
            }
            else
            {
                    if ("android.appwidget.action.APPWIDGET_DELETED".equals(str))
                      {
                        int i = paramIntent.getExtras().getInt("appWidgetId", 0);
                        if (i == 0)
                        {

                        }
                        else
                        {
                            int[] arrayOfInt = new int[1];
                            arrayOfInt[0] = i;
                            onDeleted(paramContext, arrayOfInt);
                        }
                      }
              super.onReceive(paramContext, paramIntent);
            }
          }
         static void updateWidgetState(Context paramContext, String paramString)
          {
            RemoteViews localRemoteViews = buildUpdate(paramContext, paramString);
            ComponentName localComponentName = new ComponentName(paramContext, ButtonWidget.class);
            AppWidgetManager.getInstance(paramContext).updateAppWidget(localComponentName, localRemoteViews);
          }
         private static RemoteViews buildUpdate(Context paramContext, String paramString)
          {
            // Toast.makeText(paramContext, "buildUpdate() ::"+paramString, Toast.LENGTH_SHORT).show();
            rview = new RemoteViews(paramContext.getPackageName(), R.layout.main);
            Intent active = new Intent(paramContext, ButtonWidget.class);
            active.setAction(ACTION_WIDGET_CLICK_RECEIVER);

           // PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, active, 0);

// upadte this R.id.buttonus1 with your layout or image id on which click you want to start Activity

Intent configIntent = new Intent(paramContext, TestReceiver.class);
configIntent.setAction(ACTION_WIDGET_CLICK_RECEIVER);
PendingIntent configPendingIntent = PendingIntent.getActivity(paramContext, 0, configIntent, 0);
            if(paramString.equals(ACTION_WIDGET_CLICK_RECEIVER))
            {

               //open Activity here..
             //your code for update and what you want on button click
//
         rview.setOnClickPendingIntent(R.id.button_one, configPendingIntent);
            }  
             return rview; 
          }
        @Override
        public void onEnabled(Context context){
            super.onEnabled(context);
           // Toast.makeText(context, "onEnabled()  ", Toast.LENGTH_SHORT).show();
        }
        // Called each time an instance of the App Widget is removed from the host
        @Override
        public void onDeleted(Context context, int [] appWidgetId){
            super.onDeleted(context, appWidgetId);
           // Toast.makeText(context, "onDeleted()  ", Toast.LENGTH_SHORT).show();
        }
        // Called when last instance of App Widget is deleted from the App Widget host.
        @Override
        public void onDisabled(Context context) {
            super.onDisabled(context);
           // Toast.makeText(context, "onDisabled()  ", Toast.LENGTH_SHORT).show();
        }

    }

      

+3


source to share


2 answers


get started with home screen widgets:

Method 1: in on onReceive

Intent intentClick =   the new Intent to (context, update, class);
PendingIntent pendingIntent = PendingIntent.getActivity (context, 0,
intentClick, 0);
rv.setOnClickPendingIntent (R.id.layout, pendingIntent);

      



Method 2: in onReceive

Intent intn = new Intent (context, update. Class);
intn.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity (intn);

      

but you have to register the broadcast for the widget click

+7


source


If you want to do sth. in background you can use Service instead of Activity: http://developer.android.com/reference/android/app/Service.html

1. You can starService immediately after starting the application or displaying your widget. http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent )

2.Try bindService when click is clicked http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent , android.content.ServiceConnection, int)

3. After binding, you can get the service instance from the binder using serviceConnection. Then you can execute the code defined in the service. see example here: http://developer.android.com/reference/android/app/Service.html



or you can just

  • try to start the service (only the service instance will be started at any time)

  • send a broadcast to a service that asks the service to do sth. for you.

Because the action is meant to display sth to the user, and the service is meant to run in the background.

+1


source







All Articles