2

我需要的

我正在为一家公司开发一个定制的 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,这在我的自定义启动器上不起作用,尽管它在默认主页启动器上完美运行。

4

0 回答 0