2016-12-09 49 views
1

所以,我已經使用了這個,但我得到的是如何生成永不過期的Facebook頁面令牌。Android永不過期Facebook用戶令牌

場景是這樣的: 我想從我的android應用程序登錄用戶。然後,當他收到令牌時,該令牌將會轉到服務器。現在,我想要做的是: 我想讓該令牌成爲永不過期的令牌,因此我不需要讓用戶從應用程序中重新登錄。這可能嗎?

+0

取一些SharedPreference變量並存儲用戶是否登錄。如果用戶未登錄,則不要再次請求登錄。 –

+0

@NigamPatro 5個月後令牌過期了嗎? –

+0

用戶登錄後令牌的需求是什麼? –

回答

0

Globals.java

import android.app.Application; 
import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 

/** 
* Created by Furkan on 28.10.2016. 
*/ 

public class Globals extends Application{ 
    @Override 
    public void onCreate() { 
     super.onCreate(); 


    } 

    private String token; 
    public void setConfig(String key, String value) { 
     try { 
      SharedPreferences spref =  PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
      SharedPreferences.Editor spref_edit = spref.edit(); 
      spref_edit.putString(key, value); 
      spref_edit.commit(); 
     } catch (Exception e) { 
      Log.i("FD", e.getMessage()); 
     } 

    } 

    public String getConfig(String key) { 
     SharedPreferences spref =  PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     return spref.getString(key, ""); 
    } 

    public void delConfig(String key) { 
     SharedPreferences spref =  PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     SharedPreferences.Editor spref_edit = spref.edit(); 
     spref_edit.remove(key); 
     spref_edit.commit(); 
    } 


    public void clearConfig() { 
     SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 
     SharedPreferences.Editor spref_edit = spref.edit(); 
     spref_edit.clear(); 
     spref_edit.commit(); 
    } 

    public void setToken(String token) { 
     this.token = token; 
     setConfig("token", token); 
    } 
} 

這個類可以幫助您保存令牌。

你應該做的主要課程中;

Globals g; 

到達令牌之後,你應該保存它喜歡;

g.setToken(token); 

如果你想跳過登錄屏幕,如果有一個保存的令牌,你應該做一些事情;

if(!g.getConfig("token").isEmpty()){ 
     Intent intent = new Intent(Main.this, BlaBlaBla.class); 
     startActivity(intent); 

祝你好運!

+0

謝謝。現在,如果令牌已過期,如何在沒有登錄的情況下獲得另一個令牌? –

+0

@MiljanVulovic我認爲它永不過期。你能告訴我一個場景嗎?那麼我會考慮一下 –

相關問題