2017-06-01 131 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

相关问题