您好,登錄后才能下訂單哦!
四大組件相關(guān):
1.啟動一個(gè)Activity,在應(yīng)用進(jìn)程至少需要兩個(gè)Binder線程。
2.啟動一個(gè)launchMode為singleTask的Activity,它并不一定會運(yùn)行在新的Activity棧中。
3.兩個(gè)不同應(yīng)用的Activity,可以運(yùn)行在同一個(gè)Activity棧中。
4.同一個(gè)應(yīng)用進(jìn)程中的所有Activity,共享一個(gè)WindowSession。
5.彈出一個(gè)AlertDialog,不一定需要Activity級別的Context,而且任何地方都有辦法彈出一個(gè)AlertDialog,只要是在Application的attachBaseContext之后。
下面是一個(gè)簡單的demo演示:
首先看DemoApplication,然后看Alert類:
在Application中初始化:
import android.app.Application;public class DemoApplication extends Application { @Override public void onCreate() { Alert.alertAnyWhere(); super.onCreate(); }}
下面這個(gè)類是對AlertDialog的封裝類:
import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Build;import android.os.Handler;import android.os.Looper;import android.view.WindowManager;import java.lang.reflect.Method;public class Alert { public static void alertDialog() { Context mAppContext = null; try { Class<?> clazz = Class.forName("android.app.ActivityThread"); Method method = clazz.getDeclaredMethod("currentApplication", new Class[0]); mAppContext = (Context) method.invoke(null, new Object[0]); } catch (Throwable e) { e.printStackTrace(); return; } AlertDialog.Builder builder = new AlertDialog.Builder(mAppContext); builder.setTitle("Hi") .setMessage("Hello World"); .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog = builder.create(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST); } else { dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE); } dialog.show(); } private static Handler handler; public static void alertAnyWhere() { if (Looper.myLooper() == Looper.getMainLooper()) { alertDialog(); } else { if (handler == null) { handler = new Handler(Looper.getMainLooper()); } handler.post(new Runnable() { @Override public void run() { alertDialog(); } }); } }}
6.可以通過設(shè)置Activity主題android.R.style.Theme_NoDisplay,來啟動一個(gè)不顯示的Activity,在某些需要過渡的地方很實(shí)用。
7.Activity、Service、Receiver在沒有配置intent-filter的action屬性時(shí),exported默認(rèn)為false,配置了intent-filter的action屬性時(shí),exported默認(rèn)為true。稍有不慎,很可能埋下越權(quán)、Intent等安全隱患。
8.當(dāng)從最近使用應(yīng)用列表中移除某個(gè)App時(shí),四大組件只有Service擁有神奇的onTaskRemoved回調(diào),但是并不一定回調(diào),還與stopWithTask屬性等有關(guān)。
9.四大組件都運(yùn)行在主線程,是因?yàn)樗鼈冊贏ctityThread中(或Instrumentation)實(shí)例化;它們的生命周期也運(yùn)行在主線程,是因?yàn)橥ㄟ^ActivityThread.H將消息從Binder線程發(fā)送到主線程,然后執(zhí)行回調(diào)。
10.TaskStackBuilder的出現(xiàn)基本上解決了所有構(gòu)造Activity回退棧的問題。
11.ContentProvider的onCreate()方法先于Application的onCreate()方法執(zhí)行,晚于Application的attachBaseContext()方法,所以在ContentProvider的onCreate()時(shí)候也是有辦法彈出一個(gè)AlertDialog的(參考5)。
12.BroadCastReceiver回調(diào)onReceive(Context context,Intent intent)中的context類型各種場景相差很大,靜態(tài)注冊的receiver回調(diào)的Context都是ReceiverRestrictedContext,動態(tài)注冊的receiver有可能是Activity或Application。
13.ServiceRecord和BroadcastRecord自身就是Binder。
14.同一個(gè)provider組件名,可能對應(yīng)多個(gè)provider。
1.MessageQueue.addIdleHandler可以用來在線程空閑的時(shí)候,完成某些操作,比較適合那種需要在將來執(zhí)行操作,卻又不知道需要指定多少延遲時(shí)間的操作。
2.Message.what盡量不要設(shè)置成0,因?yàn)閜ostRunnable的方式會生成Message.what為0的消息,如果刪除了what為0的Message,也會將runnable方式創(chuàng)建的Message刪掉。
3.Handler可以設(shè)置同步異步(默認(rèn)是同步的),他們的區(qū)別在于異步不會被Barrier阻塞,而同步會被阻塞。
4.Handler的消息分發(fā)流程是如果Message的callback不為空,通過callback處理,如果Handler的mCallback不為空,通過mCallback來處理,如果前兩個(gè)都為空,才調(diào)用handleMessage來處理。在DroidPlugin中,便是利用ActivityThread.H的這一特性,攔截了部分消息,實(shí)現(xiàn)Activity的插件化。
5.Java層和Native層Looper、MessageQueue的創(chuàng)建時(shí)序,Java層Looper—>Java層MessageQueue—>Native層NativeMessageQueue—>Native層Looper。
6.Java層通過Handler去發(fā)送消息,而Native層是通過Looper發(fā)消息。
1.硬件加速在Window級只能開不能關(guān),View級只能關(guān)不能開。
2.自android2.3刪除MidWindow后,PhoneWindow成了Window的唯一實(shí)現(xiàn)類。
3.WMS管理Window的過程中涉及4個(gè)Binder,應(yīng)用進(jìn)程只有ViewRootImpl.W一個(gè)Binder服務(wù)端。
4.MotionEvent、KeyEvent、DragEvent等具有相似的鏈?zhǔn)骄彺?,類似Message。
5.在View的狀態(tài)保存、恢復(fù)過程中,ActionBar中所有View共享一個(gè)SparseArray容器,ContentView中所有View共享一個(gè)SparseArray容器。當(dāng)前獲取焦點(diǎn)的View會額外存儲。
6.設(shè)置ViewTreeObserver的系列監(jiān)聽方法需要確保View在attachToWindow之后,否則可能因?yàn)閍dd監(jiān)聽和remove監(jiān)聽不是作用于同一個(gè)對象而引起內(nèi)存泄漏等。
1.可以通過文件鎖來實(shí)現(xiàn)進(jìn)程間互斥(參考:RePlugin),在處理某些只需要單進(jìn)程執(zhí)行的任務(wù)時(shí)很實(shí)用。
2.Binder設(shè)計(jì)架構(gòu)中,只有Binder主線程是由本進(jìn)程主動創(chuàng)建,Binder普通線程都是由Binder驅(qū)動根據(jù)IPC通信需求被動創(chuàng)建。
3.oneway與非oneway,都需要等待Binder Driver的回應(yīng)消息(BR_TRANSACTION_COMPLETE),區(qū)別在于oneway不用等待BR_REPLY消息。
4.mediaserver和servicemanager的主線程都是binder線程,但system_server的主線程不是Binder線程,system_server主線程的玩法跟應(yīng)用進(jìn)程一樣。
5.同一個(gè)BpBinder可以注冊多個(gè)死亡回調(diào),但Kernel只允許注冊一次死亡通知。
6.應(yīng)用進(jìn)程由Zygote進(jìn)程孵化而來,在它真正成為應(yīng)用進(jìn)程之前,系統(tǒng)通過拋異常的方式來清理?xiàng)?,并反射調(diào)用ActivityThread的main方法。
7.在Binder通信的過程中,數(shù)據(jù)是從發(fā)起通信進(jìn)程的用戶空間直接寫到目標(biāo)進(jìn)程內(nèi)核空間,內(nèi)核空間的數(shù)據(jù)釋放是由用戶空間控制的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。