如何在按钮点击时调用widget的onUpdate方法?

时间:2018-02-28 11:35:56

标签: java android android-appwidget appwidgetprovider onupdate

所以基本上,我有一个简单的应用程序小部件,它显示文件中的值并每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>

抱歉我的英语不好。

2 个答案:

答案 0 :(得分:0)

在您的refreshButtonClick()代码中,只需致电onUpdate()

,即可致电onUpdate();

答案 1 :(得分:0)

要在按钮点击期间触发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);