이 블로그 검색

2011년 11월 9일 수요일

Intent Action을 통한 다른 Application 끼리 통신.

http://alnova2.tistory.com/365   <-- 요기서 퍼옴.

setAction(key) 와 intent-filter 의 action tag 로 통신 할 수 있음. 


* 인텐트 Sender & Receiver  <-- Application 1
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");   <-- setAction 으로 Action Key 값을 날린다.
        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   <-- application 2
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" />   <-날린 Action Key로 받는다. 
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest> 

댓글 없음:

댓글 쓰기