So basically, I have a simple app widget that shows a value from a file and updates every 24h. It works properly but then I wanted to add the refresh button to my widget and I want to call onUpdate every time I click on that button. I've been searching for some answers but they not seem to match what I am trying to do.
AppWidgetProvider
public class NewAppWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
//Petlja za id-ove
String prosek = citaIzFajla(context);
final int N = appWidgetIds.length;
for(int i=0; i<N; i++){
int awID = appWidgetIds[i];
RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
v.setTextViewText(R.id.widget_text, prosek);
appWidgetManager.updateAppWidget(awID, v);
}
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
super.onDeleted(context, appWidgetIds);
Toast.makeText(context, "Deleted widget", Toast.LENGTH_LONG).show();
}
public void refreshButtonClick(View v){
//I need code that goes here to call onUpdate()
}
public String readFromFile(Context c) {
String out = "fail";
String fileName = "PROSEK";
try {
FileInputStream fileIS = c.openFileInput(fileName);
try {
int size = fileIS.available();
byte[] bufferReader = new byte[size];
fileIS.read(bufferReader);
fileIS.close();
out = new String(bufferReader);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return out;
}
}
xml file of a widget
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp"
android:background="#cbcfd6">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="2dp"
android:background="#000000">
<Button
android:id="@+id/widget_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Refresh"
android:onClick="refreshButtonClick"/>
<TextView
android:id="@+id/widget_prosek"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#ffffff"
android:text="Prosek:"
android:textSize="28dp"
android:textAlignment="center"/>
<TextView
android:id="@+id/widget_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#ffffff"
android:text="Example"
android:textSize="28dp"
android:gravity="right"
/>
</LinearLayout>
</LinearLayout>
Sorry for my bad English.
To trigger an onUpdate
on your Widget during button click, you can try this:
Intent intent = new Intent(this, NewAppWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] ids = AppWidgetManager.getInstance(getApplication()).getAppWidgetIds(new ComponentName(getApplication(), NewAppWidget.class));
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);