마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

putExtra 관련 질문

0 추천
public void run(){
    byte[] buffer = new byte[1024];  // buffer store for the stream

    int bytes; // bytes returned from read()

    while (true) {
        // Read from the InputStream
        try {
            bytes = mmInStream.read(buffer);
            String incomingMessage = new String(buffer, 0, bytes);
            Log.d(TAG, "InputStream: " + incomingMessage);

            Intent incomingMessageIntent = new Intent("IncomingMessage");
            incomingMessageIntent.putExtra("theMessage" , incomingMessage);
            LocalBroadcastManager.getInstance(mContext).sendBroadcast(incomingMessageIntent);

        } catch (IOException e) {
            Log.e(TAG, "write: Error reading Input Stream. " + e.getMessage() );
            break;
        }
    }
}

putExtra로 Intent한 incomingMessageIntent에 문자를 넣어주려고 하는데요

Intent 방법이랑 putExtra 방법이 틀렸나요?? 문자가 저장이 안되는거 같네요

연세골드 님이 2017년 11월 5일 질문

1개의 답변

0 추천
값을 넘길 Activity와 값을 받을 Activity 예제 입니다.

 

1. 객체가 아닌 단순 String, int등의 변수를 넘길때

 

[넘기는 Activity]

String value = "넘긴다";

Intent intent = new Intent(this, Activity2.class); // 현재 액티비티에서 Activity2 클래스로 값을 넘길 예정
intent.putExtra("send", value);
startActivity(intent);

//startActivityForResult(intent, 123333); // 사용해도 되고 안해도 되고. 사용하게되면 해당 함수를 구글에 찾아보세요

 

[받는 Activity]

Activity2.java 의 onCreate()에서

String send = getIntent().getStringExtra("send");  // "넘긴다"를 받음

 

2. 객체를 넘길경우는 필요하시면 재질문 해주세요.
우쭈쭈 (540 포인트) 님이 2017년 12월 6일 답변
...