Common code snippets for Android Development

Source: Internet
Author: User
1. Set the window format to translucent getwindow (). setformat (pixelformat. translucent); 2. Different Methods for updating the view in a non-UI thread in Android: * activity. runonuithread (runnable) * view. post (runnable) * view. postdelayed (runnable, long) * hanlder3. The requestwindowfeature (window. feature_no_title); getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); 4. Method A: windowmanager = getw Indowmanager (); display = windowmanager. getdefadisplay display (); handw [0] = display. getwidth (); handw [1] = display. getheight (); Method B: displaymetrics dm = new displaymetrics (); getwindowmanager (). getdefadisplay display (). getmetrics (DM); handw [0] = DM. widthpixels; handw [1] = DM. heightpixels; 5. Call the browser to load the URL uri = Uri. parse (""); intent it = new intent (intent. action_view, Uri); startactivity (it ); 6. Get the memory size activitymanager. memoryinfo outinfo = new activitymanager. memoryinfo (); activitymanager. getmemoryinfo (outinfo); // available memory outinfo. availmem // whether it is in the low memory status outinfo. lowmemory gets the actual height of the scrollview. getheight () scrollview. getmeasuredheight () scrollview. compute () scrollview. getlayoutparams (). height7. Listening for app installation/uninstall events. define a class derived from class broadcastreceiver; B. register broadcast receiver er; mybroadcas Treceiver myreceiver = new mybroadcastreceiver (); intentfilter filter = new intentfilter (intent. action_package_install); filter. addaction (intent. action_package_removed); filter. addaction (intent. action_package_added); filter. addaction (intent. action_package_changed); filter. addaction (intent. action_package_restarted );... filter. adddatascheme ("package"); // This line is very important. otherwise, broadcast C An't be written ed. registerreceiver (myreceiver, filter); Notes: The package name is intent. mdata. intent. mdata is not available in sdks 1.0, but it can be retrieved by calling intent. getdatastring (); 8. Obtain the IP address. // connect via wifi via wifiwifimanager wifimanager = (wifimanager) getsystemservice (wifi_service); wifiinfo = wifimanager. getconnectioninfo (); int IPaddress = wifiinfo. getipaddress (); B. // con Nect via GPRS through maid string getlocalipaddress () {try {for (enumeration <networkinterface> en = networkinterface. getnetworkinterfaces (); en. hasmoreelements ();) {networkinterface INTF = en. nextelement (); For (enumeration <inetaddress> enumipaddr = INTF. getinetaddresses (); enumipaddr. hasmoreelements ();) {inetaddress = enumipaddr. nextelement (); If (! Inetaddress. isloopbackaddress () {return inetaddress. gethostaddress (). tostring () ;}}} catch (socketexception ex) {log. E (S. tag, Ex. tostring ();} return NULL;} 9. The adapter data behind listview has been changed, but listview does not receive notification. First, you must update Code Put in: Handler. post (runnable) method; then, if the source of the adapter data is cursor (cursoradapter), you can cursor. requery, if it is another one, you can force call notifychange, notifychange will call invalidate for re-painting; 10. Simulate the Home Key intent I = new intent (intent. action_main); I. addcategory (intent. category_home); I. addflags (intent. flag_activity_new_task); context. startactivity (I); 11. Set the focus edittext. setfocusable (true); edittext. requestfocus (); edittext. setfocusableintouchmode (true );
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.