实用的七个程序段
来源:互联网 发布:网络挣钱项目 编辑:程序博客网 时间:2024/05/29 14:02
声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com
农民伯伯: http://over140.cnblogs.com
正文
一、获取应用程序下所有Activity
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;
}
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;
}
二、检测字符串中是否包含汉字
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;
}
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;
}
三、检测字符串中只能包含:中文、数字、下划线(_)、横线(-)
public staticboolean checkNickname(String sequence) {
final String format = "[^\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w-_]";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(sequence);
return !matcher.find();
}
final String format = "[^\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w-_]";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(sequence);
return !matcher.find();
}
四、检查有没有应用程序来接受处理你发出的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; }
五、使用TransitionDrawable实现渐变效果
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);
}
// 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);
}
比使用AlphaAnimation效果要好,可避免出现闪烁问题。
六、 扫描指定的文件
sendBroadcast( new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
用途:从本软件新增、修改、删除图片、文件某一个文件(音频、视频)需要更新系统媒体库时使用,不必扫描整个SD卡。
七、Dip转px
public staticint dipToPX( final Context ctx,float dip) {
return ( int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, ctx.getResources().getDisplayMetrics());
}
return ( int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, ctx.getResources().getDisplayMetrics());
}
用途:难免在Activity代码中设置位置、大小等,本方法就很有用了!
- 实用的七个程序段
- 实用的七个程序段
- 提升JSP应用程序的七个实用方法
- 提升JSP应用程序的七个实用方法
- 几段实用小JAVA程序
- JavaScript实用的代码段
- 管理者必须掌握的的七个实用管理工具
- WebEx 创始人朱敏做企业家的七个理论(非常实用)
- WebEx 创始人朱敏做企业家的七个理论(非常实用)
- WebEx 创始人朱敏做企业家的七个理论(非常实用)
- 开放源代码开发七个最实用的开源系统永远
- 程序段的前缀
- 可执行程序的段
- .NET 程序优化性能的七个方面
- .NET 程序优化性能的七个方面
- 三段比较实用的js代码
- 50个的实用jQuery代码段
- 必备的实用jquery代码段
- 汇丰 密码器 使用
- js异步上传图片
- 宇信易诚“高级系统架构师”内训圆满结束
- Why NOT Use My Index
- 使用sqlserver日期函数获取当前日期
- 实用的七个程序段
- Android 升级版本 如何保存旧数据
- eclipse cdt 导入c ++ 工程并建立头头文件 索引
- SAP HANA 11. Using the SQLScript Debugger---terminated ,Debug不执行问题
- 修改phpmyadmin中的默认超时时间
- MVC页面重定向'页面跳转
- matlab处理各种数据、文件
- Core Animation编程指南
- 冒泡法排序