15

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

Vadim Kotov
  • 7,766
  • 8
  • 46
  • 61
RMK
  • 769
  • 3
  • 9
  • 13

5 Answers5

19

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,442
  • 6
  • 39
  • 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
  • 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
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,541
  • 13
  • 19
Shivanand Darur
  • 2,998
  • 1
  • 25
  • 30
  • 7
    "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
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,065
  • 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
  • 38,157
  • 14
  • 182
  • 142
1

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
  • 232
  • 2
  • 8