Java 未调用Onupdate,多次调用onreceive
我已经搜索了这么多,但我没有遇到我遇到过的同样的问题 我正在开发一个具有配置活动的小部件,用户可以在其中设置颜色、文本大小和选择类别。基本思想是,当用户关闭配置活动时,带有所选类别的数组被发送到Onreceive方法,在该方法中,我调用另一个方法,在该方法中,我从XML文件中读取内容,过滤类别并填充另一个数组,以便在Onupdate方法中读取内容 调用Onreceived并填充和过滤我的内容,但我的问题是:Java 未调用Onupdate,多次调用onreceive,java,android,widget,Java,Android,Widget,我已经搜索了这么多,但我没有遇到我遇到过的同样的问题 我正在开发一个具有配置活动的小部件,用户可以在其中设置颜色、文本大小和选择类别。基本思想是,当用户关闭配置活动时,带有所选类别的数组被发送到Onreceive方法,在该方法中,我调用另一个方法,在该方法中,我从XML文件中读取内容,过滤类别并填充另一个数组,以便在Onupdate方法中读取内容 调用Onreceived并填充和过滤我的内容,但我的问题是: 当我关闭配置时,onreceive会被调用两次 根本不调用Onupdate方法 我尝试了
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".WidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
<activity android:name=".Nastavitve" android:label="@string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
</application>
这里的代码太多了。将范围仅限于绝对相关的部分,否则研究代码会花费太长时间,没有人会提供帮助好的,我做了,我认为主要问题在于我发布的配置代码的最后一部分,在那里引发了更新。我正在进一步检查,似乎AppWidgetId为空/空,它可能应该有一些值?
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:updatePeriodMillis="1800000"
android:minWidth="250dp"
android:minHeight="50dp"
android:initialLayout="@layout/widget_layout" android:configure="com.example.mislicitati.Nastavitve">
</appwidget-provider>
public class WidgetProvider extends AppWidgetProvider {
ArrayList<String> recepti = new ArrayList<String>();
NodeList listCitat;
static final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
//random object from array
Random rand = new Random();
String besedilo = null, avtor = null;
HashMap<String, String> map = new HashMap<String, String>();
map = list.get(rand.nextInt(list.size()));
for (Entry<String, String> mapEntry : map.entrySet()) {
String key = mapEntry.getKey();
String value = mapEntry.getValue();
if (key == "avtor") {
avtor = value;
}
if (key == "besedilo") {
besedilo = value;
}
}
for (int widgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setTextViewText(R.id.textView1, besedilo);
views.setTextViewText(R.id.textView2, avtor);
appWidgetManager.updateAppWidget(widgetId, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("onReceive", "poklican");
ArrayList<String> izbranekat;
izbranekat = intent.getStringArrayListExtra("kategorije");
if(list.isEmpty())
{
napolniSeznam(context, izbranekat);
}
else
{
list.clear();
napolniSeznam(context, izbranekat);
}
super.onReceive(context, intent);
}
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("mainOk", "called");
// Configuration...
// Call onUpdate for the first time.
Log.d("Ok Button", "First onUpdate broadcast sending...");
final Context context = Nastavitve.this;
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
ComponentName thisAppWidget = new ComponentName(
context.getPackageName(), Nastavitve.class.getName());
// N.B.: we want to launch this intent to our AppWidgetProvider!
Intent firstUpdate = new Intent(context, WidgetProvider.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
firstUpdate.putStringArrayListExtra("kategorije", izbkat);
firstUpdate.setAction("android.appwidget.action.APPWIDGET_UPDATE");
firstUpdate
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
context.sendBroadcast(firstUpdate);
Log.d("Ok Button", "First onUpdate broadcast sent");
// Return the original widget ID, found in onCreate().
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK, resultValue);
finish();