Android 开发之开机默认桌面修改教程
白羽 2018-06-05 来源 :网络 阅读 1367 评论 0

摘要:本文将带你了解Android 开发之开机默认桌面修改教程,希望本文对大家学Android有所帮助。




需要root权限

文件路径/data/system/packages.xml里边的preferred-activities属性!

把自己需要的桌面应用设置上Attribute

<strong>android.intent.category.DEFAULT</strong>

   


<strong>android.intent.category.HOME</strong>

   



   

<strong>android.intent.action.MAIN</strong>

   



<strong>首先是一个bean类</strong>

   


<strong>public class </strong>XmlBean {
<strong>public static final </strong>String <strong>
<em>NODE</em></strong>=<strong>"preferred-activities"</strong>;
 <strong>private </strong>String <strong>value</strong>;
<strong>public </strong>String getValue() {
<strong>return </strong><strong>value</strong>; }
<strong>public void </strong>setValue(String value) {
<strong>this</strong>.<strong>value </strong>= value; } }

   


<strong>工具类</strong>

   


<em>/** </em><em> * dom解析工具类 </em><em> */ </em><em> </em>
<strong>public class </strong>DomUtils {
<strong>private static </strong>DocumentBuilderFactory
<em>factory </em>= DocumentBuilderFactory.newInstance();
<strong>private static </strong>XPathFactory <em>xpathFactory
</em>= XPathFactory.newInstance();<em>//创建XPath工厂 </em><em>
</em><strong>private static </strong>XPath <em>xpath </em>=
<em>xpathFactory</em>.newXPath();<em>//创建XPath对象 </em><em>
 </em><em> </em><em> /** </em><em> * 修改添加xml文件 </em><em> *
</em><em> * </em><strong><em>@param </em></strong><strong>
<em>filePath </em></strong><em>恢复的xml文件路径 </em><em> */
</em><em> </em><strong>public static void
</strong>writeXml(String filePath, XmlBean bean) { <strong>try
</strong>{ DocumentBuilder db =
<em>factory</em>.newDocumentBuilder(); File file = <strong>new
</strong>File(filePath); Document xmlDoc = db.parse(file);
Element root = xmlDoc.getDocumentElement(); Element son =
(Element) selectSingleNode(<strong>"//preferred-activities"
</strong>, root); <strong>if </strong>(son !=
<strong>null</strong>) { root.removeChild(son); } <em>//如果查不
到意味着要插入 </em><em> </em>Element newElement; newElement =
xmlDoc.createElement(XmlBean.<strong><em>NODE</em></strong>);
Element item = xmlDoc.createElement(<strong>"item"</strong>);
item.setAttribute(<strong>"name"</strong>,
<strong>"com.android.unan.urineanalysis/.activity.MainActivity"
</strong>); item.setAttribute(<strong>"match"</strong>,
<strong>"100000"</strong>); item.setAttribute(<strong>"set"
</strong>, <strong>"2"</strong>); Element set =
xmlDoc.createElement(<strong>"set"</strong>);
set.setAttribute(<strong>"name"</strong>,
<strong>"com.android.unan.urineanalysis/.activity.MainActivity"
</strong>); Element set2 = xmlDoc.createElement(<strong>"set"
</strong>); set2.setAttribute(<strong>"name"</strong>,
<strong>"com.android.launcher/com.android.launcher2.Launcher"
</strong>); Element filter =
xmlDoc.createElement(<strong>"filter"</strong>); Element action
= xmlDoc.createElement(<strong>"action"</strong>);
action.setAttribute(<strong>"name"</strong>,
<strong>"android.intent.action.MAIN"</strong>); Element cat =
xmlDoc.createElement(<strong>"cat"</strong>);
cat.setAttribute(<strong>"name"</strong>,
<strong>"android.intent.category.HOME"</strong>); Element cat2 =xmlDoc.createElement(<strong>"cat"</strong>);
cat2.setAttribute(<strong>"name"</strong>,
<strong>"android.intent.category.DEFAULT"</strong>);
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.<strong><em>INDENT</em>
</strong>, <strong>"yes"</strong>);<em>//增加换行 </em><em>//
former.setOutputProperty("{https://xml.apache.org/xslt}indent-
amount", "2");//增加缩进 </em><em>
</em>former.transform(<strong>new </strong>DOMSource(xmlDoc),
<strong>new </strong>StreamResult(file)); } <strong>catch
</strong>(Exception e) { e.printStackTrace(); } } <em>// 获取指
定节点信息 </em><em> </em><strong>private static </strong>Node
selectSingleNode(String express, Element source) { Node result =
<strong>null</strong>; <strong>try </strong>{ result = (Node)
<em>xpath</em>.evaluate(express, source, XPathConstants.<strong>
<em>NODE</em></strong>); } <strong>catch </strong>
(XPathExpressionException e) { System.<strong><em>out</em>
</strong>.println(e.getMessage()); } <strong>return
</strong>result; } }

   




   


 

使用方式十分简单

   


DomUtils.writeXml(Contents.<strong>
<em>SYSTEM_DEFULTLAUNCHER</em></strong>
<strong>new </strong>XmlBean());

   


操作前请用root权限修改文件的操作权限!!!!


本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!


本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程