Android Utility Code Seven paragraph (v)

Source: Internet
Author: User

Objective

Every time sharing means progress every time, this series to be practical, welcome and I share and recommend useful code Snippets ~ ~

StatementWelcome reprint, 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 close 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: Can be used to close the status bar after clicking Notifacation

2. Get 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, the ListView uses the Viewholder extremely shorthand method

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);

use very concise, will viewholder 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:windowistranslucent" >true</item>
<item name= "Android:windownotitle" >true</item>
<item name= "Android:windowcontentoverlay" > @null </item>
</style>

Description: Appbasetheme General is your application designated Android:theme is what here is what, otherwise activity internal space style may be inconsistent.

Use: Used to simulate dialog effects, such as the service can not be used dialog, it is possible to use activity to simulate

5. Code switch fullscreen

//Switch to full screen
GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
Getactivity (). GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_FULLSCREEN);

Switch to non-fullscreen
GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_FULLSCREEN);
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

Note: When you switch to full screen, the virtual keys at the bottom are still displayed. The secondary method can be called multiple times for switching

Purpose: The player interface is often used

6. Call the developer option to display the Touch location feature

android.provider.Settings.System.putInt (Getcontentresolver (), "Show_touches", 1);

Set 1 to display, setting 0 is not displayed.

7. Get a list of installed and bootable apps on your 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 apps that cannot be started or even have no icons. ResolveInfo.activityInfo.applicationInfo can also fetch the data you want.

Android Utility Code Seven paragraph (v)

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.