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

时间:2023-03-09 22:11:41
Android代码优化----Application节点的模板写法及UI工具类

一、 MyApplication类的编写:

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

MyApplication.java:

 package com.smyhvae.homepicdemo;

 import android.app.Application;
import android.os.Handler;
import android.os.Looper; /**
* Created by smyhvae on 2015/5/13.
*/
public class MyApplication extends Application {
//获取到主线程的上下文
private static MyApplication mContext = null;
//获取到主线程的handler
private static Handler mMainThreadHandler = null;
//获取到主线程的looper
private static Looper mMainThreadLooper = null;
//获取到主线程
private static Thread mMainThead = null;
//获取到主线程的id
private static int mMainTheadId; @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
this.mContext = this;
this.mMainThreadHandler = new Handler();
this.mMainThreadLooper = getMainLooper();
this.mMainThead = Thread.currentThread();
//android.os.Process.myUid() 获取到用户id
//android.os.Process.myPid()获取到进程id
//android.os.Process.myTid()获取到调用线程的id
this.mMainTheadId = android.os.Process.myTid();
} public static MyApplication getApplication() {
return mContext;
} public static Handler getMainThreadHandler() {
return mMainThreadHandler;
} public static Looper getMainThreadLooper() {
return mMainThreadLooper;
} public static Thread getMainThread() {
return mMainThead;
} public static int getMainThreadId() {
return mMainTheadId;
} }

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

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

     <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name=".MyApplication">

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

二、UI工具类的编写:

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

UIUtils.java:

 package com.smyhvae.homepicdemo.utils;

 import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View; import com.smyhvae.homepicdemo.MyApplication; /**
* Created by smyhvae on 2015/5/13.
*/ public class UIUtils { public static Context getContext() {
return MyApplication.getApplication();
} public static Thread getMainThread() {
return MyApplication.getMainThread();
} public static long getMainThreadId() {
return MyApplication.getMainThreadId();
} /** dip转换px */
public static int dip2px(int dip) {
final float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f);
} /** pxz转换dip */
public static int px2dip(int px) {
final float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (px / scale + 0.5f);
} /** 获取主线程的handler */
public static Handler getHandler() {
return MyApplication.getMainThreadHandler();
} /** 延时在主线程执行runnable */
public static boolean postDelayed(Runnable runnable, long delayMillis) {
return getHandler().postDelayed(runnable, delayMillis);
} /** 在主线程执行runnable */
public static boolean post(Runnable runnable) {
return getHandler().post(runnable);
} /** 从主线程looper里面移除runnable */
public static void removeCallbacks(Runnable runnable) {
getHandler().removeCallbacks(runnable);
} public static View inflate(int resId){
return LayoutInflater.from(getContext()).inflate(resId,null);
} /** 获取资源 */
public static Resources getResources() { return getContext().getResources();
} /** 获取文字 */
public static String getString(int resId) {
return getResources().getString(resId);
} /** 获取文字数组 */
public static String[] getStringArray(int resId) {
return getResources().getStringArray(resId);
} /** 获取dimen */
public static int getDimens(int resId) {
return getResources().getDimensionPixelSize(resId);
} /** 获取drawable */
public static Drawable getDrawable(int resId) {
return getResources().getDrawable(resId);
} /** 获取颜色 */
public static int getColor(int resId) {
return getResources().getColor(resId);
} /** 获取颜色选择器 */
public static ColorStateList getColorStateList(int resId) {
return getResources().getColorStateList(resId);
}
//判断当前的线程是不是在主线程
public static boolean isRunInMainThread() {
return android.os.Process.myTid() == getMainThreadId();
} public static void runInMainThread(Runnable runnable) {
if (isRunInMainThread()) {
runnable.run();
} else {
post(runnable);
}
} public static void startActivity(Intent intent){
// BaseActivity activity = BaseActivity.getForegroundActivity();
// if(activity != null){
// activity.startActivity(intent);
// }else{
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// getContext().startActivity(intent);
// }
} /** 对toast的简易封装。线程安全,可以在非UI线程调用。 */
public static void showToastSafe(final int resId) {
showToastSafe(getString(resId));
} /** 对toast的简易封装。线程安全,可以在非UI线程调用。 */
public static void showToastSafe(final String str) {
if (isRunInMainThread()) {
showToast(str);
} else {
post(new Runnable() {
@Override
public void run() {
showToast(str);
}
});
}
} private static void showToast(String str) {
// BaseActivity frontActivity = BaseActivity.getForegroundActivity();
// if (frontActivity != null) {
// Toast.makeText(frontActivity, str, Toast.LENGTH_LONG).show();
// }
}
}