欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android 开发板开机默认桌面修改教程

程序员文章站 2022-03-16 16:42:47
需要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权限修改文件的操作权限!!!!

上一篇: 2018目标

下一篇: CSS3动画基本使用