一、应用程序上下文

Android 应用程序上下文: 扩展 Application 类 ; AndroidManifest.xml 中注册 <application android:name=".AppContext"  />

  BaseApplication extend Application  ;

  AppContext extend BaseApplication

 

BaseApplication 功能解析:

  1、context 

    private static Context sContext;
    private static Resources sResources;

    @Override
    public void onCreate() {
        super.onCreate();

        sContext=getApplicationContext();
        sResources=sContext.getResources();
    }

    public static synchronized Context context(){
        return sContext;
    }

    public static Resources resources(){
        return sResources;
    }

  2、showToast : Toast消息提醒,防止重复或间隔时间过短

	
	private static String sLastToastMessage = "";
    private static long sLastToastTime;
	
	public static void showToast(int message) {
        showToast(message, Toast.LENGTH_LONG, 0);
    }

    public static void showToast(String message) {
        showToast(message, Toast.LENGTH_LONG, 0);
    }

    public static void showToastShort(int message) {
        showToast(message, Toast.LENGTH_SHORT, 0);
    }

    public static void showToastShort(String message) {
        showToast(message, Toast.LENGTH_SHORT, 0);
    }

    public static void showToast(String message, int duration, int icon) {
        showToast(message, duration, icon, Gravity.BOTTOM);
    }

    public static void showToast(int message, int duration, int icon) {
        showToast(resources().getString(message), duration, icon, Gravity.BOTTOM);
    }

    public static void showToast(int message, int duration, int icon, int gravity) {
        showToast(resources().getString(message), duration, icon, gravity);
    }

    public static void showToast(int message, int duration, int icon, int gravity, Object... args) {
        showToast(resources().getString(message, args), duration, icon, gravity);
    }

    public static void showToast(String message, int duration, int icon, int gravity) {
        if (message != null && !message.equalsIgnoreCase("")) {
            if (message.equalsIgnoreCase(sLastToastMessage) ||
                    Math.abs(sLastToastTime - System.currentTimeMillis()) < 2000) {
                return;
            }

            View view = LayoutInflater.from(context()).inflate(R.layout.view_toast, null);
            TextView tvMessage = (TextView) view.findViewById(R.id.tv_message);
            ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
            tvMessage.setText(message);
            if (icon != 0) {
                ivIcon.setImageResource(icon);
                ivIcon.setVisibility(View.VISIBLE);
            }
            Toast toast = new Toast(context());
            toast.setView(view);

            if (gravity == Gravity.CENTER)
                toast.setGravity(gravity, 0, 0);
            else
                toast.setGravity(gravity, 0, 35); // margin-bottom:35

            toast.setDuration(duration);
            toast.show();

            sLastToastMessage = message;
            sLastToastTime = System.currentTimeMillis();
        }
    }

  view_toast.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="@drawable/toast_background">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="wrap_content"
        android:layout_height="40.0dip"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:scaleType="fitCenter"
        android:visibility="gone" />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/iv_icon"
        android:gravity="center_vertical"
        android:textColor="@color/white"
        android:textSize="16.0dip" />
</LinearLayout>

  3、sharePreference : 首选项的setter与getter , editor提交方式:commit vs. apply

   private static final String PREFERENCE_NAME="oschina.pref";

  public static SharedPreferences getPreferences(){
        return context().getSharedPreferences(PREFERENCE_NAME,Context.MODE_MULTI_PROCESS);
    }

    public static void preference_editor_commit(SharedPreferences.Editor editor){
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD){
            editor.apply();
        }else{
            editor.commit();
        }
    }

    public static void setPreference(String key,int value){
        SharedPreferences.Editor editor=getPreferences().edit();
        editor.putInt(key,value);
        preference_editor_commit(editor);
    }

    public static void setPreference(String key,boolean value){
        SharedPreferences.Editor editor=getPreferences().edit();
        editor.putBoolean(key, value);
        preference_editor_commit(editor);
    }

    public static void setPreference(String key,String value){
        SharedPreferences.Editor editor=getPreferences().edit();
        editor.putString(key, value);
        preference_editor_commit(editor);
    }

    public static int getPreference(String key,int defValue){
        return getPreferences().getInt(key,defValue);
    }

    public static boolean getPreference(String key,boolean defValue){
        return getPreferences().getBoolean(key,defValue);
    }

    public static String getPreference(String key,String defValue){
        return getPreferences().getString(key,defValue);
    }

  4、BaseApplication 完整代码

package com.yizhui.oschina.base;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Build;
import android.view.Gravity;
import android.widget.Toast;

/**
 * Created by Yizhui on 2016/5/21.
 */
public class BaseApplication extends Application {

    private static Context sContext;
    private static Resources sResources;

    private static String sLastToastMessage="";
    private static long sLastToastTime;

    private static final String PREFERENCE_NAME="oschina.pref";

    @Override
    public void onCreate() {
        super.onCreate();

        sContext=getApplicationContext();
        sResources=sContext.getResources();
    }

    public static synchronized Context context(){
        return sContext;
    }

    public static Resources resources(){
        return sResources;
    }


    public static SharedPreferences getPreferences(){
        return context().getSharedPreferences(PREFERENCE_NAME,Context.MODE_MULTI_PROCESS);
    }

    public static void preference_editor_commit(SharedPreferences.Editor editor){
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.GINGERBREAD){
            editor.apply();
        }else{
            editor.commit();
        }
    }

    public static void setPreference(String key,int value){
        SharedPreferences.Editor editor=getPreferences().edit();
        editor.putInt(key,value);
        preference_editor_commit(editor);
    }

    public static void setPreference(String key,boolean value){
        SharedPreferences.Editor editor=getPreferences().edit();
        editor.putBoolean(key, value);
        preference_editor_commit(editor);
    }

    public static void setPreference(String key,String value){
        SharedPreferences.Editor editor=getPreferences().edit();
        editor.putString(key, value);
        preference_editor_commit(editor);
    }

    public static int getPreference(String key,int defValue){
        return getPreferences().getInt(key,defValue);
    }

    public static boolean getPreference(String key,boolean defValue){
        return getPreferences().getBoolean(key,defValue);
    }

    public static String getPreference(String key,String defValue){
        return getPreferences().getString(key,defValue);
    }


    public static void showToast(int message){
        showToast(message,Toast.LENGTH_LONG,0);
    }

    public static void showToast(String message){
        showToast(message,Toast.LENGTH_LONG,0);
    }

    public static void showToastShort(int message){
        showToast(message,Toast.LENGTH_SHORT,0);
    }

    public static void showToastShort(String message){
        showToast(message,Toast.LENGTH_SHORT,0);
    }

    public static void showToast(String message,int duration,int icon){
        showToast(message,duration,icon,Gravity.BOTTOM);
    }

    public static void showToast(int message,int duration,int icon){
        showToast(resources().getString(message),duration,icon, Gravity.BOTTOM);
    }

    public static void showToast(int message,int duration,int icon,int gravity){
        showToast(resources().getString(message),duration,icon,gravity);
    }

    public static void showToast(int message,int duration,int icon,int gravity,Object... args){
        showToast(resources().getString(message,args),duration,icon,gravity);
    }

    public static void showToast(String message,int duration,int icon,int gravity){
        if(message!=null && !message.equalsIgnoreCase("")){
            if(message.equalsIgnoreCase(sLastToastMessage)||
                    Math.abs(sLastToastTime-System.currentTimeMillis())<2000){
                return ;
            }

            //View view= LayoutInflater.from(context()).inflate(R.layout.view_toast);
            Toast toast=new Toast(context());
            toast.setText(message);
            toast.setDuration(duration);
            toast.setGravity(gravity, 0, 0);
            toast.show();

            sLastToastMessage=message;
            sLastToastTime=System.currentTimeMillis();
        }
    }
}

  

posted @ 2016-05-21 19:28  chenyizh  阅读(166)  评论(0)    收藏  举报