Android AppWidgetService需要android.permission.INTERACT_ACROSS_USERS_FULL

时间:2014-02-20 08:44:47

标签: android

Android上的主页小部件有一个奇怪的问题> = 4.2。每次尝试在AppWidgetManager上调用一些方法时(例如在AppWidgetProvider onUpdate中,当我尝试将小部件添加到主屏幕时),应用程序崩溃了:

02-20 08:32:48.281: E/AndroidRuntime(18978): java.lang.RuntimeException: Unable to start receiver cz.szn.app.launcher.LauncherWidget: java.lang.SecurityException: Permission Denial: AppWidgetService from com.android.server asks to run as user 23886906 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2453)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.app.ActivityThread.access$1700(ActivityThread.java:139)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1286)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.os.Looper.loop(Looper.java:136)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.app.ActivityThread.main(ActivityThread.java:5102)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at java.lang.reflect.Method.invokeNative(Native Method)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at java.lang.reflect.Method.invoke(Method.java:515)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at dalvik.system.NativeStart.main(Native Method)
02-20 08:32:48.281: E/AndroidRuntime(18978): Caused by: java.lang.SecurityException: Permission Denial: AppWidgetService from com.android.server asks to run as user 23886906 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.os.Parcel.readException(Parcel.java:1465)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.os.Parcel.readException(Parcel.java:1419)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at com.android.internal.appwidget.IAppWidgetService$Stub$Proxy.updateAppWidgetIds(IAppWidgetService.java:612)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.appwidget.AppWidgetManager.updateAppWidget(AppWidgetManager.java:378)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.appwidget.AppWidgetManager.updateAppWidget(AppWidgetManager.java:445)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at cz.szn.app.launcher.LauncherWidget.onUpdate(LauncherWidget.java:79)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at cz.szn.app.launcher.LauncherWidget.onReceive(LauncherWidget.java:41)
02-20 08:32:48.281: E/AndroidRuntime(18978):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2446)
02-20 08:32:48.281: E/AndroidRuntime(18978):    ... 10 more

在清单中是这样的:

<receiver android:name="cz.szn.app.launcher.LauncherWidget">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>

    <meta-data
         android:name="android.appwidget.provider"
         android:resource="@xml/app_launcher_widget" />
 </receiver>

在AppWidgetProvider sublcass中:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

    final int appWidgetCount = appWidgetIds.length;

    for (int i = 0; i < appWidgetCount; i++) {
        final int appWidgetId = appWidgetIds[i];

        PendingIntent openEmailIntent = PendingIntent.getActivity(context, 0, new Intent(
                context, FolderActivity.class), 0);

        PendingIntent writeMessageIntent = PendingIntent.getActivity(context, 0, new Intent(
                Intent.ACTION_SEND, null, context, FolderActivity.class), 0);

        PendingIntent syncIntent = PendingIntent.getBroadcast(context, 0, new Intent(
                Intent.ACTION_SYNC, null, context, EmailLauncherWidget.class), 0);

        final RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget_launcher);
        views.setOnClickPendingIntent(R.id.launcherWidgetLogo, openEmailIntent);
        views.setOnClickPendingIntent(R.id.launcherWidgetWrite, writeMessageIntent);
        views.setOnClickPendingIntent(R.id.launcherWidgetSync, syncIntent);

        views.setTextViewText(R.id.launcherWidgetName, "Moje Jmeno");

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

我阅读了App Widgets指南并一步一步地走了,但我看不出有什么问题。唯一不同的例子是我的应用程序还实现了AccountAuthenticator并需要帐户,使用SyncAdapter并具有自定义Application类。

我还注意到它在我从AccountManager获取帐户之前可以工作(我可以将小部件添加到主屏幕),登录后崩溃。它也适用于我从应用程序登录,然后清除应用程序数据(但帐户仍在系统中)。

有任何帮助吗?谢谢!

0 个答案:

没有答案