如何在Android中使用Service连接到Internet

时间:2017-06-01 11:45:55

标签: android service internet-connection

如何使用服务连接到互联网

1 个答案:

答案 0 :(得分: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();
    }
}