此刻我正在尝试消化Android App Widget。我了解App小部件是驻留在主屏幕上的微型应用程序,可以在受支持的视图中显示一些数据。我想了解的是构建应用程序小部件时采用的典型设计模式。
App小部件从中获取数据的典型来源是什么?以下是到目前为止我的理解和问题:
数据库:MainActivity从网络获取数据,存储在本地数据库中,然后将广播意图发送到小部件,以便小部件通过从数据库中获取数据来更新自身
网络:小部件能否使用翻新直接进行网络通话?
共享首选项:小部件从共享首选项中获取一小段信息(例如字符串数组)是个好主意吗?
MainActivity:MainActivity(或其他任何活动类)可以直接在小部件的Listview适配器中设置数据吗?
意图:我可以在广播意图中使用putExtra来设置字符串数组吗?
在我的用例中,我要在mainActivity中获取新数据,并将其推送到小部件。最好的方法是什么?
答案 0 :(得分:1)
我不得不从MainActivty
正在更新的数据库中获取数据。因此,为了在小部件类中获取数据,我只调用了查询以从数据库中获取数据。我也使用共享首选项来获取一些数据。那不是问题。您可以使用共享首选项获取数据。
public class BottleWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.bottle_widget);
// isUnitAdded getting value from shared pref
boolean isUnitAdded= TinyDB.getInstance(context).getBoolean("units_added");
if (isUnitAdded){
Intent intent2 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, intent2, 0);
views.setOnClickPendingIntent(R.id.widget_root_layout_bottle, pendingIntent2);
}
else {
Intent intent2 = new Intent(context, UnitsActivity.class);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, intent2, 0);
views.setOnClickPendingIntent(R.id.widget_root_layout_bottle, pendingIntent2);
}
Constants constants = new Constants(context);
//getting value from shared pref
Double mTargetQuantity = Double.parseDouble(constants.getPref(Constants.DRINK_VALUE_SHARED_PREF, context));
if (mTargetQuantity ==0)
mTargetQuantity= Double.valueOf(100);
// initializing database
MyDataBase myDataBase = MyDataBase.getInstance(context);
Calendar calendarToday = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
long millis = calendarToday.getTimeInMillis();
String date = dateFormat.format(millis);
// calling query to get data from database
int mProgress = myDataBase.dailyConsumptionDao().getQuantity(date);
// getting data from sharedpref
String drinkUnit=constants.getPref(Constants.DRINK_UNIT_SHARED_PREF,context);
int target= (int)Math.round(mTargetQuantity);
int percentage= (mProgress*100)/target;
int Icon= TinyDB.getInstance(context).getInt("icon");
views.setImageViewResource(R.id.widget_iv_add,R.drawable.ic_add);
views.setTextViewText(R.id.tv_total_percentage,String.valueOf(percentage)+ " %");
views.setTextColor(R.id.tv_total_percentage, Color.WHITE);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
ComponentName thisWidget = new ComponentName(context.getApplicationContext(), BottleWidget.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
if (appWidgetIds != null && appWidgetIds.length > 0) {
onUpdate(context, appWidgetManager, appWidgetIds);
}
}