擷取全域上下文(getApplicationContext)_建立Shared Preference工具類_實現自動登入

來源:互聯網
上載者:User

標籤:

擷取全域上下文(getApplicationContext)_建立Shared Preference工具類_實現自動登入

 

===========================擷取全域上下文(getApplicationContext)========================

 

1.在com.example.autologin.myapplication包中建立Myapplication extends Application

           代碼:

 1 public class Myapplication extends Application 2 { 3     private static Context context; 4      5     @Override 6     public void onCreate() 7     { 8         super.onCreate(); 9         10         context = getApplicationContext();11     }12     13     public static Context getAppContext()14     {15         return context;16     }17 }

2.在AndroidManifest.xml中添加屬性, 申明Myapplication:

      android:name="com.example.autologin.myapplication.Myapplication"

           代碼:

 1 <!-- 申明Myapplication --> 2 <application 3     android:name="com.example.autologin.myapplication.Myapplication" 4     android:allowBackup="true" 5     android:icon="@drawable/ic_launcher" 6     android:label="@string/app_name" 7     android:theme="@style/AppTheme" > 8  9     <!-- ....... -->10 11 </application>

 

================================建立Shared Preference工具類==============================

在包com.example.autologin.utils中建立SpUtil.java類:

           代碼:

 1 public class SpUtil 2 { 3     // 4     //通過全域上下文建立靜態SharedPreferences對象 5     // 6     private static SharedPreferences sp = 7             Myapplication.getAppContext().getSharedPreferences("user",Context.MODE_PRIVATE); 8  9     //靜態儲存使用者帳號密碼方法10     public static void saveUser(String name, String pwd)11     {12         Editor edit = sp.edit();13         edit.putString("username", name);14         edit.putString("pwd", pwd);15 16         edit.commit();17 18     }19 20     //21     //儲存是否有過登入的狀態22     //23     public static void isLogin(boolean isLogin)24     {25         Editor edit = sp.edit();26         edit.putBoolean("islogin", isLogin);27         edit.commit();28     }29 30     //31     //讀取是否有過登入的狀態32     //33     public static boolean getIsLogin()34     {35         return sp.getBoolean("islogin", false);36     }37 }

 

=============================實現自動登入============================

建立歡迎頁面SplashActivity.java,讀取登入狀態,若有過登入則直接跳轉首頁面,沒有登入過跳轉登入頁面

           代碼:

 1 Handler handler = new Handler(); 2 handler.postDelayed(new Runnable() 3 { 4     @Override 5     public void run() 6     { 7         // 8         //如果有過登入 9         //直接跳轉首頁面10         //11         if (SpUtil.getIsLogin())12         {13             Intent intent = new Intent(SplashActivity.this, MainActivity.class);14             startActivity(intent);15             SplashActivity.this.finish();16         } else17         {18             //19             //如果沒有登入過登入20             //跳轉登入頁面21             //22             Intent intent = new Intent(SplashActivity.this, LoginActivity.class);23             startActivity(intent);24             SplashActivity.this.finish();25         }26 27     }28 }, 3000);

           **注意: 將SplashActivity.java設定為啟動頁面

 

擷取全域上下文(getApplicationContext)_建立Shared Preference工具類_實現自動登入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.