您的位置:首页 > 编程语言 > Java开发

Launcher3源码浅析(5.1)--Launcher.java

2016-04-16 15:58 471 查看

目录

前言

初始化

启动App/Widget

前言

  第一次写博客,先来点废话唠叨唠叨。博客这玩意儿,还在上大学的时候老师就建议写一写了,可惜比较懒,一直没有动力去写。然后工作了两三年,现在突然发现以前学过和写过的那些JNI、FFMPEG、OPENGL等,久了不用都快忘光了,有新人问到都不好意思说做过了。所以,想想还是写个博客,做点记录吧,忘了还可以回头翻翻,至少证明我曾经真的做过这么些东西。

  最近一年基本都在搞系统应用开发,改最多的就是Launcher3,改改布局、换换风格、修修bug,所以就写写Launcher3的一些东西吧,计划是分几个模块来写,具体再看情况吧。其实没有太多的改动,研究的也不够深入,所以有什么写错的地方望大牛指出,新人也不要太相信了,以免误人子弟。

  先大概写写Launcher.java这个类吧。Launcher.java是Launcher里面非常主要的一个类,可以说是Launcher的入口吧。Launcher.java的代码量很多,而且跟各个模块各种交互,具体就不一一分析了,就简单说一下相对独立的功能吧,跟其他模块相关的在其他模块再看了。

初始化

Launcher.java是一个Activity,所以从onCreate()开始分析。

protected void onCreate(Bundle savedInstanceState) {
.....             
LauncherAppState.setApplicationContext(
    getApplicationContext());
 LauncherAppState app = LauncherAppState.getInstance();                 
 LauncherAppState.getLauncherProvider().
     setLauncherProviderChangeListener(this);

// Lazy-initialize the dynamic grid
DeviceProfile grid = app.initDynamicGrid(this);
// the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
mSharedPrefs = getSharedPreferences(LauncherAppState.
  getSharedPreferencesKey(),Context.MODE_PRIVATE);
mIsSafeModeEnabled =getPackageManager().isSafeMode();
mModel = app.setLauncher(this);
mIconCache = app.getIconCache();
mIconCache.flushInvalidIcons(grid);
mDragController = new DragController(this);
mInflater = getLayoutInflater();

mStats = new Stats(this);
mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);

mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
mAppWidgetHost.startListening();

// If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
// this also ensures that any synchronous binding below doesn't re-trigger another
// LauncherModel load.
mPaused = false;

if (PROFILE_STARTUP) {
android.os.Debug.startMethodTracing(
Environment.getExternalStorageDirectory() + "/launcher");
}

checkForLocaleChange();
setContentView(R.layout.launcher);

setupViews();
grid.layout(this);
......
}


  首先是初始化了LauncherAppState对象app,LauncherAppState用的是单例模式,里面初始化了LauncherModel和IconCache等对象,并且注册了广播监听器和ContentObserver。然后通过对象app又初始化一些对象。

设置布局launcher.xml,调用setupViews又是一些View的初始化,设置回调监听等。

  总之,这里面就是一些初始化的操作。

启动App/Widget

  当点击应用或者插件的图标时,会启动对应应用或插件,这一功能就是在Launcher.java里实现的。监听onClick事件,然后根据View的不同类型进行不同处理。总之,启动应用和启动插件这两个操作最终都是调用startActivitySafely方法实现启动。

  下面看看startActivitySafely的实现:

boolean startActivitySafely(View v, Intent intent, Object tag) {
boolean success = false;
if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
return false;
}
try {
success = startActivity(v, intent, tag);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
}
return success;
}


  如果mIsSafeModeEnabled为true,即在安全模式下,且当前要启动的应用不是系统允许的应用,则返回false,即不给启动。

  不在上面的情况下,那就调用startActivity方法来启动,至于startActivity到底是怎么启动的,那得跟进到framework里去看了,这里就不深入了。

博客搬家:http://www.mosiliang.top/?p=174

以后更新都在自个博客上了,有兴趣的可以关注看看,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: