Send Data from Service to Activity in Android

Here is source code of the Program to Send Data From Service to Activity in Android using Java. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

The following code demonstrates to send an object from one activity to another , we can’t do that directly an approach to solve this problem has been demonstrated by using Parcable class in android to send object data from one activity to another.
Send data by

     intent.putExtra("value", object);

and similarly retreive by –

     Intent i = getIntent();
     Myobject myObject = (Myobject) i.getParcelableExtra("value");

Main Activity

advertisement
advertisement
package com.example.javaandroidprogramtosenddatafromoneactivitytoanotherinandroid;
 
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button mybuttton = (Button) findViewById(R.id.button1);
        final EditText inp = (EditText) findViewById(R.id.inp);
 
        mybuttton.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int num = Integer.parseInt(inp.getText().toString());
                Intent intent = new Intent(getApplicationContext(),
                        Second.class);
                Myobject objMyobject = new Myobject();
                objMyobject.mData = num;
                intent.putExtra("value", objMyobject);
                startActivity(intent);
 
            }
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}

Myobject

package com.example.javaandroidprogramtosenddatafromoneactivitytoanotherinandroid;
 
import android.os.Parcel;
import android.os.Parcelable;
 
public class Myobject implements Parcelable {
    int mData;
 
    public int describeContents() {
        return 0;
    }
 
    // write your object's data to the passed-in Parcel
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(mData);
    }
 
    public static final Parcelable.Creator<Myobject> CREATOR = new Parcelable.Creator<Myobject>() {
        public Myobject createFromParcel(Parcel in) {
            return new Myobject(in);
        }
 
        public Myobject[] newArray(int size) {
            return new Myobject[size];
        }
    };
 
    Myobject(Parcel in) {
        mData = in.readInt();
    }
 
    Myobject() {
    }
}

Second

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
package com.example.javaandroidprogramtosenddatafromoneactivitytoanotherinandroid;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
 
public class Second extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        TextView out = (TextView)findViewById(R.id.out);
        Intent i = getIntent();
        Myobject myObject = (Myobject) i.getParcelableExtra("value");
        out.setText("Received : " + "\n"+myObject.mData);
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }
 
}

Screenshot_2013-11-21-05-27-04

advertisement

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

If you wish to look at all Tutorials, go to Java Android Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.