Reputation: 73
I am doing the widget development first time. So please bear with me. I have an Android service that starts the widget from onStart()
. So I am in my WidgetProvider
class onUpdate()
and update the views like buttons.
Now my question is -
1) How do I handle press of buttons in widget and tell the service about button press so that service does its work?
2) After the service has done its work, it needs to update the widget buttons and how do I handle this?
3) When the app is killed, the service is also killed. So when I click on widget, how do I determine that app is killed and open the app?
4) If the UI in app is talking to service and receiving responses from service, how do I tell these responses to widget too?
My widget code is like this -
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews remoteViews;
ComponentName componentName;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
componentName = new ComponentName(context, MyWidgetProvider.class);
remoteViews.setOnClickPendingIntent(R.id.button1,getPendingSelfIntent(context, "Play",componentName));
remoteViews.setOnClickPendingIntent(R.id.button2,getPendingSelfIntent(context, "Pause",componentName));
appWidgetManager.updateAppWidget(componentName, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
if ("Play".equals(intent.getAction())) {
//I really dont know what to do here....
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
watchWidget = new ComponentName(context, MyWidgetProvider.class);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action, ComponentName componentName) {
Intent intent = new Intent(context, MyService.class);
intent.setAction(action);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, componentName);
return PendingIntent.getService(context, 0, intent, 0);
}
}
Request you to please help me in this
Upvotes: 0
Views: 180
Reputation: 881
1) You're already doing that with your getPendingSelfIntent()
method. Although your target should be MyWidgetProvider
like this:
Intent intent = new Intent(context, MyWidgetProvider.class);
Then in your onReceive
you will check if the intent action is "Play" and start your service from there:
Intent serviceIntent = new Intent(context, MyService.class);
2) In order to your service communicate to your Widget you need to send a broadcast:
Intent widgetUpdateIntent = new Intent(this, MyWidgetProvider.class);
widgetUpdateIntent.setAction(ApplicationEvents.DATA_FETCHED);//this is your custom action
widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
sendBroadcast(widgetUpdateIntent);
3) You don't need to explicit open the app (you can't do that, actually), if you have your update cycle set to 30 mins you will receive the onUpdate
callback and you need to start your service from there too.
4) You will update the UI in the onReceive
event when the action matches your custom action (ApplicationEvents.DATA_FETCHED
).
Upvotes: 1