App Widgets 详解二 Configuration Activity
程序员文章站
2022-05-30 13:41:41
...
导读:
本篇主要介绍 Configuration Activity 的配置与使用
- App Widget 小部件系列其他文章链接:
App Widgets 详解二 Configuration Activity
App Widgets 详解三 Activity中添加App Widgets
App Widgets 详解四 RemoteViews、RemoteViewsService和RemoteViewsFactory
定义App Widget Configuration Activity
如果用户希望添加AppWidget前设置 Widget 配置,可以创建App Widget Configuration Activity.
该Activity 将由App Widget Host 自动启动,并允许用户在创建时配置App Widget的可用设置,例如App Widget 颜色,大小,更新周期或其他功能设置.
定义步骤:
一、在清单文件声明activity,添加 “android.appwidget.action.APPWIDGET_CONFIGURE” action
<activity android:name=".ExampleAppWidgetConfigureActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
二、在AppWidgetProviderInfo XML文件声明 android:configure 属性
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
...
android:configure="zs.xmx.ExampleAppWidgetConfigureActivity"
... >
</appwidget-provider>
三、Configuration Activity 实现:
从Launched Activity(主屏) 找到 Widget ID (保存在Launched Activity Intent extras 的EXTRA_APPWIDGET_ID)
App Widget 配置逻辑,这边按需配置即可
配置完成后,获取AppWidgetManager实例
通过使用AppWidgetManager调用updateAppWidget(int,RemoteViews)通过RemoteViews布局更新App Widget
结束Activity,将设置的参数用 inent.putXX 传递出去(要做回显的可以保存一份到SP)
public class ExampleAppWidgetConfigureActivity extends Activity implements View.OnClickListener {
private static final String PREFS_NAME = "zs.xmx.ExampleAppWidget";
private static final String PREF_PREFIX_KEY = "appwidget_";
/**
* Widget ID
**/
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
EditText mAppWidgetText;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// 当您的配置活动首次打开时,将活动结果与EXTRA_APPWIDGET_ID一起设置为RESULT_CANCELED,
// 当用户在到达结束之前退出活动,会通知App Widget Host 配置已取消,
// 并且将不会添加App Widget.(Configuration Activity退出,不再创建App Widget)
setResult(RESULT_CANCELED);
setContentView(R.layout.example_app_widget_configure);
mAppWidgetText = (EditText) findViewById(R.id.appwidget_text);
findViewById(R.id.add_button).setOnClickListener(this);
//1.从Launched Activity(主屏) 找到 Widget ID (保存在Launched Activity Intent extras 的EXTRA_APPWIDGET_ID)
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mAppWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
//判断app widget ID是否为空.(App Widget 是否添加到Launched Activity)
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
return;
}
mAppWidgetText.setText(loadTitlePref(ExampleAppWidgetConfigureActivity.this, mAppWidgetId));
}
@Override
public void onClick(View v) {
final Context context = ExampleAppWidgetConfigureActivity.this;
//2.App Widget 配置逻辑,这边按需配置即可
String widgetText = mAppWidgetText.getText().toString();
//保存配置到SP中
saveTitlePref(context, mAppWidgetId, widgetText);
//3.配置完 Widget 配置,拿到AppWidgetManager实例
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
//4.通过调用updateAppWidget(int,RemoteViews)通过RemoteViews布局更新App Widget
/*ExampleAppWidget.updateAppWidget(..)这里写过了 :
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.example_appwidget);
appWidgetManager.updateAppWidget(mAppWidgetId, views);*/
ExampleAppWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId);
//5.结束 Activity,将设置的参数用 inent.putXX 传递出去(要做回显的可以保存一份到SP)
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
public ExampleAppWidgetConfigureActivity() {
super();
}
/**
* 下面都是SP数据处理
**/
// Write the prefix to the SharedPreferences object for this widget
static void saveTitlePref(Context context, int appWidgetId, String text) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
prefs.apply();
}
// Read the prefix from the SharedPreferences object for this widget.
// If there is no preference saved, get the default from a resource
static String loadTitlePref(Context context, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
if (titleValue != null) {
return titleValue;
} else {
return context.getString(R.string.appwidget_text);
}
}
static void deleteTitlePref(Context context, int appWidgetId) {
SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
prefs.remove(PREF_PREFIX_KEY + appWidgetId);
prefs.apply();
}
}
效果图:
==注意==
- 使用 Configuration Activity 应该始终返回结果.结果应包括保存在Launched Activity Intent extras 的EXTRA_APPWIDGET_ID
- 使用了 Configuration Activity ,当创建App Widgets时,不会调用onUpdate()方法,因此需要在Configuration Activity使用AppWidgetManager请求更新.(只调用一次,后续的交回AppWidgetProvider类的onUpdate(()更新)
- 当您的配置活动首次打开时,将活动结果与EXTRA_APPWIDGET_ID一起设置为RESULT_CANCELED,当用户在到达结束之前退出活动,会通知App Widget Host 配置已取消,并且将不会添加App Widget.(Configuration Activity退出,不再创建App Widget)
总结:
本篇文章到此结束,欢迎关注,后续有补充的会即使更新,有问题也欢迎评论,共同成长