Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data from widget to activity

I'm trying to send data from my widget to my activity. I've seen several topics about the widget side, but I didn't succeed to get the data in my activity.

See Passing data from widget to app

In my Activity I try to do in the onStart() method:

Intent intent = getIntent();
if(intent.getStringExtra("he")!=null)
  LogWrapper.debug(MainActivity.class, "DATA "+intent.getStringExtra("he"));
else
  LogWrapper.debug(MainActivity.class, "DATA null"); 

But Intent is always null.

Widget provider side :

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
LogWrapper.info(DefibWidgetProvider.class,"in onUpdate!");
final int N = appWidgetIds.length;

// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
  int appWidgetId = appWidgetIds[i];

  // Create an Intent to launch MainActivity
  Intent intent = new Intent(context, MainActivity_.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
  intent.putExtra("he","Hello !");
  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.widget_defib);
  views.setOnClickPendingIntent(R.id.refreshButton, pendingIntent);

  // Tell the AppWidgetManager to perform an update on the current app widget
  appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

Context : My app contains a Google map. I have 2 buttons in my widget, And I want to open the mainactivity and center the map on a special long/lat when the user click one of these buttons.

Could you help me to make this working ? thank you

like image 659
psv Avatar asked Oct 14 '25 18:10

psv


1 Answers

As per your suggested link, you will get your intent's data inside onNewIntent method,

@Override
protected void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    super.onNewIntent(intent);
}

Get your intent data from inside this method.

like image 128
Chitrang Avatar answered Oct 17 '25 07:10

Chitrang