Nauman Aslam
Nauman Aslam

Reputation: 299

Triggering pending intent in Android

I created a widget with a button and a pending intent for it to open an activity when the button is pressed. I need to know if there is some way that I can automatically trigger my pending intent after a certain amount of time repeatedly to check if there is an update in that activity. Is it alarmManager that I need to use?

public class MainActivity extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        final int N = appWidgetIds.length;

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

             Intent intent = new Intent(context, But1.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);


            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
            views.setOnClickPendingIntent(R.id.button1, pendingIntent);


         appWidgetManager.updateAppWidget(appWidgetId, views);

        }
    }
}

Upvotes: 1

Views: 794

Answers (2)

andyandy
andyandy

Reputation: 948

A pretty easy pattern is to use a broadcast receiver. Your service or a provider sends the broadcast like this:

getContext().sendBroadcast(new Intent(MyWidgetService.ACTION_UPDATE, null));

Implement onReceive in your widget provider class like this:

@Override
public void onReceive(Context context, Intent intent) {
    if (MyWidgetService.ACTION_UPDATE.equals(intent.getAction())) {
    // Do something.
        // For example you can just call your widget provider's onUpdate method here.
        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        assert appWidgetManager != null;

        final int[] allWidgetIds;
        if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
          allWidgetIds = new int[] {
              intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID),
          };
        } else {
          final ComponentName componentName = new ComponentName(context, this.getClass());
          allWidgetIds = appWidgetManager.getAppWidgetIds(componentName);
        }

        if (allWidgetIds == null || allWidgetIds.length <= 0) {
          return;
        }

        onUpdate(context, appWidgetManager, allWidgetIds);
    } else {
  super.onReceive(context, intent);
}
  }

Upvotes: 1

jwv
jwv

Reputation: 485

I've used countdown timer in the past to update things after an amount of time.

See: http://developer.android.com/reference/android/os/CountDownTimer.html

But I'm not really sure what you're trying to achieve here.

Could you not have the "other activity" alert what you need if any changes occur?

Perhaps using a callback routine.

http://www.javaworld.com/article/2077462/learn-java/java-tip-10--implement-callback-routines-in-java.html

Upvotes: 0

Related Questions