2017-06-01 89 views

回答

1

加入此服務

public class AccessTokenService extends Service { 
    int i = 0; 
    HttpHelper httpHelper = new HttpHelper(); 
    private PreferenceUtils preferenceUtils; 

    public AccessTokenService() { 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     handler.removeCallbacks(runnableCode); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     handler = new Handler(); 
     runnableCode = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        PreferenceUtils preferenceUtils; 
        preferenceUtils = new PreferenceUtils(AccessTokenService.this); 
        StringBuilder urlBuilder = new StringBuilder(); 
        urlBuilder.append("<<url>>"); 


        new UpdateAccessToken().execute(urlBuilder.toString()); 
       } finally { 
        PreferenceUtils preferenceUtils; 
        preferenceUtils = new PreferenceUtils(AccessTokenService.this); 
        handler.postDelayed(runnableCode, (long) (preferenceUtils.getExpiresIn() * 0.75)); 
        //handler.postDelayed(runnableCode, 10000); 

        LoggerUtils.info("refresh time", String.valueOf(preferenceUtils.getExpiresIn() * 0.75)); 
       } 
      } 
     }; 
     callAsynchronousTask(); 
     return Service.START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO: Return the communication channel to the service. 
     throw new UnsupportedOperationException("Not yet implemented"); 
    } 

    Handler handler; 
    Runnable runnableCode; 

    public void callAsynchronousTask() { 

     //updateAccessToken.execute("http://139.162.42.96:9900/api/Countries"); 

     runnableCode.run(); 
    } 

    public class UpdateAccessToken extends AsyncTask<String, Void, String> { 

     @Override 
     protected void onPostExecute(String s) { 
      preferenceUtils = new PreferenceUtils(AccessTokenService.this); 
      LoggerUtils.info("service data" + i++, s); 

      try { 
       JSONObject jsonObject = new JSONObject(s); 
       if (jsonObject.has("access_token")) { 
        RefreshTokenDomain refreshTokenDomain = new Gson().fromJson(s, RefreshTokenDomain.class); 
        preferenceUtils.saveAccessTokenRefreshToken(refreshTokenDomain); 
        AppConstant.accessUpdated = true; 
        if (AppConstant.updated != null) 
         AppConstant.updated.isUpdated(); 
       } else { 
        gotoLogin(); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     protected String doInBackground(String... params) { 

      //call ur service 
     } 
    } 

    private void gotoLogin() { 
     preferenceUtils.doLogout(); 
     onDestroy(); 
     Intent intent = new Intent(this, LoginActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(intent); 
    } 

    public interface Updated { 
     void isUpdated(); 
    } 
} 
+0

儘管此代碼片段**可能會解決問題,包括解釋確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – Denny

+1

@Denny除了我非常確定這裏的目標不是爲了幫助,而是爲了找出一些重要點。看到upvote,它的工作。 – 2Dee

相關問題