Some basic implementations of Android

Source: Internet
Author: User

1.NotificationBuild as Android updates have been changing, so the most up-to-date design method can refer to Google Docs.

1, notification update is generally not the same as the Activivity control update, but instead of rebuilding the same notification, and then use Notify (id,notification) to refresh the notification, as long as the ID will be updated, If not, create it.

2, such as players, fmradio, etc. need long-term background to run the outside can use Startforeground (id,notification), notification bar generally will not be recycled.

3, the use of SetStyle to design notification style and some action display: such as the following Google documents in a section of code:

Setshowactionsincompactview Design Displays the number of the action, the parameter is an int variable array, which displays the corresponding action event

Notification Notification = new Notification.builder (context)    //Show controls on lock screens Even when user hides Sen Sitive content:    Setvisibility (Notification.visibility_public)    . Setsmallicon (R.drawable.ic_stat_player)    //ADD Media Control buttons This invoke intents in your media service    . Addaction (R.drawable.ic_prev, "Previous", prevpendingintent)//#0    . Addaction (R.drawable.ic_pause, "pause", pausependingintent)  //#1    . Addaction ( R.drawable.ic_next, "Next", nextpendingintent)     //#2    //Apply The Media style template    . SetStyle (New Notification.mediastyle ()    . Setshowactionsincompactview (1/* #1: Pause button *    /). Setmediasession ( Mmediasession.getsessiontoken ())    . Setcontenttitle ("Wonderful music")    . Setcontenttext ("My Awesome Band")    . Setlargeicon (Albumartbitmap)    . Build ();
2, press the return button does not exit the application but the background run (overwrite the back key)

The key sentence is Movetasktoback (false);

@Override Public     boolean onKeyDown (int keycode, keyevent event) {         if (keycode = = keyevent.keycode_back) {             Movetasktoback (false);             return true;         }         Return Super.onkeydown (KeyCode, event);     }
3 ListView Settings Entry location, Setsetselectionfromtop invalid

1, invalid reason should be clicked on a button when the consumption of resources time-consuming operation, such as I encountered is to open a radio service caused the ListView interface automatically refreshed back to the top of the ListView, In order to solve this problem, it is not effective to use Setselectionfromtop to force the specified position in the item click event. The refresh of the ListView occurs after the service is started, and the start service is another thread, so even if setselectionfromtop is not valid after starting the service code, the service is still not open when running to Setselectionfromtop. The ListView does not refresh until it is turned on. Therefore, an asynchronous delay operation must be used:

Final int c = arg2;       Final int y = Arg1.gettop ();       Mstationlist.postdelayed (New Runnable () {           @Override public           void Run () {               Mstationlist.requestfocusfromtouch ();               int y =mstationlist.getheight ()/2-mstationlist.getheight ()/6;               Mstationlist.setselectionfromtop (c,y);           }       } , 150);  

2, Arg1.gettop () is the distance from the top of the ListView to get the current entry. Arg1 is an item in the ListView.

Some basic implementations of Android

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.