Android代码优化----Application节点的模板写法及UI工具类

 

 

一、 MyApplication类的编写:

新建一个类MyApplication,继承自Application。代码如下:

MyApplication.java:

 1 package com.smyhvae.homepicdemo;
 2 
 3 import android.app.Application;
 4 import android.os.Handler;
 5 import android.os.Looper;
 6 
 7 /**
 8  * Created by smyhvae on 2015/5/13.
 9  */
10 public class MyApplication extends Application {
11     //获取到主线程的上下文
12     private static MyApplication mContext = null;
13     //获取到主线程的handler
14     private static Handler mMainThreadHandler = null;
15     //获取到主线程的looper
16     private static Looper mMainThreadLooper = null;
17     //获取到主线程
18     private static Thread mMainThead = null;
19     //获取到主线程的id
20     private static int mMainTheadId;
21 
22     @Override
23     public void onCreate() {
24         // TODO Auto-generated method stub
25         super.onCreate();
26         this.mContext = this;
27         this.mMainThreadHandler = new Handler();
28         this.mMainThreadLooper = getMainLooper();
29         this.mMainThead = Thread.currentThread();
30         //android.os.Process.myUid()   获取到用户id
31         //android.os.Process.myPid()获取到进程id
32         //android.os.Process.myTid()获取到调用线程的id
33         this.mMainTheadId = android.os.Process.myTid();
34     }
35 
36     public static MyApplication getApplication() {
37         return mContext;
38     }
39 
40     public static Handler getMainThreadHandler() {
41         return mMainThreadHandler;
42     }
43 
44     public static Looper getMainThreadLooper() {
45         return mMainThreadLooper;
46     }
47 
48     public static Thread getMainThread() {
49         return mMainThead;
50     }
51 
52     public static int getMainThreadId() {
53         return mMainTheadId;
54     }
55 
56 }

上面的所有代码每次在开发一个新的app时都需要用到的,然后具体到不同的项目,再继续添加不同的东西。

然后记得在清单文件中进行声明:

1     <application
2         android:allowBackup="true"
3         android:icon="@mipmap/ic_launcher"
4         android:label="@string/app_name"
5         android:theme="@style/AppTheme" 
6         android:name=".MyApplication">

需要声明的是上方代码的第6行android:name的属性。

 

二、UI工具类的编写:

这个工具类也是在app开发中经常用到的。可以直接copy。代码如下:

UIUtils.java:

  1 package com.smyhvae.homepicdemo.utils;
  2 
  3 import android.content.Context;
  4 import android.content.Intent;
  5 import android.content.res.ColorStateList;
  6 import android.content.res.Resources;
  7 import android.graphics.drawable.Drawable;
  8 import android.os.Handler;
  9 import android.view.LayoutInflater;
 10 import android.view.View;
 11 
 12 import com.smyhvae.homepicdemo.MyApplication;
 13 
 14 /**
 15  * Created by smyhvae on 2015/5/13.
 16  */
 17 
 18 public class UIUtils {
 19 
 20     
 21     public static Context getContext() {
 22         return MyApplication.getApplication();
 23     }
 24 
 25     public static Thread getMainThread() {
 26         return MyApplication.getMainThread();
 27     }
 28 
 29     public static long getMainThreadId() {
 30         return MyApplication.getMainThreadId();
 31     }
 32 
 33     /** dip转换px */
 34     public static int dip2px(int dip) {
 35         final float scale = getContext().getResources().getDisplayMetrics().density;
 36         return (int) (dip * scale + 0.5f);
 37     }
 38 
 39     /** pxz转换dip */
 40     public static int px2dip(int px) {
 41         final float scale = getContext().getResources().getDisplayMetrics().density;
 42         return (int) (px / scale + 0.5f);
 43     }
 44 
 45     /** 获取主线程的handler */
 46     public static Handler getHandler() {
 47         return MyApplication.getMainThreadHandler();
 48     }
 49 
 50     /** 延时在主线程执行runnable */
 51     public static boolean postDelayed(Runnable runnable, long delayMillis) {
 52         return getHandler().postDelayed(runnable, delayMillis);
 53     }
 54 
 55     /** 在主线程执行runnable */
 56     public static boolean post(Runnable runnable) {
 57         return getHandler().post(runnable);
 58     }
 59 
 60     /** 从主线程looper里面移除runnable */
 61     public static void removeCallbacks(Runnable runnable) {
 62         getHandler().removeCallbacks(runnable);
 63     }
 64 
 65     public static View inflate(int resId){
 66         return LayoutInflater.from(getContext()).inflate(resId,null);
 67     }
 68 
 69     /** 获取资源 */
 70     public static Resources getResources() {
 71         
 72         return getContext().getResources();
 73     }
 74 
 75     /** 获取文字 */
 76     public static String getString(int resId) {
 77         return getResources().getString(resId);
 78     }
 79 
 80     /** 获取文字数组 */
 81     public static String[] getStringArray(int resId) {
 82         return getResources().getStringArray(resId);
 83     }
 84 
 85     /** 获取dimen */
 86     public static int getDimens(int resId) {
 87         return getResources().getDimensionPixelSize(resId);
 88     }
 89 
 90     /** 获取drawable */
 91     public static Drawable getDrawable(int resId) {
 92         return getResources().getDrawable(resId);
 93     }
 94 
 95     /** 获取颜色 */
 96     public static int getColor(int resId) {
 97         return getResources().getColor(resId);
 98     }
 99 
100     /** 获取颜色选择器 */
101     public static ColorStateList getColorStateList(int resId) {
102         return getResources().getColorStateList(resId);
103     }
104     //判断当前的线程是不是在主线程 
105     public static boolean isRunInMainThread() {
106         return android.os.Process.myTid() == getMainThreadId();
107     }
108     
109     public static void runInMainThread(Runnable runnable) {
110         if (isRunInMainThread()) {
111             runnable.run();
112         } else {
113             post(runnable);
114         }
115     }
116 
117     public static void startActivity(Intent intent){
118 //        BaseActivity activity = BaseActivity.getForegroundActivity();
119 //        if(activity != null){
120 //            activity.startActivity(intent);
121 //        }else{
122 //            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
123 //            getContext().startActivity(intent);
124 //        }
125     }
126 
127     /** 对toast的简易封装。线程安全,可以在非UI线程调用。 */
128     public static void showToastSafe(final int resId) {
129         showToastSafe(getString(resId));
130     }
131 
132     /** 对toast的简易封装。线程安全,可以在非UI线程调用。 */
133     public static void showToastSafe(final String str) {
134         if (isRunInMainThread()) {
135             showToast(str);
136         } else {
137             post(new Runnable() {
138                 @Override
139                 public void run() {
140                     showToast(str);
141                 }
142             });
143         }
144     }
145 
146     private static void showToast(String str) {
147 //        BaseActivity frontActivity = BaseActivity.getForegroundActivity();
148 //        if (frontActivity != null) {
149 //            Toast.makeText(frontActivity, str, Toast.LENGTH_LONG).show();
150 //        }
151     }
152 }

 

posted @ 2015-05-13 16:34  千古壹号  阅读(2770)  评论(0编辑  收藏  举报