android 开发板开机默认桌面修改教程
程序员文章站
2022-07-01 08:56:52
需要root权限
文件路径/data/system/packages.xml里边的preferred-activities属性!
把自己需要的桌面应用设置上Attribute...
需要root权限
文件路径/data/system/packages.xml里边的preferred-activities属性!
把自己需要的桌面应用设置上Attribute
android.intent.category.DEFAULT
android.intent.category.HOME
android.intent.action.MAIN
首先是一个bean类
public class XmlBean { public static final String NODE="preferred-activities"; private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
工具类
/** * dom解析工具类 */ public class DomUtils { private static DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); private static XPathFactory xpathFactory = XPathFactory.newInstance();//创建XPath工厂 private static XPath xpath = xpathFactory.newXPath();//创建XPath对象 /** * 修改添加xml文件 * * @param filePath 恢复的xml文件路径 */ public static void writeXml(String filePath, XmlBean bean) { try { DocumentBuilder db = factory.newDocumentBuilder(); File file = new File(filePath); Document xmlDoc = db.parse(file); Element root = xmlDoc.getDocumentElement(); Element son = (Element) selectSingleNode("//preferred-activities", root); if (son != null) { root.removeChild(son); } //如果查不到意味着要插入 Element newElement; newElement = xmlDoc.createElement(XmlBean.NODE); Element item = xmlDoc.createElement("item"); item.setAttribute("name", "com.android.unan.urineanalysis/.activity.MainActivity"); item.setAttribute("match", "100000"); item.setAttribute("set", "2"); Element set = xmlDoc.createElement("set"); set.setAttribute("name", "com.android.unan.urineanalysis/.activity.MainActivity"); Element set2 = xmlDoc.createElement("set"); set2.setAttribute("name", "com.android.launcher/com.android.launcher2.Launcher"); Element filter = xmlDoc.createElement("filter"); Element action = xmlDoc.createElement("action"); action.setAttribute("name", "android.intent.action.MAIN"); Element cat = xmlDoc.createElement("cat"); cat.setAttribute("name", "android.intent.category.HOME"); Element cat2 = xmlDoc.createElement("cat"); cat2.setAttribute("name", "android.intent.category.DEFAULT"); filter.appendChild(action); filter.appendChild(cat); filter.appendChild(cat2); item.appendChild(set); item.appendChild(set2); item.appendChild(filter); newElement.appendChild(item); root.appendChild(newElement); TransformerFactory factory = TransformerFactory.newInstance(); Transformer former = factory.newTransformer(); former.setOutputProperty(OutputKeys.INDENT, "yes");//增加换行 // former.setOutputProperty("{https://xml.apache.org/xslt}indent-amount", "2");//增加缩进 former.transform(new DOMSource(xmlDoc), new StreamResult(file)); } catch (Exception e) { e.printStackTrace(); } } // 获取指定节点信息 private static Node selectSingleNode(String express, Element source) { Node result = null; try { result = (Node) xpath.evaluate(express, source, XPathConstants.NODE); } catch (XPathExpressionException e) { System.out.println(e.getMessage()); } return result; } }
请原谅我写死了包名233 各位自行更改
使用方式十分简单
DomUtils.writeXml(Contents.SYSTEM_DEFULTLAUNCHER, new XmlBean());操作前请用root权限修改文件的操作权限!!!!
上一篇: CAD中设置图层状态和标注样式及文字样式该怎么永久保存?
下一篇: switchery按钮的使用方法