Android learning notes (4) and android learning notes
2. settings. system. the getInt (ContentResolvercr, String name, int def) method extracts the value of the parameter named name from the set of system settings parameters. If this parameter is not set in the system, the default value def is returned.
3. The setVolumeControlStream (intstreamType) function of the Activity is used to set the audio stream controlled by the volume control key in the Activity, which is generally set in the onCreate () function. Among them, streamType is the type to adjust the volume, there are the following types:
● AudioManager. STREAM_MUSIC media volume, that is, audio and video volume
● AudioManager. STREAM_RING
● AudioManager. STREAM_ALARM mobile alarm volume
● AudioManager. STREAM_NOTIFICATION the number of sound notifications in the status bar at the top of the window
● AudioManager. STREAM_SYSTEM system sound volume
● AudioManager. STREAM_VOICECALL call sound volume
● AudioManager. STREAM_DTMF dual-tone dual-band volume
4. Functions of public Intent registerReceiver (BroadcastReceiverreceiver, IntentFilter filter:
Resgister aBroadcastRecevier to be run in the main activity thread. The Handler er will becalled with any broadcast Intent that matches the filter in the mainapplication thread.
5. Use the boolean android. app. Activity. isFinishing () function:
Check to seewhether this activity is in the process of finishing, either because you calledfinish () on it or someone else has requestedthat it finished. this is often used inonPause () to determine whether the activity is simply pausing or completely finishing. ifthe activity is finishing, returns true; else return false.
6. Introduction to the java. lang. ref. WeakReference class
When writing a Java program, you want to obtain information about an object at any time. For example, when the object will be reclaimed by the garbage collection mechanism, you can use Weak Reference, but you cannot use Reference. If you use Reference, the object cannot be cleared. However, Weak Reference can be used to obtain the object information at any time, but does not affect the garbage collection of the object. Example:
A obj = newA ();
WeakReferencewr = new WeakReference (obj );
Obj = null;
...
// Wait for a while and the obj object will be reclaimed
If (wr. get () = null ){
System. out. println ("obj cleared ");
} Else {
System. out. println ("obj not cleared ");
}
Get the object referred to by WeakReference using the get () method.
7. The View. setVisibility () parameter has three different values:
● View. VISIBLE this View is VISIBLE
● View. INVISIBLE the View is INVISIBLE, but the View still retains its position in the ViewGroup and does not layout it again.
● View. GONE: this View is invisible, but this view does not retain its position in the ViewGroup. Re-layout, and the subsequent View will replace its position.
8. The android: cacheColorHint attribute and android: listSelector attribute of ListView
● The Default background of ListView is the same transparent color as that of the System window. if you add a slice or background color to the ListView, The ListView will be black when scrolling,
Because, when the view in the list is re-painted during scrolling, the system still uses the default transparent color, and the color value is # ff19191919. To change this situation,
You only need to call the setCacheColorHint (0) of ListView, set the color value to 0, or set the attribute android: cacheColorHint = of ListView in the xml file.
#00000000 ". In this way, the background color is not displayed when the View is re-painted.
● When you click "I" for "ListView", the selected item is displayed as orange by default. To remove this effect, you can use the property android: listSelector. If it is set to android: listSelector = "#00000000" or android: listSelector =
"@ Android: color/transparent" means that when you click the item in the ListView, there will be no problem.
9. Overview of android unit px, dip (dp), sp, dpi
● Px (pixels): The actual pixel unit on the screen. This unit is generally not used.
● Dip/dp (device independent pixels): independent pixel of the device, independent of the device Screen
● Sp (scaled pixels): similar to dp, it is mainly used to process the font size.
● Dpi (dot per inch): screen pixel density, number of pixels per inch. The larger the value, the higher the fineness of the screen, the clearer the screen.
10. @ Override usage
@ Override is a pseudo-code that indicates rewriting (you can do it without writing it). However, writing it has the following benefits:
● It can be used as a comment for easy reading
● The compiler can verify whether the method name under @ Override is in your parent class. If no, an error is returned.
11. TouchEvent processing mechanism of the android system: each View subclass in the android system has three closely related to TouchEvent processing:
Method:
● Public booleandispatchTouchEvent (MotionEvent ev): used to distribute TouchEvent. This MotionEvent records the coordinates of the trigger point.
● Public booleanonInterceptTouchEvent (MotionEvent ev): Used to intercept TouchEvent
● Public booleanonTouchEvent (MotionEvent ev): used to handle TouchEvent events.
When a TouchEvent occurs, the Activity first passes the TouchEvent to the top-level View. The TouchEvent first reaches the dispatchTouchEvent of the top-level view, and then the dispatchTouchEvent distributes the TouchEvent. If the dispatchTouchEvent returns true, the event is handled by onTouchEvent of the view. If dispatchTouchEvent returns false, the interceptTouchEvent method of the view is used to determine whether to intercept the event. If the interceptTouchEvent returns true, it is handed over to its onTouchEvent for processing. If false is returned, it is passed to the subview, where the dispatchTouchEvent of the subview starts distributing the event. If the event is transmitted to the onTouchEvent of a sub-view at a certain layer, the method returns false, and the event is passed up from this view, which is received by onTouchEvent. If false is returned when the onTouchEvent is passed to the top, the event disappears and the next event cannot be received.
12.Activity. finish () Introduction
This method is called after the Activity is completed or when the Activity needs to be closed. When this method is used, the system removes the top Activity from the Task stack, but does not call the onDestory () method in time, so the resources occupied by the method are not released in time. Because the stack is removed, the Activity will not be found when you click the "back" button on the phone.
13.Differences between AndroidWebViewClient and WebChromeClient
● WebViewClient: Mainly used to help WebView process various notifications and request events. For example: onLoadResource, onPageStart, onPageFinish, onReceiveError, onReceivedHttpAuthRequest
● WebChromeClient: Mainly used to assist WebView in processing Javascript dialogs, website titles, and loading progress. For example, onCloseWindow (disable WebView), onCreateWindow, onJsAlert (alert on WebView is invalid, and WebChromeClient processing needs to be customized), response, onJsConfirm, onProgressChanged, onReceivedIcon, onReceivedTitle, and.
In actual use, if WebView is only used to process html page content, only WebViewClient is used.
Effect, such as JS and progress bar, must use WebChromeClient.
14.WeakReference class
WeakReference weakref = new WeakReference (ref );
In this way, weakref is a weakreference that the ref points to the object. You can use the get method to reference the objects pointed to by this weakreference. Put the weak reference of an object into Hashtable or cache. When no strong reference points to them, the object can be recycled by the garbage collector. In fact, there is a WeakHashMap dedicated to doing this. When WeakReference returns null using the get method, the object it points to has become garbage, and this weakref object is useless. This requires some cleanup work. The ReferenceQueue class does this. If you pass a WeakReference constructor to the ReferenceQueue class, when the referenced object becomes garbage, the referenced object is automatically inserted into the reference queue. You can process this queue within a certain interval.
15.ViewStub component
The ViewStub component is a hidden view object that does not occupy memory space. It can load layout resource files at runtime delay. The layout resource file is loaded only when ViewStub is visible or the inflate () function is called. This ViewStub replaces itself in the parent container when loading the view. Therefore, ViewStub will remain in the view until setVisibility (int) or inflate () is called. The layout parameters of ViewStub are added to the parent container of ViewStub along with the number of loaded views. Similarly, you can use the inflatedId attribute to define or rename the Id value of the view object to be loaded. When inflate () is called, The ViewStub is replaced by the loaded view and the view object is returned. In this way, the application does not need to execute findViewById () to obtain reference for loading views.
As follows:
1) there is a ViewStub layout between ButtonOne and ButtonTwo, such:
2) Click ButtonOne to render the layout in ViewStub, for example: