Android code segment 7 (II)

Source: Internet
Author: User

 

 

Statement

You are welcome to repost, but please keep the original source of the article :)
Blog: http://www.cnblogs.com

Farmer's uncle: http://over140.cnblogs.com

 

Body

1. obtain all the activities under the Application

Public static ArrayList <String> getActivities (Context ctx ){
ArrayList <String> result = new ArrayList <String> ();
Intent intent = new Intent (Intent. ACTION_MAIN, null );
Intent. setPackage (ctx. getPackageName ());
For (ResolveInfo info: ctx. getPackageManager (). queryIntentActivities (intent, 0 )){
Result. add (info. activityInfo. name );
}
Return result;
}

 

2. Check whether the string contains Chinese Characters

Public static boolean checkChinese (String sequence ){
Final String format = "[\ u4E00-\ u9FA5 \ uF900-\ uFA2D]";
Boolean result = false;
Pattern pattern = Pattern. compile (format );
Matcher matcher = pattern. matcher (sequence );
Result = matcher. find ();
Return result;
}

 

3. Check strings can only contain Chinese characters, numbers, underscores (_), and hyphens (-)

Public static boolean checkNickname (String sequence ){
Final String format = "[^ \ u4E00-\ u9FA5 \ uF900-\ uFA2D \ w-_]";
Pattern pattern = Pattern. compile (format );
Matcher matcher = pattern. matcher (sequence );
Return! Matcher. find ();
}

 

4. check whether there are applications to accept and process your intent

    public static boolean isIntentAvailable(Context context, String action) {        final PackageManager packageManager = context.getPackageManager();        final Intent intent = new Intent(action);        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);        return list.size() > 0;    }

 

5. Use TransitionDrawable to achieve gradient effect

Private void setImageBitmap (ImageView imageView, Bitmap bitmap ){
// Use TransitionDrawable to fade in.
Final TransitionDrawable td = new TransitionDrawable (new Drawable [] {new ColorDrawable (android. R. color. transparent), new BitmapDrawable (mContext. getResources (), bitmap )});
// Noinspection deprecation
ImageView. setBackgroundDrawable (imageView. getDrawable ());
ImageView. setImageDrawable (td );
Td. startTransition (200 );
}

It is better than AlphaAnimation to avoid flickering issues.

 

6. Scan specified files

SendBroadcast (new Intent (Intent. ACTION_MEDIA_SCANNER_SCAN_FILE, uri ));

Purpose: This function is used to add, modify, and delete an image or a file (audio or video) from the software to update the system media library without scanning the entire SD card.

 

7. Convert Dip to px

Public static int dipToPX (final Context ctx, float dip ){
Return (int) TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, dip, ctx. getResources (). getDisplayMetrics ());
}

Purpose: it is inevitable to set the position and size in the Activity code. This method is useful!

 

Related Article

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.