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

需要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("{http://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权限修改文件的操作权限!!!!


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值