Launching Activity from Widget

Start activity by clicking on widget

Use this snippet in onUpdate() method of your widget AppWidgetProvider class:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
Intent configIntent = new Intent(context, Activity.class);

PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);

remoteViews.setOnClickPendingIntent(R.id.widget, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

Here widgetlayout is name of your widget layout and R.id.widget is it's parent layout id.

Edit:

Now,I see your code that you added to your question.You would to do:

PendingIntent.getActivity(context, 0, configIntent, 0);

(that start's activity) instead of

PendingIntent.getService(...);

that attempt to starts service.Good luck.

References:

doityourselfandroid.com

helloandroid.com

Launching activity from widget

I was having the same issue. I discovered that the fix is to call an update through the appwidget manager. here is an example of how to do that in onEnabled. It appears it needs to be done in both onEnabled and onUpdated so that when device is powering on your click intent is also intialized - in onUpdated the params already provide the reference to the manager, luckily.

@Override 
public void onEnabled(Context context) {
//Log.v("toggle_widget","Enabled is being called");

AppWidgetManager mgr = AppWidgetManager.getInstance(context);
//retrieve a ref to the manager so we can pass a view update

Intent i = new Intent();
i.setClassName("yourdoman.yourpackage", "yourdomain.yourpackage.yourclass");
PendingIntent myPI = PendingIntent.getService(context, 0, i, 0);
//intent to start service

// Get the layout for the App Widget
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.togglelayout);

//attach the click listener for the service start command intent
views.setOnClickPendingIntent(R.id.toggleButton, myPI);

//define the componenet for self
ComponentName comp = new ComponentName(context.getPackageName(), ToggleWidget.class.getName());

//tell the manager to update all instances of the toggle widget with the click listener
mgr.updateAppWidget(comp, views);
}

How do I start an Activity when my Widget is clicked?

Instead of:

Intent active = new Intent(paramContext, AppWidget.class);

You use:

Intent active = new Intent(paramContext, YOURCLASS.class); 

before you create the pending intent, @imran khan helped me but there are some tweaks you should do 2...this should fire up the Activity you need.

How to launch an activity after clicking a widget?

This is what worked for me...

The onUpdate method code should be:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
int appWidgetId = appWidgetIds[i];

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(context.getPackageName(),
"Activity.class"));
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
} catch (ActivityNotFoundException e) {
Toast.makeText(context.getApplicationContext(),
"There was a problem loading the application: ",
Toast.LENGTH_SHORT).show();
}

}
}

How to start Activity from Android AppWidget?

Problem is that I can't use startActivity() function in AppWidget.

Yes, you can. You are passed in a Context object into onUpdate() (or onReceive()) of your AppWidgetProvider -- call startActivity() on that.

Start Activity after put widget on screen

Some time ago I came up with a solution to my problem ... To start some activity after put widget on screen, it's enough to define it in the manifest with special intent-filter, for example:

<activity android:name=".MyWidgetConfigurationActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>

and add android:configure entry in AppWidgetProviderInfo metadata, for example:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure=".MyWidgetConfigurationActivity"
android:initialLayout="@layout/widget_layout"
android:minHeight="80dp"
android:minWidth="80dp"
android:updatePeriodMillis="30000" />

How to launch the configuration activity from widget?

If you are updating from Provider class, fetch the widget id from onUpdate() method, like this:

final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
}

or if your updating widget from ConfigurationActivity, fetch the widget id from onCreate method like this:

    Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}

Make your widget lauch ConfigurationActivity from a button/imageview in your widget_layout

    // Create intent pointing to ConfigurationActivity, in this example we are at ConfigurationActivity
Intent configurationIntent = new Intent(WidgetBarConfiguration.this, WidgetBarConfiguration.class);
// Create a extra giving the App Widget Id
configurationIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
// Create a pending intent giving configurationIntent as parameter
PendingIntent configurationPendingIntent = PendingIntent.getActivity(WidgetBarConfiguration.this, 0, configurationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Here we fecth the layout item and give it a action
RemoteViews views = new RemoteViews(WidgetBarConfiguration.this.getPackageName(), R.layout.widget_bar_layout);
// Setting onClick event that will lauch ConfigurationActivity
views.setOnClickPendingIntent(R.id.imageViewSettings, configurationPendingIntent);
// Updating widget
mWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();

AndroidManifest.xml

<activity
android:name=".WidgetBarConfiguration"
android:label="@string/title_activity_widget_configuration"
android:launchMode="singleInstance"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>

Look that lauchMode = "singleInstance" will make sure only the ConfigurationActivity layout appear on clicked, without any other activity layout attached.

How do I run an Activity from a button on a widget in Android?

start activity on Home Screen Widget click:

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 must register a broadcast for widget click

How to pass data and open an Activity from Widget? [Android]

I added setData() in my onReceive where I pass data.

 @Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);

if (intent.getAction().equals(TOAST_ACTION)) {
int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);

Intent goToDetails = new Intent(context, DetailsActivity.class);
goToDetails.putExtra(Constants.MERCHANT_ID, intent.getStringExtra(MERCHANT_ITEM) );

goToDetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
goToDetails.setData(Uri.parse(goToDetails.toUri(Intent.URI_INTENT_SCHEME)));
context.startActivity(goToDetails);

}

}

But I encounter a crash, if I un-install the app and then install and doesn't open the app first then add a widget and click on the ListView.

Found out why it crashes because of my database it still doesn't exist. Because I'm creating my database in my SplashActivity



Related Topics



Leave a reply



Submit