안드로이드용 앱을 작성할 때 하나의 Activity와 다른 Activity간 소통 정보를 교환하는 방법은 여러가지가 있다. 그 중 한가지 방법은 "Intent의 Extras (Bundle)"를 이용하는 것이다.

여기서는 Extras에 Java 클래스를 넣어 정보 교환을 해 보자.
음, 점심시간에 잠깐 하려고 했더니 누가 와서 업무 관련 문의를 해서 점심 시간을 오버 했다.  아래 정리도 좀 부실해 졌군.

*정보를 교환할 클래스는 반드시 Serializable 해야 한다.
public class Contact implements Serializable {
}

*정보 전달
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;

 Intent intent = new Intent(getApplicationContext(), ContactEditActivity.class);
     try {
bos = new ByteArrayOutputStream(1024 * 100);
oos = new ObjectOutputStream(bos);
oos.writeObject(contact);
oos.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
     intent.putExtra("contact", bos.toByteArray());
     startActivity(intent);

*정보 수신
       Contact contact = null;
        byte[] contactStream = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;

        Intent intent = getIntent();
        contactStream = intent.getExtras().getByteArray("contact");
        
        try {
bis = new ByteArrayInputStream(contactStream);
ois = new ObjectInputStream(bis);
contact = (Contact) ois.readObject();
ois.close();
} catch (StreamCorruptedException e) {
e.printStackTrace();
return;
    }

*** 참고 문헌 ***

Posted by 산사랑

2010/07/21 13:13 2010/07/21 13:13
, , , , ,
Response
No Trackback , No Comment
RSS :
http://www.jopenbusiness.com/tc/oss/rss/response/254

Trackback URL : http://www.jopenbusiness.com/tc/oss/trackback/254

Leave a comment
[로그인][오픈아이디란?]
« Previous : 1 : ... 7 : 8 : 9 : 10 : 11 : 12 : 13 : 14 : 15 : ... 253 : Next »

블로그 이미지

개인적인 글쓰기와 오픈소스 비즈니스 컨설팅 관련 글을 정리합니다. consult (골뱅이) jopenbusiness.com

- 산사랑

Archives

78명이 RSS를 구독하고 있습니다.

Site Stats

Total hits:
143485
Today:
43
Yesterday:
240

*** 방문자 통계 ***
0902 : (187)
0903 : (186)
0904 : (149)
0905 : (160)
0906 : (236)
0907 : (253)
0908 : (240)
0909 : (43)
7일간 총 방문자수 : 1411