如何在单击按钮时调用小部件的 onUpdate 方法?
How to call onUpdate method of widget on button click?
基本上,我有一个简单的应用程序小部件,它显示文件中的值并每 24 小时更新一次。它工作正常,但后来我想将刷新按钮添加到我的小部件,并且我想在每次单击该按钮时调用 onUpdate。我一直在寻找一些答案,但它们似乎与我想要做的不相符。
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 小部件文件
<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>
抱歉我的英语不好。
在您的 refreshButtonClick()
代码中,只需编写 onUpdate();
来调用 onUpdate()
要在点击按钮时触发 Widget 上的 onUpdate
,您可以试试这个:
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);
基本上,我有一个简单的应用程序小部件,它显示文件中的值并每 24 小时更新一次。它工作正常,但后来我想将刷新按钮添加到我的小部件,并且我想在每次单击该按钮时调用 onUpdate。我一直在寻找一些答案,但它们似乎与我想要做的不相符。
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 小部件文件
<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>
抱歉我的英语不好。
在您的 refreshButtonClick()
代码中,只需编写 onUpdate();
onUpdate()
要在点击按钮时触发 Widget 上的 onUpdate
,您可以试试这个:
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);