在App Install上添加Android Widget

时间:2014-01-06 18:56:55

标签: android android-widget lockscreen

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

以下是我必须将小部件添加到主屏幕的内容。我错过了什么?

 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);
     }
   }
 }

1 个答案:

答案 0 :(得分:5)

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

欢迎您推广自己的ROM mod,其中您的应用程序小部件预先安装在主屏幕上,就像制造商那样。