26

I newbie at programming Android and I try to do a widget which has be able get some data from ISP about my account. There are a lot of unknown things how to do it, but I have did a few things - I've got a widget with configure activity, where user should type login and password. Widget stores the data in SharedPerferences, and when it's time to update widget I use a Service to start an AsyncTask to getting it from ISP an account data. Now I want to do start an activity by click on widget. I've tried all advice which I found on this site and widget can't start activity. My widget based on another widget which placed here https://github.com/Arturus/MetrikaWidget. I dont understand what and where I should change to start activity by clicking on my widget. Thanks.

UPDATE: My update function, where I suggest I should place PendingIntent

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
    Log.d(TAG, "onUpdate");

    for (int appWidgetId : appWidgetIds)
    {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }

    /* An updateAppWidget functions looks like as:

    Intent intent = new Intent(context, UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
    context.startService(intent);
    */

    Intent intent = new Intent(context, DetailedStatActivity.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.layout);

    views.setOnClickPendingIntent(R.id.layout, pendingIntent);

    ComponentName componentName = new ComponentName(context.getPackageName(), WidgetProvider.class.getName());

    appWidgetManager.updateAppWidget(componentName, views);
}
1
  • 1
    How you are trying to start the activity now ? Paste code which you are doing now
    – Kamalone
    Jul 19, 2012 at 4:52

4 Answers 4

65

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

3
  • Thanks, it's works, I placed this 5 srings inside loop in onUpdate and click on widget works!
    – Nova
    Jul 19, 2012 at 5:32
  • is it really going to open two activities? i only see one activity in the intent so how two activity will open parallely
    – silverFoxA
    May 30, 2015 at 12:57
  • @silverFoxA It does not do that.Only opens one Activity. May 30, 2015 at 15:11
10
Intent inet = new Intent(your_action);
inet.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntentNetworkInfo = PendingIntent.getActivity(context, 2, 
            inet, Intent.FLAG_ACTIVITY_NEW_TASK);
remoteViews.setOnClickPendingIntent(you_component_when_the_user_pressing_this_activity_should_start, pIntentNetworkInfo);
2

I don't know about "Creating widget from another widget". This is out of my knowledge but I suggest you to build your own widget.

Apart from that, calling activity from widget should be using PendingIntent

Here is simple example to do it

Intent iSetting = new Intent(this, MyConfig.class);
PendingIntent piSetting = PendingIntent.getActivity(this, 0, iSetting, 0);
views.setOnClickPendingIntent(R.id.IdComponent, piSetting);

Or you might need to see this link and this link

2
  • My bad. I mean based on another widget instead created. I suggest I should place this code in my update function? But my update function already using to start a Service - it would be work? I already have tried change update function, but it's no works.
    – Nova
    Jul 19, 2012 at 5:02
  • Yes onUpdate method usually just start the Service. Inside the Service class itself, you could put those codes on onStart. Jul 19, 2012 at 5:11
0

In order to initiate the launcher activity, I have integrated the addCategory() and setAction() methods into the onUpdate() method.

Intent configIntent = new Intent(context, MainActivity.class);

configIntent.addCategory("android.intent.category.LAUNCHER");
configIntent.setAction("android.intent.action.MAIN");

PendingIntent configPendingIntent = PendingIntent.getActivity(context, Values.REQ_CODE, configIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

views.setOnClickPendingIntent(R.id.btn_widget, configPendingIntent);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.