下面列出了android.appwidget.AppWidgetManager#ACTION_APPWIDGET_BIND 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Called when place-automatically button is clicked.
*/
public void onPlaceAutomaticallyClick(View v) {
if (mRequest.getRequestType() == PinItemRequestCompat.REQUEST_TYPE_SHORTCUT) {
InstallShortcutReceiver.queueShortcut(
new ShortcutInfoCompat(mRequest.getShortcutInfo()), this);
mRequest.accept();
finish();
return;
}
mPendingBindWidgetId = mAppWidgetHost.allocateAppWidgetId();
boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
mPendingBindWidgetId, mRequest.getAppWidgetProviderInfo(this), mWidgetOptions);
if (success) {
acceptWidget(mPendingBindWidgetId);
return;
}
// request bind widget
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingBindWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER,
mPendingWidgetInfo.componentName);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE,
mRequest.getAppWidgetProviderInfo(this).getProfile());
startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
}
public void startBindFlow(Launcher launcher, int appWidgetId, ItemInfo info, int requestCode) {
launcher.setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, this, info));
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, mProviderInfo.provider);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE,
mProviderInfo.getProfile());
// TODO: we need to make sure that this accounts for the options bundle.
// intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
launcher.startActivityForResult(intent, requestCode);
}
public static View getWidgetView(final Context context, final DesktopCallback callback, final DragAction.Action type, final Item item) {
if (HomeActivity._appWidgetHost == null) return null;
AppWidgetProviderInfo appWidgetInfo = HomeActivity._appWidgetManager.getAppWidgetInfo(item.getWidgetValue());
// If we can't find the Widget, we don't want to proceed or we'll end up with a phantom on the home screen.
if (appWidgetInfo == null) {
int appWidgetId = HomeActivity._appWidgetHost.allocateAppWidgetId();
if (item._label.contains(Definitions.DELIMITER)) {
String[] cnSplit = item._label.split(Definitions.DELIMITER);
ComponentName cn = new ComponentName(cnSplit[0], cnSplit[1]);
if (HomeActivity._appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn)) {
appWidgetInfo = HomeActivity._appWidgetManager.getAppWidgetInfo(appWidgetId);
item.setWidgetValue(appWidgetId);
HomeActivity._db.updateItem(item);
} else {
LOG.error("Unable to bind app widget id: {} ", cn);
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, cn);
HomeActivity._launcher.startActivityForResult(intent, HomeActivity.REQUEST_PICK_APPWIDGET);
return null;
}
} else {
// Delete the Widget if we don't have enough information to rehydrate it.
LOG.debug("Unable to identify Widget for rehydration; removing from database");
HomeActivity._db.deleteItem(item, false);
return null;
}
}
final WidgetView widgetView = (WidgetView) HomeActivity._appWidgetHost.createView(context, item.getWidgetValue(), appWidgetInfo);
widgetView.setAppWidget(item.getWidgetValue(), appWidgetInfo);
final WidgetContainer widgetContainer = new WidgetContainer(context, widgetView, item);
// TODO move this to standard DragHandler.getLongClick() method
// needs to be set on widgetView but use widgetContainer inside
widgetView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (Setup.appSettings().getDesktopLock()) {
return false;
}
if (Setup.appSettings().getGestureFeedback()) {
Tool.vibrate(view);
}
DragHandler.startDrag(widgetContainer, item, DragAction.Action.DESKTOP, callback);
return true;
}
});
widgetView.post(new Runnable() {
@Override
public void run() {
widgetContainer.updateWidgetOption(item);
}
});
return widgetContainer;
}
/**
* Process a widget drop.
*
* @param info
* The PendingAppWidgetInfo of the widget being added.
* @param screenId
* The ID of the screen where it should be added
* @param cell
* The cell it should be added to, optional
* @param position
* The location on the screen where it was dropped, optional
*/
void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container,
long screenId, int[] cell, int[] span, int[] loc) {
resetAddInfo();
mPendingAddInfo.container = info.container = container;
mPendingAddInfo.screenId = info.screenId = screenId;
mPendingAddInfo.dropPos = loc;
mPendingAddInfo.minSpanX = info.minSpanX;
mPendingAddInfo.minSpanY = info.minSpanY;
if (cell != null) {
mPendingAddInfo.cellX = cell[0];
mPendingAddInfo.cellY = cell[1];
}
if (span != null) {
mPendingAddInfo.spanX = span[0];
mPendingAddInfo.spanY = span[1];
}
AppWidgetHostView hostView = info.boundWidget;
int appWidgetId;
if (hostView != null) {
appWidgetId = hostView.getAppWidgetId();
addAppWidgetImpl(appWidgetId, info, hostView, info.info);
} else {
// In this case, we either need to start an activity to get
// permission to bind
// the widget, or we need to start an activity to configure the
// widget, or both.
appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Bundle options = info.bindOptions;
boolean success = false;
if (options != null) {
success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
appWidgetId, info.componentName, options);
} else {
success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
appWidgetId, info.componentName);
}
if (success) {
addAppWidgetImpl(appWidgetId, info, null, info.info);
} else {
mPendingAddWidgetInfo = info.info;
Intent intent = new Intent(
AppWidgetManager.ACTION_APPWIDGET_BIND);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER,
info.componentName);
// TODO: we need to make sure that this accounts for the options
// bundle.
// intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS,
// options);
startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
}
}
}