I’m new to Android development. I’ve managed to code a widget which updates every 30 minutes using Alarm Manager. This part is working correctly.
I now want to also enable manually updating of the widget whenever I click on it (i.e. trigger the same OnReceive() via a broadcast whether via the repeating alarm or the click).
I hoped it would be as simple as adding the two lines at the end (as shown below), but the onClick is not working when I click the widget. What is the best way to achieve what I am trying to do?
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//Update after 30 minutes
am.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis()+ 300, 18000000 , pi);
//Update manually by Clicking widget (this part not working)
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.my_widget_layout);
remoteViews.setOnClickPendingIntent(R.layout.my_widget_layout, pi);
Are you mixing of
R.layout.my_widget_layoutandR.id.my_widget_layoutin your code.Should notice that Android uses referenced IDs of
R.id.xyzfor all the generated widget items whileR.layout.abcfor your views (layout)I anticipated that error was posed on line: