Users Online

· Guests Online: 93

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

137 Java Android Program to Send Message From Service to Activity

Java Android Program to Send Data From Service to Activity in Android

Here is source code of the Program to Send Data From Service to Activity in Android. 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

advertisements

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 CREATOR = new Parcelable.Creator() {
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

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;
}

}

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.91 seconds
10,827,097 unique visits