如何在自定义Android主屏幕中添加默认小部件?

时间:2011-09-28 04:20:11

标签: android android-widget android-launcher

我使用Launcher2 home screen作为自定义主屏幕的基础。 我想用默认的Widget自定义Android主屏幕。

但是当我使用这个例子时,我收到了这个错误:

09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187): Problem allocating appWidgetId
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187): java.lang.SecurityException: bindGagetId appWidgetId=68 provider=ComponentInfo{com.android.protips/com.android.protips.ProtipWidget}: User 10081 does not have android.permission.BIND_APPWIDGET.
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at android.os.Parcel.readException(Parcel.java:1322)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at android.os.Parcel.readException(Parcel.java:1276)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at com.android.internal.appwidget.IAppWidgetService$Stub$Proxy.bindAppWidgetId(IAppWidgetService.java:470)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at android.appwidget.AppWidgetManager.bindAppWidgetId(AppWidgetManager.java:337)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at com.android.launcher2.LauncherProvider$DatabaseHelper.addAppWidget(LauncherProvider.java:812)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at com.android.launcher2.LauncherProvider$DatabaseHelper.addAppWidget(LauncherProvider.java:790)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at com.android.launcher2.LauncherProvider$DatabaseHelper.loadFavorites(LauncherProvider.java:670)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at com.android.launcher2.LauncherProvider$DatabaseHelper.onCreate(LauncherProvider.java:239)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at com.android.launcher2.LauncherProvider.query(LauncherProvider.java:108)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at android.content.ContentProvider$Transport.query(ContentProvider.java:187)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at android.content.ContentResolver.query(ContentResolver.java:271)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at com.android.launcher2.LauncherModel$Loader$LoaderThread.loadWorkspace(LauncherModel.java:711)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at com.android.launcher2.LauncherModel$Loader$LoaderThread.loadAndBindWorkspace(LauncherModel.java:521)
09-28 12:03:13.052: ERROR/Launcher.LauncherProvider(9187):     at com.android.launcher2.LauncherModel$Loader$LoaderThread.run(LauncherModel.java:588)

我已经在我的清单中添加了android.permission.BIND_APPWIDGET,但它仍然无效,我的默认小部件仍然缺失。

有没有人知道会出现什么问题?

1 个答案:

答案 0 :(得分:4)

android.permission.BIND_APPWIDGET不适用于系统存储之外的应用程序。

如果没有完整的堆栈跟踪,我无法确定错误的确切位置,但这是我所期望的导致问题。

void addAppWidget(Intent data) {
        int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
        AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);

        if (appWidget.configure != null) {
            // Launch over to configure widget, if needed
            Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
            intent.setComponent(appWidget.configure);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

            startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
        } else {
            // Otherwise just add it
            onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data);
        }
}