1

From a login activity while pressing a button I run the following service:

public class PlayerService extends Service{

    private StorageApi api;
    private String user, password, host = "****";
    private Intent i;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        i = intent;
        user = intent.getStringExtra("User");
        password = intent.getStringExtra("Pass");

        new connectStorage().execute();

        return startId;
    }
    @Override
    public void onDestroy() {

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private class connectStorage extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected Boolean doInBackground(Void... params) {
            try {

                api = DefaultClientFactory.create(host, user, password);
                Log.i("TEST",""+api.getUserInfo());
                return true;
            } catch (StorageApiException e) {
                e.printStackTrace();
                Log.i("TEST", "" + e.getMessage());

                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);

            if(result == false){
//                progressGenerator.finnish(btnSignIn);
//                btnSignIn.setProgress(-1);
                Toast.makeText(getApplicationContext(), "Invalid E-mail or Password !", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Intent myIntent = new Intent(PlayerService.this, musicPlayer.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getApplicationContext().startActivity(myIntent);

            }
        }
    }
}

As you see in Asynctask after I connect to api I use

            Log.i("TEST",""+api.getUserInfo());

getUserInfo method and I get informations like Username, Name, Last Name, E-mail etc...

After doing this this, onPostExecute I switch activity to another screen. My question is, how I can use the API object after connection was initialized and for example get user info on another activity ?

Marian Pavel
  • 2,726
  • 8
  • 29
  • 65

2 Answers2

1

You can make your objects Parcelable something like this:

ublic class StorageApi implements Parcelable{
    private String userInfo;
...

then you must override writeToParcel(Parcel dest, int flags) method in that class.

Then you can send your objects to another activity like this:

intent.putExtra("api", new StorageApi (api.getUserInfo(), ...));

For more info you can take a look at this question it will gives you a good example.

Community
  • 1
  • 1
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
1

You can store the contents of api.getUserInfo() in a string and then pass the string via intent to the activity you would like to switch. For ex in your code you could write

@Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);

            if(result == false){
//                progressGenerator.finnish(btnSignIn);
//                btnSignIn.setProgress(-1);
                Toast.makeText(getApplicationContext(), "Invalid E-mail or Password !", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Intent myIntent = new Intent(PlayerService.this, musicPlayer.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                myIntent.putExtra("apiInfo", api.getUserInfo());
                getApplicationContext().startActivity(myIntent);

            }
        }

and then in your other activity you can get this api info by

@Override
    protected void onCreate(Bundle savedInstanceState) {
        String apiInfo;
        super.onCreate(savedInstanceState);
        Intent i = getIntent();
        //If api.getUserInfo() returns a String
        apiInfo = i.getStringExtra("apiInfo");
        //If api.getUserInfo() returns a String Array
        apiInfo = i.getStringArrayExtra("apiInfo");



    }
bonney
  • 455
  • 4
  • 7