将字符串从小部件配置屏幕传输到 AppWidget 本身?
Transfer a string from widget configuration screen to the AppWidget itself?
我正在创建一个天气 Widget.The 第一次将小部件拖到主屏幕时,应该会出现一个配置屏幕,其中包含代表三个城市的三个按钮。当用户单击一个按钮(比如哥本哈根)时,我希望将字符串 "Copenhagen"" 传输到 Widget 本身,这样它就可以获取哥本哈根的 fetech 天气数据并将其显示给用户。我该怎么做真的做到了吗?
AppWidget 配置屏幕
小部件屏幕
我将解释使用 SharedPreferences 的解决方案。
您确实需要两个 java 文件。 App Widget Configuration Activity(我称之为 Configure)和 AppWidgetProvider(我称之为 WeatherWidget)。
您将需要两个 xml 文件,定义配置 activity 和小部件的布局(我将我的命名为 activity_configure.xml 和 weather_widget.xml) .
您将需要一个清单文件和一个小部件信息文件(我将我的命名为 weather_widget_info
这里是Configure
中的相关代码
public class Configure extends AppCompatActivity {
private int widgetID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configure);
// if the user hits back button
setResult(RESULT_CANCELED);
// get widgetID for this widget instance
Bundle extras = getIntent().getExtras();
if (extras != null) {
widgetID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
Button copenhagenButton = (Button) findViewById(R.id.copenhagenButton);
copenhagenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveData("Copenhagen");
resultOK();
}
});
Button stockholmButton = (Button) findViewById(R.id.stockholmButton);
stockholmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveData("Stockholm");
resultOK();
}
});
Button osloButton = (Button) findViewById(R.id.osloButton);
osloButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveData("Oslo");
resultOK();
}
});
}
private void saveData(String cityName) {
String widgetIDString = Integer.toString(widgetID);
SharedPreferences prefs = this.getSharedPreferences(widgetIDString, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("city", cityName);
edit.commit();
}
private void resultOK() {
// send an onUpdate() message to the widget via a broadcast
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, WeatherWidget.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{widgetID});
sendBroadcast(intent);
// signal OK
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
setResult(RESULT_OK, resultValue);
finish();
}
}
这里是WeatherWidget的相关代码
public class WeatherWidget extends AppWidgetProvider {
final static String TAG = "stille";
private int widgetID;
private String city="N/A";
RemoteViews views;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
widgetID = appWidgetIds[i];
loadCity(context);
views = new RemoteViews(context.getPackageName(), R.layout.weather_widget);
views.setTextViewText(R.id.location, city);
appWidgetManager.updateAppWidget(widgetID, views);
}
}
private void loadCity(Context context) {
String widgetIDString = Integer.toString(widgetID);
SharedPreferences prefs = context.getSharedPreferences(widgetIDString, Context.MODE_PRIVATE);
String cityName = prefs.getString("city", "N/A ");
city = cityName;
}
}
我正在创建一个天气 Widget.The 第一次将小部件拖到主屏幕时,应该会出现一个配置屏幕,其中包含代表三个城市的三个按钮。当用户单击一个按钮(比如哥本哈根)时,我希望将字符串 "Copenhagen"" 传输到 Widget 本身,这样它就可以获取哥本哈根的 fetech 天气数据并将其显示给用户。我该怎么做真的做到了吗?
AppWidget 配置屏幕
小部件屏幕
我将解释使用 SharedPreferences 的解决方案。
您确实需要两个 java 文件。 App Widget Configuration Activity(我称之为 Configure)和 AppWidgetProvider(我称之为 WeatherWidget)。
您将需要两个 xml 文件,定义配置 activity 和小部件的布局(我将我的命名为 activity_configure.xml 和 weather_widget.xml) .
您将需要一个清单文件和一个小部件信息文件(我将我的命名为 weather_widget_info
这里是Configure
中的相关代码public class Configure extends AppCompatActivity {
private int widgetID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configure);
// if the user hits back button
setResult(RESULT_CANCELED);
// get widgetID for this widget instance
Bundle extras = getIntent().getExtras();
if (extras != null) {
widgetID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
Button copenhagenButton = (Button) findViewById(R.id.copenhagenButton);
copenhagenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveData("Copenhagen");
resultOK();
}
});
Button stockholmButton = (Button) findViewById(R.id.stockholmButton);
stockholmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveData("Stockholm");
resultOK();
}
});
Button osloButton = (Button) findViewById(R.id.osloButton);
osloButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveData("Oslo");
resultOK();
}
});
}
private void saveData(String cityName) {
String widgetIDString = Integer.toString(widgetID);
SharedPreferences prefs = this.getSharedPreferences(widgetIDString, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("city", cityName);
edit.commit();
}
private void resultOK() {
// send an onUpdate() message to the widget via a broadcast
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, WeatherWidget.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{widgetID});
sendBroadcast(intent);
// signal OK
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
setResult(RESULT_OK, resultValue);
finish();
}
}
这里是WeatherWidget的相关代码
public class WeatherWidget extends AppWidgetProvider {
final static String TAG = "stille";
private int widgetID;
private String city="N/A";
RemoteViews views;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
widgetID = appWidgetIds[i];
loadCity(context);
views = new RemoteViews(context.getPackageName(), R.layout.weather_widget);
views.setTextViewText(R.id.location, city);
appWidgetManager.updateAppWidget(widgetID, views);
}
}
private void loadCity(Context context) {
String widgetIDString = Integer.toString(widgetID);
SharedPreferences prefs = context.getSharedPreferences(widgetIDString, Context.MODE_PRIVATE);
String cityName = prefs.getString("city", "N/A ");
city = cityName;
}
}