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哟!!
/**
* @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哟!!
相关文章推荐
- Android Manifest 用法
- Linux 自检和 SystemTap
- 个人信息安全报告发布:有 APP 每分钟调用位置权限 1468 次
- 下载量超过一亿的流行应用被发现含有恶意模块
- 如何成为一名专家级的开发人员
- Python 七步捉虫法
- 苹果与Siri的七年之痒:“宫斗”戏码不断上演
- gitinspector+jenkins 开发代码统计CI
- 跨越fso、wsh、 application写文件
- IIS 错误 Server Application Error 详细解决方法
- asp 内置对象 Application 详解
- IIS运行错误 Server Application Error 错误代码 Error: 8004的解决方法
- 路由器的配置与调试
- 对于技术人员的出现了运行时间错误,是否要进行调试的解决方法
- 解决IIS的Server Application Error的2种方法
- APP添加CNZZ统计插件教程 Android版添加phonegap
- ASP.NET中Application、Cookie、Session、Cache和ViewState
- 在ASP.NET 2.0中操作数据之七十二:调试存储过程
- ASP.NET 中的Application详解
- ASP.NET中application对象的使用介绍