做sp31项目时测试这边提出一个要求,
手机添加小部件时弹出对话框"是否允许launcher3创建小部件并查看其数据",建议去除该对话框
看了两天代码,结果只要加一行代码就能解决问题了。解决方法如下:
在Launcher.java里 函数
void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId, int[] cell, int[] span, int[] loc) { if (LauncherLog.DEBUG) { LauncherLog.d(TAG, "addAppWidgetFromDrop: info = " + info + ", container = " + container + ", screenId = " + screenId); } resetAddInfo(); mPendingAddInfo.container = info.container = container; mPendingAddInfo.screenId = info.screenId = screenId; mPendingAddInfo.dropPos = loc; mPendingAddInfo.minSpanX = info.minSpanX; mPendingAddInfo.minSpanY = info.minSpanY; if (cell != null) { mPendingAddInfo.cellX = cell[0]; mPendingAddInfo.cellY = cell[1]; } if (span != null) { mPendingAddInfo.spanX = span[0]; mPendingAddInfo.spanY = span[1]; } AppWidgetHostView hostView = info.boundWidget; int appWidgetId; if (hostView != null) { appWidgetId = hostView.getAppWidgetId(); addAppWidgetImpl(appWidgetId, info, hostView, info.info); } else { // In this case, we either need to start an activity to get permission to bind // the widget, or we need to start an activity to configure the widget, or both. appWidgetId = getAppWidgetHost().allocateAppWidgetId(); Bundle options = info.bindOptions; boolean success = false; mAppWidgetManager.setBindAppWidgetPermission(this.getPackageName(), true); //txk add if (options != null) { success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName, options); } else { success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName); } if (success) { addAppWidgetImpl(appWidgetId, info, null, info.info); } else { /* 当用户第一次添加widget的时候会走到这里,弹出一个提示框让用户授权。为了不让代码走到这里,只要在 * mAppWidgetManager.bindAppWidgetIdIfAllowed(……)之前提前授权就好了。添加一行授权代码 * * mAppWidgetManager.setBindAppWidgetPermission(this.getPackageName(), true); * * 但是需要在Launcher3的AndroidManifest.xml里添加相应权限 * <uses-permission android:name="android.permission.MODIFY_APPWIDGET_BIND_PERMISSIONS"/> * 而这个权限是系统级权限,所以还需要把Launcher3改为系统级应用,在AndroidManifest.xml添加 * android:sharedUserId="android.uid.system" 这个标签 * 还需要在Android.mk里 把LOCAL_CERTIFICATE := shared 改为LOCAL_CERTIFICATE := platform */ mPendingAddWidgetInfo = info.info; Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName); // TODO: we need to make sure that this accounts for the options bundle. // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options); startActivityForResult(intent, REQUEST_BIND_APPWIDGET); } } }
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.launcher3" android:sharedUserId="android.uid.system" > <uses-sdk android:targetSdkVersion="21" android:minSdkVersion="16"/> ……………… <!-- xuankai.tang add 2015.5.20 start--> <uses-permission android:name="android.permission.MODIFY_APPWIDGET_BIND_PERMISSIONS"/> <!-- xuankai.tang add 2015.5.20 end --> ……………… </manifest>