Android_CodeWiki_01

简介: 记录常用代码片,以备不时之需..wkakak,开始:    1、 精确获取屏幕尺寸(例如:3.5、4.0、5.0寸屏幕)     1 public static double getScreenPhysicalSize(Activity ctx) { 2 Displa...

 记录常用代码片,以备不时之需..wkakak,开始:

   1、 精确获取屏幕尺寸(例如:3.5、4.0、5.0寸屏幕) 

  

1  public static double getScreenPhysicalSize(Activity ctx) {
2         DisplayMetrics dm = new DisplayMetrics();
3         ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
4         double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));
5         return diagonalPixels / (160 * dm.density);
6     }

一般小于7寸的,都是非平板,属于正常智能机系列

  2、 判断是否是平板(官方用法)

  

public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_L         ARGE;
    }

3、 文字根据状态更改颜色 android:textColor 

  

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#53c1bd" android:state_selected="true"/>
    <item android:color="#53c1bd" android:state_focused="true"/>
    <item android:color="#53c1bd" android:state_pressed="true"/>
    <item android:color="#777777"/>
</selector>

放在工程 res/color/目录下

4、背景色根据状态更改颜色 android:backgroup

     

 1 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 2 
 3     <item android:state_selected="true"><shape>
 4             <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />
 5         </shape></item>
 6     <item android:state_focused="true"><shape>
 7             <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />
 8         </shape></item>
 9     <item android:state_pressed="true"><shape>
10             <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />
11         </shape></item>
12     <item><shape>
13             <gradient android:angle="0" android:centerColor="#00ff00" android:endColor="00ff00" android:startColor="00ff00" />
14         </shape></item>
15 
16 </selector>

  如果直接给背景色color会报错。

5、 启动APK的默认Activity

  

 1 public static void startApkActivity(final Context ctx, String packageName) {
 2         PackageManager pm = ctx.getPackageManager();
 3         PackageInfo pi;
 4         try {
 5             pi = pm.getPackageInfo(packageName, 0);
 6             Intent intent = new Intent(Intent.ACTION_MAIN, null);
 7             intent.addCategory(Intent.CATEGORY_LAUNCHER);
 8             intent.setPackage(pi.packageName);
 9 
10             List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);
11 
12             ResolveInfo ri = apps.iterator().next();
13             if (ri != null) {
14                 String className = ri.activityInfo.name;
15                 intent.setComponent(new ComponentName(packageName, className));
16                 ctx.startActivity(intent);
17             }
18         } catch (NameNotFoundException e) {
19             Log.e("startActivity", e);
20         }
21     }

7、计算字宽

  

1  public static float GetTextWidth(String text, float Size) {
2         TextPaint FontPaint = new TextPaint();
3         FontPaint.setTextSize(Size);
4         return FontPaint.measureText(text);
5     }

8、获取应用程序下所有Activity 

  

1  public static ArrayList<String> getActivities(Context ctx) {
2       ArrayList<String> result = new ArrayList<String>();
3       Intent intent = new Intent(Intent.ACTION_MAIN, null);
4       intent.setPackage(ctx.getPackageName());
5       for (ResolveInfo info : ctx.getPackageManager().queryIntentActivities(intent, 0)) {
6           result.add(info.activityInfo.name);
7       }
8       return result;
9   }

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

  

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

10、检查有没有应用程序来接受处理你发出的intent

  

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

 参考:http://www.cnblogs.com/over140/archive/2013/03/05/2706068.html

目录
相关文章
|
存储 安全 物联网
Android:Android 应用权限详解
这篇文章为大家系统的梳理一下 Android 权限相关的知识,在日常开发中,我们都用过权限,但是对于权限的一些细节我们可能掌握的还不够全面,这篇文章会全面的为大家介绍权限相关的知识。
942 0
Android:Android 应用权限详解
|
XML Java Android开发
Android 中的 StateListDrawable
Android 中的 StateListDrawable
87 0
|
Android开发
[Android]电话拨号器
继续今天的Android,经过昨天大体了解了Android开发的一些基本文件结构,今天来做一个电话拨号器! 预期达到的效果 实现过程 首先还是按照昨天第一篇教程,新建一个项目叫PhoneCall的Android的应用 这里采用的是Linear...
1080 0
|
Android开发
Android 多进程通信之几个基本问题
开启多进程的方法 Android 中使用多进程只有一种方法,那就是给四大组件(Activity、Service、Receiver、ContentProvider)在AndroidMenifest中指定android:process属性 <service android:name="com.
2487 0
|
XML Android开发 数据格式
Android小知识10则(下)
Android小知识10则(上)github传送门 注: 在目录中点击可以跳转到具体代码页 目录 Chronometer和CountDownTimer计时器 Chronometer的使用 CountDownTimer的使用 正则表达...
1138 0
|
Android开发
|
消息中间件 XML Android开发
|
Android开发 API 数据库
Android 之dragger使用
1、依赖的注入和配置独立于组件之外,注入的对象在一个独立、不耦合的地方初始化,这样在改变注入对象时,我们只需要修改对象的实现方法,而不用大改代码库。 2、依赖可以注入到一个组件中:我们可以注入这些依赖的模拟实现,这样使得测试更加简单。 3、app中的组件不需要知道有关实例创建和生命周期的任何事情,这些由我们的依赖注入框架管理的。 我觉得,dagger2这样的依赖注入框架对MVP架
1735 0