android_快速上手.pdf
81页Android: Quick Start Dmitry Lukashev What is the plan? •History foo.hello(); // With reflection Class cls = Class.forName(“Foo“); Object foo = cls.newInstance(); Method method = cls.getMethod(“hello“, null); method.invoke(foo, null); Android Architecture Overview Linux Kernel Display Driver Keypad Driver Camera Driver WiFi Driver Flash Memory Driver Audio Drivers Binder (IPC) Driver Power Management Libraries Surface Manager Media Framework OpenGL | ES FreeType SQLite WebKit SGL SSL libc Android Runtime Core Libraries Dalvik VM Application Framework Activity Manager Package Manager Window Manager Telephony Manager Content Providers Notification Manager View System Location Manager Resource Manager Applications Home Contacts Phone Browser … NDK / JNI JVM The pc register (program counter) JVM Stack Native methods stacks Heap Method Area VM Thread Сlass Runtime Constant Pool class files Class loader subsystem native method libraries Execution engine Native method interface Dalvik VM • Was written by Dan Bornstein • Transform class files into DEX • It is VM… – integrated with Linux – uses shared memory, mmap – for OS without swap space – while powered by a battery – zygote • The Dalvik VM is register-based: fewer instructions, code units, instructions • Verification editor.putType(String key, T value); mit(); Activity.getPreferences() PreferenceManager.getDefaultSharedPreferences(Context ctx) Context.getSharedPreferences(String name, int mode) mPreferences.getType(String key, T defValue); Backup Application Data (android.app.backup – 2.2) •Perform backup arbitrary data to remote “cloud” storage •Easily perform backup of SharedPreferences and files •Restore the data saved to remote storage •Controlled by Android Backup Manager – Extend class BackupAgent and override onBackup() intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); NEW_TASK Task Activity 3 affinity=com.gtug.task2 BACK HOME taskAffinity=com.gtug.task2 Task reparenting android:allowTaskReparenting=[“true“ | “false“] Task Activity 1 Activity 2 allowTaskReparenting=true affinity=com.gtug.task2 Intent w/o NEW_TASK taskAffinity=com.gtug.task1 No Task with affinity=com.gtug.task2 Task Activity 1 Intent w/o NEW_TASK taskAffinity=com.gtug.task1 Task Activity 2 allowTaskReparenting=true affinity=com.gtug.task2 taskAffinity=com.gtug.task2 Activity 2 View Activity/Task/Process details • adb shell dumpsys dump.txt Activities in Current Activity Manager State: * TaskRecord{43ddd1e8 #10 A com.test.xxx} clearOnBackground=false numActivities=1 rootWasReset=false affinity=com.test.xxx intent={flg=0x10000000 cmp=com.test.TestUI/.Activity1} realActivity=com.test.TestUI/.Activity1 lastActiveTime=15088595 (inactive for 5s) * Hist #4: HistoryRecord{43ddd000 com.test.TestUI/.Activity1} packageName=com.test.TestUI processName=com.gtug.test123123 launchedFromUid=10025 app=ProcessRecord{43d5a1a8 293:com.gtug.test123123/10025} Intent { flg=0x10000000 cmp=com.test.TestUI/.Activity1 } frontOfTask=true task=TaskRecord{43ddd1e8 #10 A com.test.xxx} taskAffinity=com.test.xxx realActivity=com.test.TestUI/.Activity1 base=/data/app/com.test.TestUI.apk/data/app/com.test.TestUI.apk data=/data/data/com.test.TestUI labelRes=0x7f040001 icon=0x7f020000 theme=0x0 stateNotNeeded=false componentSpecified=true isHomeActivity=false configuration={ scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=18} launchFailed=false haveState=false icicle=null state=RESUMED stopped=false delayedResume=false finishing=false keysPaused=false inHistory=true persistent=false launchMode=0 fullscreen=true visible=true frozenBeforeDestroy=false thumbnailNeeded=false idle=true Activity launch modes (1) standard android:launchMode=[“standard“ | “singleTop“ | “singleTask“ | “singleInstance“] singleTop singleInstance singleTask Multiple instances in one Task Single instance in one Task only one Activity in the Task Activity launch modes (2) Task Activity 1 standard Task Activity 1 Activity 2 Task Activity 1 Activity 2 Activity 3 Task Activity 1 Activity 2 Activity 3 Activity 2 singleTop Task Activity 1 Activity 2 Task Activity 1 Activity 2 Activity 1 Task Activity 1 Activity 2 Activity 1 Activity1 is singleTop onNewIntent() run Activity1 Activity launch modes (3) singleTask Task Activity 1 Activity 2 Activity 3 Activity1 is singleTask Task Activity 1 run Activity1 singleInstance Activity1 is singleInstance Task1 Activity 1 no more Activities in this task Task2 Activity 2 run Activity2 Activity Graph of Life Start Stop onCreate() onStart() onResume() onRestart() onDestroy() onPause() onStop() Kill process Running partly visible foreground no longer visible foreground onPostCreate () onPostResume() Fragments Android • Architecture Overview findViewById(R.id.my_button); Layout Parameters • layout_something – layout parameter • Every ViewGroup class implements a nested class that extends ViewGroup.LayoutParams • layout_width and layout_height are required for all v。





