반응형
Android 에서는 Activity 간 통신을 할때 Intent 를 이용한다. 이는 하나의 어플리케이션 내에서 뿐만 아니라 어플리케이션 간의 통신에서도 활용이 가능하다. 다음은 간단하게 Intent 를 이용하여 다른 어플리케이션과 데이터를 주고 받는 예제이다.

* 인텐트 Sender & Receiver
1. intentsender.java
package com.android.intentsender;
....
public class intentsender extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // sendIntent
        Intent i = new Intent();
        i.setAction("com.android.intentsender.sendintent");
        i.putExtra("send", "Hello I'am Intent sender");
        startActivityForResult(i,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        TextView tv=(TextView)findViewById(R.id.tv01);
        if(resultCode==RESULT_OK){
            if (requestCode==1){
                tv.setText(data.getStringExtra("reply"));
            }
        }
    }
}
2. layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:id ="@+id/tv01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

- 이 어플리케이션은 com.android.intentsender.sendintent(임의의) 라는 action 을 가지고 activity를 실행한다. 실행한 결과를 TextView에 뿌리도록 한다

* 인텐트 Receiver/Reply
1. intentreceiver.java
package com.android.intentreceiver;
....
public class intentreceiver extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent i = getIntent();
        TextView tv=(TextView)findViewById(R.id.tv01);
        tv.setText(i.getStringExtra("sender"));
        i.putExtra("reply","Hello I'am IntentReceiver");
        setResult(RESULT_OK,i);
        finish();
    }
}
- 이 어플리케이션은 intent를 받아서 reply를 설정하고 종료한다.

* 인텐트 sender/receiver를 호출하면 화면 맨 위에 인텐트 Receiver/Reply 어플리케이션에서 받은 문자열을 출력하는 것을 알수 있다. 그런데 인텐트 Receiver/Reply 어플리케이션에서 해당 인텐트를 받기 위해서는  intentreceiver의 AndroidManifest.xml에 다음과 같이 intent-filter설정을 해주어야 한다
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.intentreceiver"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".intentreceiver"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                 <action android:name="com.android.intentsender.sendintent" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>

반응형
Posted by alias
,