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!