我开发了一个在主屏幕小部件上显示新闻提要的应用程序。由于以下情况,在棒棒糖之前的 android 设备上一切正常:
- 用户进入其启动器的小部件屏幕以选择/添加特定小部件
- 用户单击 MyNewsWidget 以添加到他们的主屏幕
- 调用配置活动,以便用户可以从不同的新闻源中进行选择
- 用户单击配置活动中的“保存”按钮,然后活动应该完成并且 MyNewsWidget 应该被添加到他们的主屏幕。
我正在 LG Nexus 5 设备上测试我的应用程序,但是当我在配置活动完成后单击以将我的小部件添加到主屏幕时,该小部件未添加到主屏幕。
这是我在 Configuration 活动中的 Intent 代码:
int[] appWidgetIds = new int[]{mWidgetId};
Intent intent = new Intent(WidgetResourcesActivity.this, NewsWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
sendBroadcast(intent);
setResult(RESULT_OK, intent);
finish();
此外,这是我的 WidgetProvider 类中的代码:
@Override
public void onReceive(Context context, Intent intent) {
//I got this result on pre-Lollipop android devices "Action is: ACTION_APPWIDGET_UPDATE"
if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
int[] appWidgetIds = intent.getExtras().getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
for (int widgetId : appWidgetIds) {
if (!isWidgetAdded(context, widgetId)) {
Intent widgetIntent = new Intent(Intent.ACTION_MAIN);
widgetIntent.setClass(context, WidgetResourcesActivity.class);
widgetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
widgetIntent.putExtra(WidgetResourcesActivity.EXTRA_WIDGET_ID, widgetId);
context.startActivity(widgetIntent);
PendingIntent pendingIntent = PendingIntent.getActivity(context, widgetId, widgetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
} else {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] widgetIds = new int[]{widgetId};
onUpdate(context, appWidgetManager, widgetIds);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
updateButtonsStatus(remoteViews, 0, mFeedsCount);
AppWidgetManager.getInstance(context).updateAppWidget(appWidgetIds, remoteViews);
}
}
return;
}
//Here is my trouble . . .
//Unfortuantely, I got this result on Lollipop android devices "Action is: ACTION_APPWIDGET_DELETED" !! Why?
if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_DELETED)) {
if (BuildConfig.DEBUG) Log.e(TAG, "action : " + intent.getAction());
removeWidgetId(context, intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID));
return;
}
super.onReceive(context, intent);
if (intent.getExtras() == null)
return;
int widgetId = intent.getExtras().getInt(EXTRA_WIDGET_ID);
if (BuildConfig.DEBUG)
Log.e(TAG, "action : " + intent.getAction() + " id : " + intent.getExtras().getInt(EXTRA_BUTTON_ID));
if (intent.getExtras().getInt(EXTRA_BUTTON_ID) == R.id.widget_arrow_back) {
mIndex = intent.getExtras().getInt(EXTRA_FEED_INDEX);
if (mIndex > 0) {
mIndex--;
if (BuildConfig.DEBUG) Log.e(TAG, "currentIndex : " + mIndex);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
updateButtonsStatus(remoteViews, mIndex, mFeedsCount);
updateWidget(context, remoteViews, mIndex, widgetId);
handleWidgetBackButton(context, remoteViews, widgetId);
handleWidgetNextButton(context, remoteViews, widgetId);
}
}
if (intent.getExtras().getInt(EXTRA_BUTTON_ID) == R.id.widget_arrow_next) {
mIndex = intent.getExtras().getInt(EXTRA_FEED_INDEX);
if (mIndex < mFeedsCount - 1) {
mIndex++;
if (BuildConfig.DEBUG) Log.e(TAG, "currentIndex : " + mIndex);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.app_widget);
updateButtonsStatus(remoteViews, mIndex, mFeedsCount);
updateWidget(context, remoteViews, mIndex, widgetId);
handleWidgetNextButton(context, remoteViews, widgetId);
handleWidgetBackButton(context, remoteViews, widgetId);
}
}
}
遗憾的是,之前的场景不适用于 android Lollipop(5.0 及更高版本)。任何好的帮助将不胜感激。