Seven Practical Android code segments (5) and seven practical android code segments
Preface
Each sharing means that every time there is progress, this series is mainly practical, welcome to share with me and recommend useful code segments ~~
StatementWelcome to repost, but please keep the original source of the article :) blog Park: http://www.cnblogs.com farmer UNCLE: http://over140.cnblogs.com
Body
1. Expand and collapse the status bar
Public static final void collapseStatusBar (Context ctx ){
Object sbservice = ctx. getSystemService ("statusbar ");
Try {
Class <?> StatusBarManager = Class. forName ("android. app. StatusBarManager ");
Method collapse;
If (Build. VERSION. SDK_INT> = Build. VERSION_CODES.JELLY_BEAN_MR1 ){
Collapse = statusBarManager. getMethod ("collapsePanels ");
} Else {
Collapse = statusBarManager. getMethod ("collapse ");
}
Collapse. invoke (sbservice );
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public static final void expandStatusBar (Context ctx ){
Object sbservice = ctx. getSystemService ("statusbar ");
Try {
Class <?> StatusBarManager = Class. forName ("android. app. StatusBarManager ");
Method expand;
If (Build. VERSION. SDK_INT> = 17 ){
Expand = statusBarManager. getMethod ("expandNotificationsPanel ");
} Else {
Expand = statusBarManager. getMethod ("expand ");
}
Expand. invoke (sbservice );
} Catch (Exception e ){
E. printStackTrace ();
}
}
Purpose: Click Notifacation to collapse the status bar.
2. Get the Status Bar Height
Public static int getStatusBarHeight (Context context ){
Class <?> C = null;
Object obj = null;
Field field = null;
Int x = 0, statusBarHeight = 0;
Try {
C = Class. forName ("com. android. internal. R$ dimen ");
Obj = c. newInstance ();
Field = c. getField ("status_bar_height ");
X = Integer. parseInt (field. get (obj). toString ());
StatusBarHeight = context. getResources (). getDimensionPixelSize (x );
} Catch (Exception e1 ){
E1.printStackTrace ();
}
Return statusBarHeight;
}
3. Simple ViewHolder writing for ListView
Public static <T extends View> T getAdapterView (View convertView, int id ){
SparseArray <View> viewHolder = (SparseArray <View>) convertView. getTag ();
If (viewHolder = null ){
ViewHolder = new SparseArray <View> ();
ConvertView. setTag (viewHolder );
}
View childView = viewHolder. get (id );
If (childView = null ){
ChildView = convertView. findViewById (id );
ViewHolder. put (id, childView );
}
Return (T) childView;
}
Usage:
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
If (convertView = null ){
ConvertView = LayoutInflater. from (getActivity (). inflate (R. layout. fragment_feed_item, parent, false );
}
ImageView thumnailView = getAdapterView (convertView, R. id. video_thumbnail );
ImageView avatarView = getAdapterView (convertView, R. id. user_avatar );
ImageView appIconView = getAdapterView (convertView, R. id. app_icon );
It is very concise to use, and the ViewHolder is invisible.
4. Set Activity transparency
<Style name = "TransparentActivity" parent = "AppBaseTheme">
<Item name = "android: windowBackground"> @ android: color/transparent </item>
<Item name = "android: colorBackgroundCacheHint"> @ null </item>
<Item name = "android: javaswistranslucent"> true </item>
<Item name = "android: windowNoTitle"> true </item>
<Item name = "android: windowContentOverlay"> @ null </item>
</Style>
Note: AppBaseTheme is generally the android: theme specified by your application. What is theme here? Otherwise, the Internal Spatial styles of the Activity may be different.
Purpose: it is used to simulate the Dialog effect. For example, if the Dialog cannot be used in the Service, the Activity can be used to simulate the Dialog effect.
5. Code switching full screen
// Switch to full screen
GetWindow (). clearFlags (WindowManager. LayoutParams. FLAG_FORCE_NOT_FULLSCREEN );
GetActivity (). getWindow (). addFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN );
// Switch to a non-full screen
GetWindow (). clearFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN );
GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_FORCE_NOT_FULLSCREEN );
Note: When switching to full screen, the virtual buttons at the bottom are still displayed. The method can be called multiple times for switching
Purpose: The player interface is often used.
6. Call the Touch Location display function in the developer options.
Android. provider. Settings. System. putInt (getContentResolver (), "show_touches", 1 );
Set 1 to display, and set 0 to not display.
7. Obtain the list of applications that have been installed and can be started on the device.
Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. addCategory (Intent. CATEGORY_LAUNCHER );
List <ResolveInfo> activities = getPackageManager (). queryIntentActivities (intent, 0)
Note: Using getInstalledApplications will return many system applications that cannot be started or even have no icons. ResolveInfo. activityInfo. applicationInfo can also obtain the data you want.
Series
Android code segment 7 (1)
Android code segment 7 (II)
Android code segment 7 (3)
Android code segment 7 (4)
Note: On the Android phone, I used a software named "Notepad" to edit a batch of processing code (correct code) and
Windows notepad provides four types of character encoding
ANSI (GBK in Chinese)
Unicode
Unicode BE
UTF-8
You can use one of the four types to write code on your mobile phone.
Explanation of a code segment of the android Calculator
When you press the button, if the input string is not blank, convert the input string to a number (floating point number)
Calculate the square root of the variable (A * A = A square), convert it to the string type, and specify the return to the variable str.
Then, specify the calculated square root to the Text of et.
Reference: yourself