16

When we click the widget at that time I need to open an activity screen (or application). How to do this?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
RMK
  • 779
  • 3
  • 9
  • 13

5 Answers5

20

You need to set an onClickpendingIntent on your widget

Intent intent = new Intent(context, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);

Check this out

Processing more than one button click at Android Widget

Community
  • 1
  • 1
DeRagan
  • 22,827
  • 6
  • 41
  • 50
  • Side question, can you extract activities for a specific app ? (maybe I don't know what class is ExampleActivity.class ) – RelativeGames Mar 27 '13 at 19:12
  • 1
    who upvoted this nonesense? where's the answer? the guy asked "how to open an application from the widget button" not "how to set an action on a widget button" – Alireza Jamali May 19 '19 at 15:15
  • @AlirezaJamali To open the application from the widget button, just use your main activity as `ExampleActivity.class` in the first line. – Donald Duck Jul 25 '22 at 10:39
  • 1
    The second line should be `PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);` (with `PendingIntent.FLAG_IMMUTABLE` instead of the last `0`), otherwise you get a "Missing PendingIntent mutability flag" warning. – Donald Duck Jul 25 '22 at 10:40
10

Include this code in yout WidgetProvider class's onUpdate() method.

for(int j = 0; j < appWidgetIds.length; j++) 
{
    int appWidgetId = appWidgetIds[j];

    try {
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.LAUNCHER");

        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        intent.setComponent(new ComponentName("your Application package",
            "fully qualified name of main activity of the app"));
        PendingIntent pendingIntent = PendingIntent.getActivity(
            context, 0, intent, 0);
        RemoteViews views = new RemoteViews(context.getPackageName(),
            layout id);
        views.setOnClickPendingIntent(view Id on which onclick to be handled, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetId, views);
    } catch (ActivityNotFoundException e) {
            Toast.makeText(context.getApplicationContext(),
                    "There was a problem loading the application: ",
                    Toast.LENGTH_SHORT).show();
    }

}
trippedout
  • 1,561
  • 13
  • 20
Shivanand Darur
  • 3,158
  • 1
  • 26
  • 32
  • 8
    "your Application package","fully qualified name of main activity of the app" means `intent.setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName());` – Nikolay Hristov Oct 30 '15 at 20:56
2

very simple(In xamarin c# android mono):

public override void OnReceive(Context context, Intent intent)
        {
            if (ViewClick.Equals(intent.Action))
            {
                var pm = context.PackageManager;
                try
                {
                    var packageName = "com.companyname.YOURPACKAGENAME";
                    var launchIntent = pm.GetLaunchIntentForPackage(packageName);
                    context.StartActivity(launchIntent);
                }
                catch
                {
                    // Something went wrong :)
                }
            }
            base.OnReceive(context, intent);

        }
Prayag
  • 422
  • 3
  • 9
1

The Android developer pages for App Widgets has information and a full example doing exactly this: http://developer.android.com/guide/topics/appwidgets/index.html

DonSteep
  • 1,075
  • 10
  • 10
1

Using Kotlin

You need to add PendingIntent on Click of your widget Views

remoteViews.setOnClickPendingIntent(R.id.widgetRoot, 
               PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), 0))

Where widgetRoot is the id of my widget's parent ViewGroup

In On Update

Pending intent is usually added in onUpdate callback

    override fun onUpdate(
        context: Context,
        appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray) {

        // There may be multiple widgets active, so update all of them
        val widgetIds = appWidgetManager.getAppWidgetIds( ComponentName(context, ClockWidget::class.java))
        for (appWidgetId in widgetIds) {

                // Construct the RemoteViews object
                val remoteViews = RemoteViews(context.packageName, R.layout.clock_widget)

               //Open App on Widget Click
                remoteViews.setOnClickPendingIntent(R.id.weatherRoot,  
   PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), 0))

                //Update Widget 
                remoteViews.setTextViewText(R.id.appWidgetText, Date().toString())
                appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
            }
        }
    }
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154