简介
我正在尝试制作一个学校小工具作为作业。这是针对 Android SO并用Java编码完成的。我对此很陌生,因此我不得不阅读很多文档,尤其是从其主要Android开发人员网站上阅读文档,我对Android开发不感兴趣,因此非常欢迎快速而可靠的回答。
问题
我正在使用Intent向WidgetConfig从WidgetConfig类(活动)发送名称和ID。该数据应该是从小部件布局写入TextView的,但由于某种原因却没有。
在下面的这段代码中,如果您从提供的存储库中测试了该程序,则可能会看到我的尝试。
所以我的问题是:
如何从活动发送的数据中设法更新小部件?
public class FerrixWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
CharSequence widgetText = context.getString(R.string.appwidget_text);
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.ferrix_widget);
//views.setTextViewText(R.id.appwidget_text, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
RemoteViews spm = new RemoteViews(context.getPackageName(), R.layout.ferrix_widget);
spm.setTextViewText(R.id.alumnoName, "plsupdate");
}
//This is called to update the App Widget at intervals defined by the updatePeriodMillis attribute
// in the AppWidgetProviderInfo.
@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);
}
}
//This is called when an instance the App Widget is created for the first time.
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
System.out.println("[INFO] FIRST INSTANCE CREATED");
}
//This is called when the last instance of your App Widget is deleted from the App Widget host.
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
//This is called for every broadcast and before each of the above callback methods.
@Override
public void onReceive(Context context, Intent intent) {
RemoteViews controles = new RemoteViews(context.getPackageName(), R.layout.ferrix_widget);
String alumnoName = intent.getStringExtra("Name"); //Gets the intent withthe key "Name"
System.out.println("[INFO] Key: Name gets result: " + alumnoName);
controles.setTextViewText(R.id.alumnoName, alumnoName);
controles.setTextViewText(R.id.claseName, "DAM2");
System.out.println("[INFO] Context: " + context);
System.out.println("[INFO] Received: " + intent.getAction().toString());
}
}
完整代码位于
https://gitlab.com/JonaFerre/ferrixwidget
我的目标
我希望当您在屏幕上设置窗口小部件时,它会弹出一个配置屏幕(已经执行此操作),然后插入您的姓名和类别,此数据(通过Intent)发送到窗口小部件,以便它会在一天中的时间和其他一些东西旁边被写入并显示(一旦正确解决此问题,我会添加该东西)
注释:
答案 0 :(得分:1)
您可以通过更新窗口小部件的 onReceive 中的窗口小部件来轻松解决此问题:
只需通过配置活动发送广播:
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences prefs = getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("msg_" + widgetId, nameInput.getText().toString()); //???
editor.commit();
//Actualizar el widget tras la config
System.out.println("[INFO] Getting instance of WidgetConfig");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(WidgetConfig.this);
System.out.println("[INFO] Updating the widget");
FerrixWidget.updateAppWidget(WidgetConfig.this, appWidgetManager, widgetId);
//Devolver un buen OK
Intent resultado = new Intent();
resultado.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
//System.out.println("[CONFIG INFO] Name text is: " + nameInput.getText().toString());
resultado.putExtra("Name", nameInput.getText().toString());
resultado.setAction("myUpdate");
ComponentName componentName = new ComponentName(getApplicationContext(), FerrixWidget.class);
resultado.setComponent(componentName);
sendBroadcast(resultado);
setResult(RESULT_OK, getIntent());
finish();
System.out.println("[INFO] OK devuelto!");
}
});
以及小部件的 onReceive
public void onReceive(Context context, Intent intent) {
if(intent.getAction() != null && intent.getAction().equals("myUpdate")) {
RemoteViews controles = new RemoteViews(context.getPackageName(), R.layout.ferrix_widget);
String alumnoName = intent.getStringExtra("Name"); //Gets the intent withthe key "Name"
int id = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
System.out.println("[INFO] Key: Name gets result: " + alumnoName);
controles.setTextViewText(R.id.alumnoName, alumnoName);
controles.setTextViewText(R.id.claseName, "DAM2");
AppWidgetManager.getInstance(context).updateAppWidget(id, controles);
System.out.println("[INFO] Context: " + context);
System.out.println("[INFO] Received: " + intent.getAction().toString());
} else {
super.onReceive(context, intent);
}
}
但是更好的方法是将数据和小部件的ID保存在db / preference中,然后在onUpdate的小部件方面进行更新。
请阅读official doc。有很多东西可以帮助您。