【原创文章,转载请声明】转自http://blog.csdn.net/allen_704/article/details/28640827
Android4.4对widget有了一些变化,遇到的问题如题所示,无法创建widget。
看4.4的源码,在frameworks\base\services\java\com\android\server\AppWidgetServiceImpl.java这个类里面
public int allocateAppWidgetId(String packageName, int hostId) {
int callingUid = enforceSystemOrCallingUid(packageName);
synchronized (mAppWidgetIds) {
if (!mHasFeature) {
return -1;
}
ensureStateLoadedLocked();
int appWidgetId = mNextAppWidgetId++;
Host host = lookupOrAddHostLocked(callingUid, packageName, hostId);
AppWidgetId id = new AppWidgetId();
id.appWidgetId = appWidgetId;
id.host = host;
host.instances.add(id);
mAppWidgetIds.add(id);
saveStateAsync();
if (DBG) log("Allocating AppWidgetId for " + packageName + " host=" + hostId
+ " id=" + appWidgetId);
return appWidgetId;
}
}
public int allocateAppWidgetId(String packageName, int hostId) {
int callingUid = enforceCallingUid(packageName);
synchronized (mAppWidgetIds) {
ensureStateLoadedLocked();
int appWidgetId = mNextAppWidgetId++;
Host host = lookupOrAddHostLocked(callingUid, packageName, hostId);
AppWidgetId id = new AppWidgetId();
id.appWidgetId = appWidgetId;
id.host = host;
host.instances.add(id);
mAppWidgetIds.add(id);
saveStateLocked();
return appWidgetId;
}
}
final boolean mHasFeature;
//-------------------------------------
AppWidgetServiceImpl(Context context, int userId, Handler saveStateHandler) {
mContext = context;
mPm = AppGlobals.getPackageManager();
mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
mUserId = userId;
mSaveStateHandler = saveStateHandler;
mHasFeature = context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_APP_WIDGETS);
computeMaximumWidgetBitmapMemory();
}
public boolean hasSystemFeature(String name) {
synchronized (mPackages) {
return mAvailableFeatures.containsKey(name);
}
}
继续看mAvailableFeatures这个集合是在哪里声明赋值的,首先看声明
// These are the features this devices supports that were read from the
// etc/permissions.xml file.
final HashMap mAvailableFeatures =
new HashMap();
private void readPermissionsFromXml(File permFile) {
FileReader permReader = null;
try {
permReader = new FileReader(permFile);
} catch (FileNotFoundException e) {
Slog.w(TAG, "Couldn't find or open permissions file " + permFile);
return;
}
try {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(permReader);
XmlUtils.beginDocument(parser, "permissions");
while (true) {
XmlUtils.nextElement(parser);
if (parser.getEventType() == XmlPullParser.END_DOCUMENT) {
break;
}
String name = parser.getName();
if ("group".equals(name)) {
// .....
} else if ("permission".equals(name)) {
// .....
} else if ("assign-permission".equals(name)) {
// .....
} else if ("library".equals(name)) {
// .....
} else if ("feature".equals(name)) {
String fname = parser.getAttributeValue(null, "name");
if (fname == null) {
Slog.w(TAG, " without name at "
+ parser.getPositionDescription());
} else {
//Log.i(TAG, "Got feature " + fname);
FeatureInfo fi = new FeatureInfo();
fi.name = fname;
mAvailableFeatures.put(fname, fi);
}
XmlUtils.skipCurrentTag(parser);
continue;
} else {
XmlUtils.skipCurrentTag(parser);
continue;
}
}
permReader.close();
} catch (XmlPullParserException e) {
Slog.w(TAG, "Got execption parsing permissions.", e);
} catch (IOException e) {
Slog.w(TAG, "Got execption parsing permissions.", e);
}
}
取出/system/etc/permissions.xml文件一搜,果真没有这个feature,同时也检查了etc目录下其他xml文件,都没有这个feature。
那么现在要做的就是增加这个feature,在permisions.xml中追加如下内容,也可以弄成单独xml放到etc目录
【原创文章,转载请声明】转自http://blog.csdn.net/allen_704/article/details/28640827