Android Open Source - asyncop My Activity






From Project

Back to project page asyncop.

License

The source code is released under:

Apache License

If you think the Android project asyncop listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2014 Marco Guidi/*from   w ww  . ja va  2 s .  c om*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.mguidi.asyncop.app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.github.mguidi.asyncop.AsyncOpCallback;
import com.github.mguidi.asyncop.AsyncOpHelper;

/**
 * Created by marco on 21/07/14.
 */
public class MyActivity extends ActionBarActivity implements AsyncOpCallback, View.OnClickListener {

    private static final String SAVED_ID_REQUEST = "id_request";

    private AsyncOpHelper mOpHelper;
    private int mIdRequest = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        mOpHelper = new AsyncOpHelper(this, savedInstanceState, this);

        findViewById(R.id.btnHello).setOnClickListener(this);

        if (savedInstanceState != null) {
            mIdRequest = savedInstanceState.getInt(SAVED_ID_REQUEST);
        }

        if (mOpHelper.isLoading(mIdRequest)) {
            findViewById(R.id.loading).setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mOpHelper.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mOpHelper.onSaveInstanceState(outState);
        outState.putInt(SAVED_ID_REQUEST, mIdRequest);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mOpHelper.onPause();
    }

    @Override
    public void onAsyncOpFinish(int idRequest, String action, Bundle args, Bundle result) {
        if (action.equals(Constants.ACTION_LONGOP)) {
            findViewById(R.id.loading).setVisibility(View.GONE);

            if (result.containsKey(LongOp.RES_RESULT)) {
                TextView txtResult = (TextView) findViewById(R.id.txtResult);
                txtResult.setText(result.getString(LongOp.RES_RESULT));
            }
        }
    }

    @Override
    public void onAsyncOpFail(int idRequest, String action) {
        if (action.equals(Constants.ACTION_LONGOP)) {
            findViewById(R.id.loading).setVisibility(View.GONE);

            TextView txtResult = (TextView) findViewById(R.id.txtResult);
            txtResult.setText("fail");
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnHello) {
            Bundle args = new Bundle();
            args.putLong(LongOp.ARGS_TIMEOUT, 2000L);

            mIdRequest = mOpHelper.execute(Constants.ACTION_LONGOP, args);
            findViewById(R.id.loading).setVisibility(View.VISIBLE);
        }
    }

}




Java Source Code List

com.github.mguidi.asyncop.AsyncOpCallback.java
com.github.mguidi.asyncop.AsyncOpHelper.java
com.github.mguidi.asyncop.AsyncOpManager.java
com.github.mguidi.asyncop.AsyncOp.java
com.github.mguidi.asyncop.Constants.java
com.github.mguidi.asyncop.app.Application.java
com.github.mguidi.asyncop.app.Constants.java
com.github.mguidi.asyncop.app.LongOp.java
com.github.mguidi.asyncop.app.MyActivity.java