編輯:關於Android編程
當系統中有多個Launcher桌面時,可通過以下方式設定默認的Launcher:
偷懶的話,將不想要的Launcher,如Launcher3的AndroidManifest.xml文件中的HOME屬性去掉,只保留自己想要的Launcher即可,前提是我們有其他Launcher的源碼。弊端是去掉Home屬性後再也無法進入。
1.編輯:
frameworks/base/services/java/com/android/server/pm/PackageManagerService.java
文件中的
public void systemReady() {}函數,在最後添加以下代碼段:
// TChip ZJ Add Below for Default Launcher
if (isFirstBoot()) {
// 修改為需要設置的 package name
String examplePackageName = com.tchip.carlauncher;
// 修改為需要設置的 launcher activity name
String exampleActivityName = com.tchip.carlauncher.ui.activity.MainActivity;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
final int callingUserId = UserHandle.getCallingUserId();
List resolveInfoList = queryIntentActivities(intent,
null, PackageManager.GET_META_DATA, callingUserId);
if (resolveInfoList != null) {
int size = resolveInfoList.size();
for (int j = 0; j < size;) {
final ResolveInfo r = resolveInfoList.get(j);
if (!r.activityInfo.packageName.equals(examplePackageName)) {
resolveInfoList.remove(j);
size -= 1;
} else {
j++;
}
}
ComponentName[] set = new ComponentName[size];
ComponentName defaultLauncher = new ComponentName(
examplePackageName, exampleActivityName);
int defaultMatch = 0;
for (int i = 0; i < size; i++) {
final ResolveInfo resolveInfo = resolveInfoList.get(i);
set[i] = new ComponentName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
if (defaultLauncher.getClassName().equals(
resolveInfo.activityInfo.name)) {
defaultMatch = resolveInfo.match;
}
}
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
filter.addCategory(Intent.CATEGORY_DEFAULT);
addPreferredActivity2(filter, defaultMatch, set,
defaultLauncher);
}
}
// TChip ZJ Add Above for Default Launcher
2.添加以下函數:
/**
* TChip ZJ Add for Default Launcher
*/
public void addPreferredActivity2(IntentFilter filter, int match,
ComponentName[] set, ComponentName activity) {
Log.d(debug_default, addPreferredActivity2 is called.);
// writer
synchronized (mPackages) {
Slog.i(TAG, Adding preferred activity + activity + :);
filter.dump(new LogPrinter(Log.INFO, TAG), );
mSettings.editPreferredActivitiesLPw(0).addFilter(
new PreferredActivity(filter, match, set, activity, true));
scheduleWriteSettingsLocked();
}
}
3.修改PackageManagerService.java的函數findPreferredActivity:
(1)對removeMatches進行條件判斷,在launcher時不remove:
if (!(intent.getAction() != null
&& intent.getAction().equals(intent.ACTION_MAIN)
&& intent.getCategories() != null && intent.getCategories()
.contains(intent.CATEGORY_HOME))) {
Slog.d(TAG, launcher);
} else {
if (removeMatches) {
pir.removeFilter(pa);
if (DEBUG_PREFERRED) {
Slog.v(TAG, Removing match + pa.mPref.mComponent);
}
break;
}
}
(2)
if (always && !pa.mPref.sameSet(query, priority)) {
Slog.i(TAG, Result set changed, dropping preferred activity for
+ intent + type + resolvedType);
if (DEBUG_PREFERRED) {
Slog.v(TAG, Removing preferred activity since set changed
+ pa.mPref.mComponent);
}
pir.removeFilter(pa);
// Re-add the filter as a last chosen entry (!always)
PreferredActivity lastChosen = new PreferredActivity(pa,
pa.mPref.mMatch, null, pa.mPref.mComponent, false);
pir.addFilter(lastChosen);
mSettings.writePackageRestrictionsLPr(userId);
return null;
}
上面這段修改為下面的內容:
if (always && !pa.mPref.sameSet(query, priority)) {
if (!(intent.getAction() != null
&& intent.getAction().equals(intent.ACTION_MAIN)
&& intent.getCategories() != null && intent.getCategories()
.contains(intent.CATEGORY_HOME))) { // MTK ADD
Slog.i(TAG,
Result set changed, dropping preferred activity for
+ intent + type + resolvedType);
if (DEBUG_PREFERRED) {
Slog.v(TAG,
Removing preferred activity since set changed
+ pa.mPref.mComponent);
}
pir.removeFilter(pa);
// Re-add the filter as a last chosen entry (!always)
PreferredActivity lastChosen = new PreferredActivity(pa,
pa.mPref.mMatch, null, pa.mPref.mComponent, false);
pir.addFilter(lastChosen);
mSettings.writePackageRestrictionsLPr(userId);
return null;
}
}
Slog.i(TAG,
Result set not change, not drop preferred activity for special intent:
+ intent);// MTK ADD
// Yay! Either the set matched or we're looking for the last chosen
概括這篇博客裡面就來實踐下。在上一篇博客裡面說到了OkHttp類似HttpUrlConnection。按這樣說的話,我們在項目中肯定還是要封裝一層。如果嫌封裝麻煩的話,也
Intent的匹配過程中有三個步驟,包括Action , category與data 的匹配。如果匹配出了多個結果,系統會顯示一個dialog讓用戶來選
Android使用XML聲明界面布局將程序的表現層和控制層分離修改用戶界面時,無需更改程序的源代碼可視化工具設計用戶界面Android五種布局方式LinearLayout
隨著移動互聯網的快速發展,它已經和我們的生活息息相關了,在公交地鐵裡面都能看到很多人的人低頭看著自己的手機屏幕,從此“低頭族”一詞就產生了,作為一名移動行業的開發人員,我