Search code examples
androidandroid-activityclickpreviewandroid-appwidget

android appWidget - put my own appWidget without user confirmation


Suppose I have an android project which has working appWidget that can be added to the app-launcher. The project also has an activity that shows the appWidget that I've created.

the probelm: For the launcher, the appWidget works perfectly and it responds to user clicks, but for the activity, it is only shown , as if it's a preview .

i've tried (and succeeded) showing other appWidgets , but they are also only being shown . however, clicking on buttons seem to have a clicked style effect , as if it might be possible to handle touch events. note that the appWidget could have extra functionality in the future in addition to user clicks .

Can anyone tell me what can I do in order to handle this issue? Is it even possible to do such a thing, and if not, why (since it's a part of the application) ? Note that I want to do as little changes to the appWidget as possible, since the appWidget can be a complex one.

here's a snippet for getting the appWidget view in order to show it:

public View fetchAppWidgetView(final Context context, final ComponentName componentName)
{
  final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  _appWidgetHost = new AppWidgetHost(context, INSTANCES_ID_PROVIDER);
  // get appWidgetInfo :
  final List<AppWidgetProviderInfo> infos = appWidgetManager.getInstalledProviders();
  AppWidgetProviderInfo appWidgetInfo = null;
  for (final AppWidgetProviderInfo info:infos)
    if (info.provider.getClassName().equals(componentName.getClassName()) && info.provider.getPackageName().equals(componentName.getPackageName()))
    {
      appWidgetInfo = info;
      break;
    }
  if (appWidgetInfo == null)
    return null;
  final int appWidgetId = _appWidgetHost.allocateAppWidgetId();
  // create and put the new widget:
  final AppWidgetHostView newWidget = _appWidgetHost.createView(context, appWidgetId, appWidgetInfo);
  newWidget.setAppWidget(appWidgetId, appWidgetInfo);
  _appWidgetHost.startListening();
  return newWidget;
}

public void startListening()
{
  //this method is called on the onStart() method of the activity
  _appWidgetHost.startListening();
}

Solution

  • This is for security reasons ok - your app doesnt have permissions to put a app widget without a users knowledge on a screen. the only ways you have is to let the user pick the widget from a list, or, your app have to be installed in the /system/app folder and in this way the app is a system app with all needed permissions.