【发布时间】:2020-10-27 06:21:23
【问题描述】:
我是 Android Studio 的新手。在我的 MainActivity.java 中,我调用了 ReadPage 类,该类从 AsyncTask 扩展(可以读取 JSON 格式的网页),其中我覆盖了 doInBackground、onProgressUpdate 和 onPostExecute;到这里为止,该应用程序运行良好。
但是,我想实现一个小部件 AppWidget.java 以便可以读取 JSON 格式的同一页面并在小部件字段中打印给定信息。由于ReadPage类被定义到MainActivity.java中,我写了一个ReadPageWidget函数,功能差不多,但是我无法实现让它工作:
在 AppWidget.java 中,我知道在我的 onUpdate 函数中,我必须调用 new ReadPageWidget(views).execute("Updating"),然后调用 appWidgetManager.updateAppWidget(appWidgetId, views);,其中之前定义了视图,例如 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget);
在我的小部件中,我希望有一个按下按钮,它会更新来自网页的信息,所以我定义了一个function public void UpdateDate(View view)。
我不知道如何实现这个小部件,并且我所做的所有问题都完成了强制关闭应用程序。
编辑:
根据创建小部件的基本示例,我定义了这样的onUpdate函数:
public class AppWidget extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget);
SimpleDateFormat Date = new SimpleDateFormat("HH:mm:ss aa");
String DateFormat = Date.format(Calendar.getInstance().getTime());
views.setTextViewText(R.id.TVUpdatingDate, DateFormat);
new ReadPageWidget(views).execute("Updating");
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
...
}
在此,我创建了 ReadPageWidget 的实例并使用参数views 执行 AsynTask。仅当我运行应用程序时,字段 DateFormat 才会更新,尽管我已将 android:updatePeriodMillis="3000" 定义到我的 app_widget_info.xml 中
我的app_widget.xml 是这样定义的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Field"
android:id="@+id/FieldJSON"
android:textSize="12sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="UpdatingDate"
android:id="@+id/BUpdatingDate"
android:onClick="UpdateDate"
android:textSize="12sp"
/>
</RelativeLayout>
其中FieldJSON 应由具有ReadPageWidget 函数的小部件更新。
ReadPage 和 ReadPageWidget 的唯一区别在于属性的定义和构造方法:
public class ReadPage extends AsyncTask<String, Integer, String> {
MainActivity Context;
ProgressBar ProgressBarReadPage;
TextView TVPage;
public ReadPage(MainActivity Context) {
this.Context = Context;
}
...
}
对
public class ReadPageWidget extends AsyncTask<String, Integer, String> {
private RemoteViews views;
public ReadPageWidget(RemoteViews views) {
this.views = views;
}
}
已编辑:
我在 AppWidget.java 的button BUpdatingDate 和 UpdateDate 函数中添加了onclick 函数:
public void UpdateDate(View view) {
Toast.makeText(view.getContext(), "Clicked!!", Toast.LENGTH_SHORT).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(view.getContext());
ComponentName thisAppWidgetComponentName = new ComponentName(view.getContext().getPackageName(), getClass().getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidgetComponentName);
this.onUpdate(view.getContext(), appWidgetManager, appWidgetIds);
}
【问题讨论】:
-
所以你想创建一个将由 json 更新的小部件?你实现了它并与我们分享吗?
-
我已经添加了一些实现的规范基础,谢谢
-
最后一件事,onUpdate 在您的活动或片段中位于何处?
-
它在我的 AppWidget 类中(已编辑)
标签: java android android-asynctask widget android-widget