Android7.0 MTK设置默认桌面
程序员文章站
2022-05-14 15:23:52
本文实例为大家分享了android7.0 mtk设置默认桌面的具体代码,供大家参考,具体内容如下
项目需求:客户安装自己公司的桌面apk,安装完成后自动设置为默认桌面且不...
本文实例为大家分享了android7.0 mtk设置默认桌面的具体代码,供大家参考,具体内容如下
项目需求:客户安装自己公司的桌面apk,安装完成后自动设置为默认桌面且不弹出始终和仅一次的弹框
1.找到安装应用的文件
frameworks\base\services\core\java\com\android\server\pm\packagemanagerservice.java try { packageparser.package newpackage = scanpackagetracedli(pkg, policyflags, scanflags, system.currenttimemillis(), user); updatesettingsli(newpackage, installerpackagename, null, res, user); if (res.returncode == packagemanager.install_succeeded) { prepareappdataafterinstalllif(newpackage); log.d("yh", "pkgname " +pkgname); // 由于客户的应用保密 这里使用点心桌面的包名---com.dianxinos.dxhome if (pkgname.equals("com.dianxinos.dxhome")){ //发送广播 intent intent = new intent("android.intent.action.update_lanucher_apps"); mcontext.sendbroadcast(intent); } //------------------------ } else { // remove package from internal structures, but keep around any // data that might have already existed deletepackagelif(pkgname, userhandle.all, false, null, packagemanager.delete_keep_data, res.removedinfo, true, null); } } catch (packagemanagerexception e) { res.seterror("package couldn't be installed in " + pkg.codepath, e); } trace.traceend(trace_tag_package_manager); }
2.由于设置默认桌面时,是在settings中设置的所以广播接收者在settings中添加并设置默认桌面(添加文件–packages\apps\settings\src\com\android\settings下—updatelanucherreceiver.java)
package com.android.settings; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.componentname; import android.content.intentfilter; import android.content.pm.packagemanager; import android.content.pm.resolveinfo; import java.util.arraylist; import android.util.log; import android.content.pm.activityinfo; import java.util.list; public class updatelanucherreceiver extends broadcastreceiver { public updatelanucherreceiver() { } @override public void onreceive(context context, intent intent) { if (intent.getaction().equals("android.intent.action.update_lanucher_apps")) { log.e("yhyh" ," onreceive"); final packagemanager mpm = context.getpackagemanager(); // 点心桌面的包名类名 com.dianxinos.dxhome / com.nd.hilauncherdev.launcher.launcher设置默认桌面 componentname defaultlauncher=new componentname(" com.dianxinos.dxhome", "com.nd.hilauncherdev.launcher.launcher"); arraylist<resolveinfo> homeactivities = new arraylist<resolveinfo>(); componentname currentdefaulthome = mpm.gethomeactivities(homeactivities); componentname[]mhomecomponentset = new componentname[homeactivities.size()]; for (int i = 0; i < homeactivities.size(); i++) { final resolveinfo candidate = homeactivities.get(i); log.e("yhyh","homeactivitie: candidate = "+candidate); final activityinfo activityinfo= candidate.activityinfo; componentname activityname = new componentname(activityinfo.packagename, activityinfo.name); mhomecomponentset[i] = activityname; } intentfilter mhomefilter = new intentfilter(intent.action_main); mhomefilter.addcategory(intent.category_home); mhomefilter.addcategory(intent.category_default); list<componentname>activities=new arraylist(); mpm.replacepreferredactivity(mhomefilter, intentfilter.match_category_empty,mhomecomponentset, defaultlauncher); //刷新桌面 intent intent2 = new intent(intent.action_main); intent2.setflags(intent.flag_activity_new_task); intent2.addcategory(intent.category_home); context.startactivity(intent2); } } }
3.packages\apps\settings\androidmanifest.xml
<!-- yh --> <receiver android:name=".updatelanucherreceiver" > <intent-filter> <action android:name="android.intent.action.update_lanucher_apps" /> </intent-filter> </receiver>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: Android事件分发机制