When we click the widget at that time I need to open an activity screen (or application). How to do this?
How to open an application from widget in Android?
Asked Answered
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
Side question, can you extract activities for a specific app ? (maybe I don't know what class is ExampleActivity.class ) –
Grazia
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" –
Ineffectual
@AlirezaJamali To open the application from the widget button, just use your main activity as
ExampleActivity.class
in the first line. –
Flita 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. –
Flita 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();
}
}
"your Application package","fully qualified name of main activity of the app" means
intent.setComponent(new ComponentName(context.getPackageName(), MainActivity.class.getName());
–
Cake 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);
}
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
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);
}
}
}
© 2022 - 2024 — McMap. All rights reserved.