gpt4 book ai didi

android - 如何通过 Intent 将数据从小部件配置类传递到小部件提供程序类?

转载 作者:行者123 更新时间:2023-11-29 15:55:58 25 4
gpt4 key购买 nike

我不打算删除这个问题,因为 commons 在下面提出了一些很好的观点,但我确实修改了代码并在这里提出了不同的问题:How do I retrieve shared preferences data in a Widget Service class without passing in incorrect default values or getting null pointer errors?

我正在开发一个应用程序,它接受用户的输入选择并将它们传递给一个小部件。它目前正在运行一项服务来管理它并且运行良好,但我无法弄清楚如何有效地将字符串从一个传递到另一个。到目前为止,这是我的代码:

        //First Widget config is called:
public class WidgetConfig extends Activity{

//Stuff happens here to get data from EditTexts and spinners and converts
//them to strings.
//Eventually a button is pressed which enters all the information:

public void onClick(View v) {
//I have already tried shared preferences like this:
//This was intended to give the shared preferences a unique identifier.
//It does not work for what I am trying to do
String str = Integer.toString(appWidgetId);
sp.putString(editor, str + "::" + "username", user_name);
//The appWidgetID was unique and so I thought it would work as an
//identifier for shared prefs.

//This is the intent for opening the provider
Intent intentUpdate = new Intent(context, MailWidgetProvider.class);

//I also attempted to put items here:
intentUpdate.putExtra("username", user_name);

//I left out the rest of the pending update code as it is irrelevant to this.
}
}


//Next the AppWidgetProvider is called
public class MailWidgetProvider extends AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {

ComponentName thisWidget = new ComponentName(context,
MailWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

//This is the intent to open up and run the service
Intent intent = new Intent(context.getApplicationContext(),
MailWidgetUpdateService.class);

intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

context.startService(intent);
}

}

//Service Class
public class MailWidgetUpdateService extends Service {
public void onStart(Intent intent, int startId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
.getApplicationContext());

int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

ComponentName thisWidget = new ComponentName(getApplicationContext(),
MailWidgetProvider.class);

int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);

//Loop through the IDs
for (int widgetId : allWidgetIds) {

int awid = widgetId;
String str = Integer.toString(widgetId);

String user_name = sp.getString(settings, str + "::" + "chosen_accout_string", "Loading...");
Log.d(TRACKING_USERNAME, user_name);

/*
Issue Here, see explanation below
*/
}
}

最佳答案

How do I retrieve the extras in the Widget Provider class from the widget config class and how do I go about passing them on to the service after receiving them?

您首先不要做任何事情。

你的 AppWidgetProvider只是更新应用小部件内容的一种方式,当您的应用小部件被添加并根据您的应用小部件元数据的请求定期更新时,Android 将专门使用这种方式。此外,请记住您的 AppWidgetProvider 的实例仅使用一次,然后丢弃。

如果你想在其他地方更新你的应用程序小部件,去更新应用程序小部件,通过创建 RemoteViews并将它们交给 AppWidgetManager .你的AppWidgetProvider与它无关。引用the documentation :

When an App Widget uses a configuration Activity, it is the responsibility of the Activity to update the App Widget when configuration is complete. You can do so by requesting an update directly from the AppWidgetManager.

如果你想有一个更新应用程序小部件逻辑的通用实现,那就是你的配置使用的一些通用类 Activity , 你的 AppWidgetProvider ,以及任何其他需要更新应用小部件内容的内容。

因此,当用户通过 Activity 配置应用小部件时,您需要:

  • 通过 AppWidgetManager 自行更新应用小部件, 和

  • 保留配置数据(在数据库中,SharedPreferences 或其他类型的文件中),以便它可以用于将来的更新

关于android - 如何通过 Intent 将数据从小部件配置类传递到小部件提供程序类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27880829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com