需要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权限修改文件的操作权限!!!!
如对本文有疑问, 点击进行留言回复!!
【Appium踩坑】小米手机,启动报错:exited with code 255 writing to settings requires:android.permission.WRITE_SECUR
android 拍照 预览图与 照片分辨率(可视区域)不一致
[PAT顶级]1025 Keep at Most 100 Characters (35分)
Android 天气APP(二十)增加欢迎页及白屏黑屏处理、展示世界国家/地区的城市数据
Android使用SharedPreferences保存List列表数据
解决android sdk 运行出现 could not install *smartsocket* listener: cannot bind to 127.0.0.1:5037:的问题
网友评论