从源码看Activity启动过程(一)LaunchActivity

本文以点击桌面快捷方式启动app来看源码

Launcher process    ->   Binder    ->     System process    ->    Binder     ->        Target process
LauncherActivity  -> ActivityManagerProxy ->AMS -> ApplicationThreadProxy -> Instrumentation.newActivity

1、先看LaunchActivity源码

public abstract class LauncherActivity extends ListActivity {
	//如果通过component、schema启动一个app,那么这个mIntent中会有数据
	Intent mIntent;
	...
	private class ActivityAdapter extends BaseAdapter implements Filterable {
        ...
		public ActivityAdapter(IconResizer resizer) {
            ...
            mActivitiesList = makeListItems();
        }
		
        public Intent intentForPosition(int position) {
            if (mActivitiesList == null) {
                return null;
            }
			//mIntent一个空的intent,貌似m
            Intent intent = new Intent(mIntent);
            ListItem item = mActivitiesList.get(position);
            intent.setClassName(item.packageName, item.className);
            if (item.extras != null) {
                intent.putExtras(item.extras);
            }
            return intent;
        }
	}
	
	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
		//从adapter中取出点击item的数据封装成intent
        Intent intent = intentForPosition(position);
        startActivity(intent);//这里启动activity
    }
    
    /**
     * 返回点击的item的app信息
     */
    protected Intent intentForPosition(int position) {
        ActivityAdapter adapter = (ActivityAdapter) mAdapter;
        return adapter.intentForPosition(position);
    }
	//获取数据源,Launcher显示的数据
	public List makeListItems() {
		//关于这个mIntent
        List list = onQueryPackageManager(mIntent);
        onSortResultList(list);
        ArrayList result = new ArrayList(list.size());
        int listSize = list.size();
        for (int i = 0; i < listSize; i++) {
            ResolveInfo resolveInfo = list.get(i);
            result.add(new ListItem(mPackageManager, resolveInfo, null));
        }
        return result;
    }
}
关于LauncherActivity中mIntent的作用简述:
调用链
PackageManager->ApplicationPackageManager->PackageManagerService
 >> LauncherActivity.onQueryPackageManager(mIntent) -> PackageManager.queryIntentActivities(queryIntent, 0)
-> ApplicationPackageManager.queryIntentActivities(intent, flag)
-> ApplicationPackageManager.queryIntentActivitiesAsUser(intent, flag, userId)
-> PackageManagerService.queryIntentActivities(Intent intent, String resolvedType, int flags, int userId)
-> PackageManagerService.queryIntentActivitiesInternal(Intent intent, String resolvedType, int flags, int userId)
/**真正的调用在这里
  * 这里根据intent中传入数据在不同范围匹配查询
  * 比如有些通过schema、component启动其他进程的app时,intent是有数据的
  */
public class PackageManagerService extends IPackageManager.Stub {
	private @NonNull List queryIntentActivitiesInternal(Intent intent, String resolvedType, int flags, int userId) {
		if (!sUserManager.exists(userId)) return Collections.emptyList();
		flags = updateFlagsForResolve(flags, userId, intent);
		enforceCrossUserPermission(Binder.getCallingUid(), userId, false /* requireFullPermission */, false /* checkShell */, "query intent activities");
		ComponentName comp = intent.getComponent();
		if (comp == null) {//intent传入的component
			if (intent.getSelector() != null) {
				intent = intent.getSelector();
				comp = intent.getComponent();
			}
		}
		if (comp != null) {//根据intent传入的component返回需要查询的数据
			final List list = new ArrayList(1);
			final ActivityInfo ai = getActivityInfo(comp, flags, userId);
			if (ai != null) {
				final ResolveInfo ri = new ResolveInfo();
				ri.activityInfo = ai;
				list.add(ri);
			}
			return list;
		}
		//intent中没有传入component
		synchronized (mPackages) {
			/* intent未传入packageName,需要在整个系统范围内进行匹配查询
			 * intent有传入packageName则在指定pkg下匹配查询
			 */
			...
		}
		...
	}
}

参考资料


你可能感兴趣的:(笔记)