您的位置:首页 > 移动开发 > Android开发

Android中,一些用于统一管理的类,比如 log,比如 toast,比如 url等(toast篇)

2016-06-03 12:05 597 查看
用于显示单例的toast,还是很简单的,一个类即可:

/**
* @author tyxo
* @qq 1577441454
* @date 2015/6/3 12:02
* @desc 显示单例的吐司,能连续快速弹的吐司
*/
public class ToastUtil {

private static Toast toast;

/*public static void showToastS(String text) {
if (toast == null) {
toast = Toast.makeText(MyApp.getAppContext(), text, Toast.LENGTH_SHORT);
}
toast.setText(text);
toast.show();
}*/

public static void showToastS(Context mContext, String text) {
if (toast == null) {
toast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
}
toast.setText(text);
toast.show();
}

/*public static void showToastS(MyApp myApp, String text, String activityName) {
if (AndroidUtil.isCurrentActivityTop(myApp, activityName)) {
if (toast == null) {
toast = Toast.makeText(myApp, text, Toast.LENGTH_SHORT);
}
toast.setText(text);
toast.show();
}
}*/

/*public static void showToastL(String text) {
if (toast == null) {
toast = Toast.makeText(MyApp.getAppContext(), text, Toast.LENGTH_LONG);
}
toast.setText(text);
toast.show();
}*/

/*public static void showToastL(MyApp myApp, String text) {
if (toast == null) {
toast = Toast.makeText(myApp, text, Toast.LENGTH_LONG);
}
toast.setText(text);
toast.show();
}*/
}当我们使用的时候,直接ToastUtil.showS(context,"要吐的内容").

可能有的童鞋对上面注释的第一个方法有疑问,为啥不传context可以直接显示toast呢?

这里有个Application的全局类,在一个应用(即一个app)中,系统内会自动创建一个全局的application类,(想要细了解的请找度娘),可以在这个类里面保存一些配置的值,在整个应用中,无论是哪个类都可以获取到application内保存的值,这个类可以自己继承系统的,然后做一些修改,

作为父类的application:

public class BaseApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
instance = this;
}

private static BaseApplication instance;

public static BaseApplication getInstance() {
return instance;
}
}application具体实现类:
public class MyApp extends BaseApplication {

private boolean isLogin;
private static PlatUser mCurrentUser;
private static MyApp singleton;
private static Context mContext;
public static String AppName = "ENT_PLAT_APP";
public static final String USER_DATA_FILE = "user_data";
private static final String TAG_FOR_LOGGER = "Logger";

/**
* 初始化事件(重载)
*/
@Override
public void onCreate() {
super.onCreate();

singleton = this;
mContext = getApplicationContext();
Logger.init(TAG_FOR_LOGGER).setLogLevel(LogLevel.FULL); //日志打印.hideThreadInfo()
//创建默认的ImageLoader配置参数
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);

//Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(configuration);

startService(new Intent(getAppContext(), MonitorUserService.class));

}

public static MyApp getInstance() {
return singleton;
}

/** 设置当前登录用户,启动消息监听服务 */
public void setCurrentUser(PlatUser user) {
this.mCurrentUser = user;

if (user != null) {
JpushManager.getInstance(mContext).initJpush();
JpushManager.getInstance(mContext).setAlias("ENT_PLAT_APP_" + user.userId);
HLog.i("tyxo", "用户别名" + "ENT_PLAT_APP_" + user.userId);
} else {
JpushManager.getInstance(mContext).stopJpush();
}
}

public PlatUser getCurrentUser() { return this.mCurrentUser; }

public boolean isLogin() {
return isLogin;
}

public void setIsLogin(boolean b) {
isLogin = b;
}

public static Context getAppContext() {
return MyApp.mContext;
}

@Override
public void onTerminate() {
super.onTerminate();
}

public static DisplayMetrics md = null;

public static DisplayMetrics getDM(Context context) {
if (md == null) {
md = new DisplayMetrics();
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(md);
}
return md;
}

/**
* 检查网络状态
*/
public boolean CheckNetworkState(Context context) {

ConnectivityManager manager = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo mMobile = manager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo mWifi = manager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

// 如果3G、WIFI、2G等网络状态是连接的,则退出,否则显示提示信息进入网络设置界面
if (mMobile != null && mMobile.isConnected()) {
return true;
}
if (mWifi != null && mWifi.isConnected()) {
return true;
}

DialogUtil.showTipsMy(context);

return false;
}
}这个类请尽量存一些整个app通用的值,尽量少做操作,因为是整个应用共享,生命周期是最长的,存的太多,释放不了,甚至会OOM哟!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息