在App Install上添加Android窗口小部件

问题描述:

我正在尝试寻找一种自动添加小部件的方法,而无需让用户通过启动器手动将其添加到主页或锁定屏幕(最好是两者,但至少要添加到锁定屏幕).

I'm trying to find a way to automatically add a widget without making the user add it manually via the launcher to the home page or lock screen (preferably both, but at minimum to the lock screen).

这是我必须将小部件添加到主屏幕的地方.我想念什么?

Here's what I have to add the widget to the home screen. What am I missing?

 public class MyWidgetProvider extends AppWidgetProvider {

   private static final String ACTION_CLICK = "ACTION_CLICK";

   @Override
   public void onUpdate(Context context, AppWidgetManager appWidgetManager,
       int[] appWidgetIds) {


     ComponentName thisWidget = new ComponentName(context,
         MyWidgetProvider.class);
     int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
     final List<AppWidgetProviderInfo> infos = appWidgetManager.getInstalledProviders();


     AppWidgetProviderInfo appWidgetInfo = null;

     for (final AppWidgetProviderInfo info : infos) {
    Log.v("AD3", info.provider.getPackageName() + " / "
            + info.provider.getClassName());
}

     for (final AppWidgetProviderInfo info : infos) {
         if (info.provider.getClassName().equals(thisWidget.getClassName()) && info.provider.getPackageName().equals(thisWidget.getPackageName())) {

        appWidgetInfo = info;
        break;
         }
     }
     if (appWidgetInfo == null)
         return; //stop here

     for (int widgetId : allWidgetIds) {

       int number = (new Random().nextInt(100));

       RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
           R.layout.widget_layout);
       Log.w("WidgetExample", String.valueOf(number));
       remoteViews.setTextViewText(R.id.update, String.valueOf(number));

       Intent intent = new Intent(context, MyWidgetProvider.class);

       intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
       intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

       PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
           0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
       remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
       appWidgetManager.updateAppWidget(widgetId, remoteViews);
     }
   }
 }

您错过了不可能的事实.没有API可以将应用小部件强制添加到主屏幕或锁定屏幕.并非所有主屏幕和锁定屏幕都支持应用程序小部件.

You are missing the fact that this is not possible. There is no API to forcibly add an app widget to a home screen or lockscreen. Not all home screens and lockscreens even support app widgets.

欢迎您使用自己的ROM mod,该应用程序的小部件已按照制造商的方式预安装在主屏幕上.

You are welcome to roll your own ROM mod, where your app widget is pre-installed on the home screen, the way manufacturers do.