Android 源码分析(八) Launcher 桌面启动App过程
一.前言:
init進程 –> Zygote進程 –> SystemServer進程 –> Launcher桌面程序 -> 我們的App應用
init進程:linux的根進程,android系統是基于linux系統的,因此可以算作是整個android操作系統的第一個進程;
Zygote進程:android系統的根進程,主要作用:可以作用Zygote進程fork出SystemServer進程和各種應用進程;
SystemService進程:主要是在這個進程中啟動系統的各項服務,比如ActivityManagerService,PackageManagerService,WindowManagerService服務等等;
Launcher桌面程序:就是我們平時看到的桌面程序,它其實也是一個android應用程序,只不過這個應用程序是系統默認第一個啟動的應用程序.
二. Launcher 桌面啟動App過程分析
Launcher應用程序在啟動過程中會通過PackageManagerService服務請求查詢系統所有的已安裝應用的包名,圖標和應用名稱等信息,然后填充到Launcher中的Adapter中.
這樣點擊某一項應用圖標的時候就可以根據該圖標的包名和啟動Activity的類名初始化Intent對象,然后調用startActivity(Intent)啟動相關的應用程序了。
其實android中應用進程可以通過許多方式啟動,比如啟動一個Activity,啟動一個Service,啟動一個ContentProvider或者是一個BroadcastReceiver,也就是說我們可以通過啟動四大組件的方式啟動應用進程,在應用進程沒有啟動的時候,如果我們通過啟動這些組件,這時候系統會判斷當前這些組件所需要的應用進程是否已經啟動,若沒有的話,則會啟動應用進程。
//LauncherActivity.java
public abstract class LauncherActivity extends ListActivity {
......
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = intentForPosition(position);
startActivity(intent);
}
......
@Override
public void startActivity(Intent intent) {
this.startActivity(intent, null);
}
......
@Override
public void startActivity(Intent intent, @Nullable Bundle options) {
if (options != null) {
startActivityForResult(intent, -1, options);
} else {
// Note we want to go through this call for compatibility with
// applications that may have overridden the method.
startActivityForResult(intent, -1);
}
}
......
/**
* Adapter which shows the set of activities that can be performed for a given intent.
*/
private class ActivityAdapter extends BaseAdapter implements Filterable {
......
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = mInflater.inflate(
com.android.internal.R.layout.activity_list_item_2, parent, false);
} else {
view = convertView;
}
bindView(view, mActivitiesList.get(position));
return view;
}
private void bindView(View view, ListItem item) {
TextView text = (TextView) view;
text.setText(item.label);
if (mShowIcons) {
if (item.icon == null) {
item.icon = mIconResizer.createIconThumbnail(item.resolveInfo.loadIcon(getPackageManager()));
}
text.setCompoundDrawablesWithIntrinsicBounds(item.icon, null, null, null);
}
}
......
}
}
//activity_list.xml Launcher 桌面就是一個列表控件,來存放所有應用的圖標和名稱
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/activity_list_empty"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</FrameLayout>
//activity_list_item_2.xml 列表控件的子項,存放應用名稱和應用logo。drawablePadding="14dip".
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:textAppearance="?attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:drawablePadding="14dip"
android:paddingStart="?attr/listPreferredItemPaddingStart"
android:paddingEnd="?attr/listPreferredItemPaddingEnd" />
后面就進入了Activity的啟動流程了。
總結
以上是生活随笔為你收集整理的Android 源码分析(八) Launcher 桌面启动App过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网页走丢了是什么原因(区块链是什么)
- 下一篇: 存折丢了怎么办?