Android实用代码

转载自: http://www.cnblogs.com/over140/archive/2013/03/05/2706068.html


声明

  欢迎转载,但请保留文章原始出处:) 
    博客园: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;
  }
复制代码

 

二、检测字符串中是否包含汉字

复制代码
    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;
    }
复制代码

 

三、检测字符串中只能包含:中文、数字、下划线(_)、横线(-)

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

 

四、检查有没有应用程序来接受处理你发出的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);
    }
复制代码

  比使用AlphaAnimation效果要好,可避免出现闪烁问题。

 

六、 扫描指定的文件 

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

  用途:从本软件新增、修改、删除图片、文件某一个文件(音频、视频)需要更新系统媒体库时使用,不必扫描整个SD卡。

 

七、Dip转px

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

用途:难免在Activity代码中设置位置、大小等,本方法就很有用了!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值