Android以编程方式将小部件添加到应用程序警告消息

时间:2014-11-10 16:02:28

标签: java android widget

我创建了一个测试Android启动器应用程序,并将其作为系统应用程序安装在root设备上。目标是让应用程序将所有已安装的小部件放在viewpager的片段上。这已成功完成;但是,每次显示新页面时都会向用户显示一条消息。

no description

即使用户选中该框,每次添加小部件时仍会显示该消息。

我通常会按照Hosting widgets in an android launcherAdding widgets to a launcher page without bindAppWidgetId()中的步骤向应用添加小部件。

首先,我获得了类中的小部件列表,这些小部件扩展了应用程序并将其存储在公共静态变量中

AppWidgetManager manager = AppWidgetManager.getInstance(this); 
widgetList = manager.getInstalledProviders();

接下来,我使用绑定意图获取小部件

appWidgetManager = AppWidgetManager.getInstance(this.getActivity());
appWidgetHost = new AppWidgetHost(this.getActivity(), APPWIDGET_HOST_ID);
int id = appWidgetHost.allocateAppWidgetId();
Intent bindIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
bindIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER,LauncherExperiment.widgetList.get(mParam1).provider);
startActivityForResult(bindIntent, REQUEST_BIND_APPWIDGET);

然后在活动结果中创建小部件

Bundle extras = data.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
AppWidgetProviderInfo appWidgetInfo = appWidgetManager.getAppWidgetInfo(appWidgetId);

AppWidgetHostView hostView = appWidgetHost.createView(this.getActivity(), appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
// Add  it on the layout you want
myLayout.addView(hostView);

同样,我已经根据设备生根并通过更新清单

将应用程序安装为系统应用程序
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.launcherexperiment"
android:sharedUserId="android.uid.system" >

按照以下步骤操作: How can I sign my application with the system signature key?

总而言之,我已经能够成功绑定应用并显示它们;但问题是警告信息最多只能显示一次。

1 个答案:

答案 0 :(得分:2)

我们走吧!使用此代码,用户只会显示一次对话框。 它还显示了以编程方式添加小部件的完整解决方案。

我将在我的github上更新此内容,请在此处查看最新版本https://github.com/DagW/FragmentHomescreen/blob/master/app/src/main/java/se/dw/fragmenthomescreen/widget/WidgetFragment.java

但是对于归档目的,这就是我现在的方式。

/**
 * checkToAddWidget
 * <p/>
 * At the creation of WidgetFragment,
 * add a default widget so that the homescreen is not empty
 * shows how to add widgets programmatically
 */
private void checkToAddWidget () {
        MainActivity m = (MainActivity) getActivity();
        AppWidgetManager manager = m.getAppWidgetManager();
        AppWidgetHost host = m.getAppWidgetHost();

        List<AppWidgetProviderInfo> widgetList = manager.getInstalledProviders();

        AppWidgetProviderInfo searchProvider = null;
        AppWidgetProviderInfo search2Provider = null;
        AppWidgetProviderInfo clockProvider = null;

        for ( AppWidgetProviderInfo info : widgetList ) {
            Log.d(TAG, info.provider.getPackageName() + " " + info.provider.getClassName());
            if ( info.provider.getClassName().equals("com.google.android.googlequicksearchbox.SearchWidgetProvider") ) {
                searchProvider = info;
                break;
            }
            if ( info.provider.getClassName().equals("com.android.alarmclock.AnalogAppWidgetProvider") ) {
                clockProvider = info;
            }
            if ( info.provider.getClassName().equals("com.android.alarmclock.DigitalAppWidgetProvider") ) {
                clockProvider = info;
            }
            if ( info.provider.getClassName().equals("com.android.quicksearchbox.SearchWidgetProvider") ) {
                search2Provider = info;
            }
        }
        if ( searchProvider != null || search2Provider != null || clockProvider != null ) {
            AppWidgetProviderInfo provider = null;
            if ( searchProvider != null ) {
                provider = searchProvider;
            } else if ( clockProvider != null ) {
                provider = clockProvider;
            } else {
                provider = search2Provider;
            }

            addProvider(m, host, manager, provider);
        }
    }



public void addProvider (MainActivity m, AppWidgetHost host, AppWidgetManager manager, AppWidgetProviderInfo provider) {
    int id = host.allocateAppWidgetId();
    boolean success = false;
    success = manager.bindAppWidgetIdIfAllowed(id, provider.provider);

    if ( success ) {
        AppWidgetHostView hostView = host.createView(getActivity(), id, provider);
        AppWidgetProviderInfo appWidgetInfo = manager.getAppWidgetInfo(id);

        LauncherAppWidgetInfo info = new LauncherAppWidgetInfo(id);
        info.setHostView(hostView);
        info.getHostView().setAppWidget(id, appWidgetInfo);

        ItemInfo launcherInfo = attachWidget(info);
        if ( launcherInfo != null ) {
            WidgetPersistance.addDesktopAppWidget(screen, launcherInfo);
        }

    } else {
        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, provider.provider);
        m.startActivityForResult(intent, MainActivity.REQUEST_BIND_APPWIDGET);
    }

}

您可能还需要

<uses-permission android:name="android.permission.BIND_APPWIDGET"
                 tools:ignore="ProtectedPermissions"/>