自訂Launcher設為預設應用

來源:互聯網
上載者:User

標籤:自訂launcher

核心代碼:

....// launcher應用中包含的xml配置String action = Intent.ACTION_MAIN;String category1 = Intent.CATEGORY_HOME;String category2 = Intent.CATEGORY_DEFAULT;IntentFilter filter = new IntentFilter();filter.addAction(action);filter.addCategory(category1);filter.addCategory(category2);// 建立預設應用componentName,這裡為本應用ComponentName component = new ComponentName(getApplicationContext().getPackageName(), MainActivity.class.getName());Intent intent = new Intent();intent.setAction(action);intent.addCategory(category1);PackageManager pm = getPackageManager();// 使用PackageManager尋找action為Intent.ACTION_MAIN、category為Intent.CATEGORY_HOME的所有應用程式套件List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_INTENT_FILTERS);ComponentName[] comNames = new ComponentName[list.size()];int size = list.size();for(int i = 0; i < size ; i++){ActivityInfo activityInfo = list.get(i).activityInfo;String pckName = activityInfo.packageName;String clsName = activityInfo.name;// 迴圈清除原有的預設應用設定pm.clearPackagePreferredActivities(pckName);ComponentName cn = new ComponentName(pckName, clsName);comNames[i] = cn;}/* *  設定預設應用,對華為Link+源碼分析,最終設定參數持久化到/data/system/users/0/package-restrictions.xml 中 *   *  addPreferredActivity 需要 android.permission.SET_PREFERRED_APPLICATIONS許可權,該許可權需要使用signapk簽名  */pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, comNames, component);...

signapk簽名可參照

Android apk 擷取系統許可權的方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.