Handler优化以及SharedPreferences工具类的封装

来源:互联网 发布:淘宝假货申诉凭证制造 编辑:程序博客网 时间:2024/06/06 02:48
eclipse里有个警告:“This Handler class should be static or leaks might occur”,为什么eclipse会提示一个handler要是静态的?

下面直接上一段优化后的Handler,由于能力有限,如有问题可以与本菜鸟联系 QQ:2428566234

技术讨论群:387648673

public class SplashActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
SplashHandler handler = new SplashHandler(this);
handler.sendEmptyMessageDelayed(100, 1000);


}
/* Handler是内部类 内部类存在对外部类的持有 

 *当外部类声明周期结束时如果不静态持有会一直存在,会造成内存泄漏 所以要静态

 * Android 源码内部类几乎都是静态的*/
private static class SplashHandler extends Handler {
private final WeakReference<SplashActivity> splash;


public SplashHandler(SplashActivity activity) {
splash = new WeakReference<SplashActivity>(activity);
}


@Override
public void handleMessage(Message msg) {
SplashActivity activity = splash.get();
if (activity != null) {
if(!SharedPreferencesUtil.getBoolean(activity,"isFirstCome")) {
Intent intent = new Intent(activity,WelcomeActivity.class);
activity.startActivity(intent);
activity.finish();

} else {
Intent intent = new Intent(activity,MainActivity.class);
activity.startActivity(intent);
activity.finish();
}
}
super.handleMessage(msg);
}
}

下面是SharedPreferences的工具类

public class SharedPreferencesUtil {


    public static boolean getBoolean(Context context, String key) {
        return getSharedPreferences(context).getBoolean(key, false);
    }


    public static boolean getBoolean(Context context, String prefName, String prefKey,
                                     boolean defaultValue) {
        return getSharedPreferences(context, prefName).getBoolean(prefKey, defaultValue);
    }


    public static void putBoolean(Context context, String key, boolean b) {
        getSharedPreferences(context).edit().putBoolean(key, b).commit();
    }


    public static void putBoolean(Context context, String prefName, String prefKey, boolean value) {
        getSharedPreferences(context, prefName).edit().putBoolean(prefKey, value).commit();
    }


    public static String getString(Context context, String key) {
        return getSharedPreferences(context).getString(key, "");
    }


    private static String getString(Context context, String key, String defaultValue) {
        return getSharedPreferences(context).getString(key, defaultValue);
    }


    public static String getString(Context context, String prefName, String prefKey,
                                   String defaultValue) {
        return getSharedPreferences(context, prefName).getString(prefKey, defaultValue);
    }


    public static void putString(Context context, String key, String value) {
        getSharedPreferences(context).edit().putString(key, value).commit();
    }


    public static void putString(Context context, String prefName, String prefKey, String value) {
        getSharedPreferences(context, prefName).edit().putString(prefKey, value).commit();
    }


    public static long getLong(Context context, String key) {
        return getSharedPreferences(context).getLong(key, -1L);
    }


    public static long getLong(Context context, String key, long defalutValue) {
        return getSharedPreferences(context).getLong(key, defalutValue);
    }


    public static long getLong(Context context, String prefName, String prefKey, long defaultValue) {
        return getSharedPreferences(context, prefName).getLong(prefKey, defaultValue);
    }


    public static void putLong(Context context, String key, long value) {
        getSharedPreferences(context).edit().putLong(key, value).commit();
    }


    public static void putLong(Context context, String prefName, String prefKey, long value) {
        getSharedPreferences(context, prefName).edit().putLong(prefKey, value).commit();
    }


    public static int getInt(Context context, String key) {
        return getSharedPreferences(context).getInt(key, -1);
    }


    public static int getInt(Context context, String key, int defaultValue) {
        return getSharedPreferences(context).getInt(key, defaultValue);
    }


    public static int getInt(Context context, String prefName, String prefKey, int defaultValue) {
        return getSharedPreferences(context, prefName).getInt(prefKey, defaultValue);
    }


    public static void putInt(Context context, String key, int value) {
        getSharedPreferences(context).edit().putInt(key, value).commit();
    }


    public static void putInt(Context context, String prefName, String prefKey, int value) {
        getSharedPreferences(context, prefName).edit().putInt(prefKey, value).commit();
    }


    public static float getFloat(Context context, String prefName, String prefKey,
                                 float defaultValue) {
        return getSharedPreferences(context, prefName).getFloat(prefKey, defaultValue);
    }


    public static void putFloat(Context context, String prefName, String prefKey, float value) {
        getSharedPreferences(context, prefName).edit().putFloat(prefKey, value).commit();
    }


    public static void putFloat(Context context, String prefKey, float value) {
        getSharedPreferences(context).edit().putFloat(prefKey, value).commit();
    }




    /**
     * 移除SharedPreferences
     *
     * @param context
     * @param prefKey
     */
    public static void remove(Context context, String prefKey) {
        getSharedPreferences(context).edit().remove(prefKey).commit();
    }


    /**
     * 清除文件内所有内容
     * @param context
     * @param preName
     */
    public static void clear(Context context, String preName) {
        getSharedPreferences(context, preName).edit().clear().commit();
    }


    /**
     * 得到默认SharePreference
     *
     * @param context
     * @return
     */
    public static SharedPreferences getSharedPreferences(Context context) {
        if (context == null) {
            context =AppContext.mContext;
        }
        return getSharedPreferences(context, null);
    }


    /**
     * 根据名字得到SharePreference
     *
     * @param context
     * @param prefName 独立存储文件
     * @return
     */
    public static SharedPreferences getSharedPreferences(Context context, String prefName) {
        if (TextUtils.isEmpty(prefName)) {
            return PreferenceManager.getDefaultSharedPreferences(context);
        } else {
            return context.getSharedPreferences(prefName, Context.MODE_PRIVATE);
        }
    }


0 0
原创粉丝点击