我需要什么
我正在为一家公司开发定制Android启动器,该启动器将安装在该公司的平板电脑上,因此不会在任何商店发布。 它基本上是一个远程管理小部件的网格(通过我们正在创建的后台服务),这意味着应用程序应该决定在用户打开时添加或删除哪些小部件。
我有什么
我正在使用以下代码添加小部件:
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getContext());
AppWidgetHost appWidgetHost = new AppWidgetHost(getContext(), APPWIDGET_HOST_ID);
appWidgetHost.startListening();
List<AppWidgetProviderInfo> appWidgetInfos appWidgetInfos = appWidgetManager.getInstalledProviders();
for(int j = 0; j < appWidgetInfos.size(); j++)
{
if (appWidgetInfos.get(j).provider.getPackageName().equals(widgetPackage))
{
// Allocates an id
int appWidgetId = appWidgetHost.allocateAppWidgetId();
// Gets the app widget info
AppWidgetProviderInfo appWidgetProviderInfo = appWidgetInfos.get(j);
// Creates Widget
AppWidgetHostView hostView = appWidgetHost.createView(getContext(), appWidgetId, appWidgetProviderInfo);
hostView.setAppWidget(appWidgetId, appWidgetProviderInfo);
// Insers the view to the layout grid
insertView(row, column, rowspan, columnspan, hostView);
//
break;
}
}
它运作得很好。小部件显示,它的按钮响应用户触摸。
这是onUpdate上的一个小部件:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
// Perform this loop procedure for each App Widget that belongs to this provider
for (int appWidgetId : appWidgetIds)
{
// Create an Intent to launch ExampleActivity
Intent launchAppIntent = new Intent(context, MapActivity.class);
PendingIntent launchApp = PendingIntent.getActivity(context, 0, launchAppIntent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.widget_layout, launchApp);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
问题
其中一个小部件(如上所示)调用Intent在单击时启动它的全屏Activity,这在我的自定义启动器上不起作用,尽管它在默认的主启动器上运行良好。