관리 메뉴

드럼치는 프로그래머

[안드로이드] 제13강좌 - Activity 추가하기, Intent 자료교환 (통신) 본문

★─Programing/☆─Android

[안드로이드] 제13강좌 - Activity 추가하기, Intent 자료교환 (통신)

드럼치는한동이 2011. 6. 15. 10:29

@@@ Activity @@@

1) 마법사 이용

src->new->class->pakage선택->name작성 -> finish->필요한 사항 작성

2)처음부터 생성

.java->오른쪽 버튼->copy->activity폴더->paste->이름 변경->Manifast파일에 <Activity>의 name선언

ex)     <activity

                   android:name=".class명"
                        </activity>

@어플리케이션 안에서 액티비티간의 화면전화

@어플리케이션 간의 액티비티 화면전환

===========================================================================================================================================

@@@ main.java @@@  작동 안됨. ㅠㅜ

package com.android.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.app.*;
import android.os.*;
import android.content.*;
import android.view.*;
import android.widget.*;

public class AndroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btnCall=(Button)findViewById(R.id.call);
        btnCall.setOnClickListener(new Button.OnClickListener(){
         public void onClick(View v){
          Intent intent=new Intent(AndroidActivity.this,CopyOfAndroidActivity.class); //다른 액티비티를 호출하는 구문 (어디에,무엇을)
           startActivity(intent);        

           }
        });
findViewById(R.id.web).setOnClickListener(mClickListener);
        findViewById(R.id.dial).setOnClickListener(mClickListener);
        findViewById(R.id.pic).setOnClickListener(mClickListener);
        findViewById(R.id.other).setOnClickListener(mClickListener);
    }
    Button.OnClickListener mClickListener = new View.OnClickListener() {
  
  @Override
  public void onClick(View v) {
   Intent intent;
   switch(v.getId()){
   case R.id.web:
    intent =new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
    startActivity(intent);
    break;
   case R.id.dial:
    intent =new Intent(Intent.ACTION_DIAL,Uri.parse("tel:010-4599-0450")); //ACTION_CALL이면 바로 전화걸림
    startActivity(intent);
    break;
   case R.id.pic:
    intent =new Intent(Intent.ACTION_VIEW);
    Uri uri=Uri.fromFile(new File("/sdcard/test.jpg"));
    intent.setDataAndType(uri,"Image/jpeg");
    startActivity(intent);
    break;
   case R.id.other:
    intent =new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName("com.android.activity","com.android.activity.AndroidActivity"));//(pakage명,pakage.클래스명)
    //intent.setClassName(,);도 가능
    startActivity(intent);
    break;
   }
   
  }
 };
}

@@@ 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:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
    <Button
     android:id="@+id/call"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Select"
     />
     <Button
     android:id="@+id/web"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="web"
     />
     <Button
     android:id="@+id/dial"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="dial"
     />
     <Button
     android:id="@+id/pic"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="pic"
     />
     <Button
     android:id="@+id/other"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="other"
     />
</LinearLayout>

============================================================================================================================================

@@@ sub.java @@@

package com.android.activity;

import android.app.Activity;
import android.os.Bundle;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.*;

public class CopyOfAndroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.subActi);
       
        Button btnCall=(Button)findViewById(R.id.close);
        btnCall.setOnClickListener(new Button.OnClickListener(){
         public void onClick(View v){
          finish();    //어플 종료 하는 .,.
         }
        });
    }
}

@@@ xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <Button
     android:id="@+id/close"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Close"
     />
</LinearLayout>

@@@ Manifast @@@

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity android:name=".CopyOfAndroidActivity"
                  android:label="subActi">
                  </activity>

    </application>
    <uses-sdk android:minSdkVersion="7" />

</manifest>

========================================================================================================================

@@@ 통신장치(자료교환) @@@

1. Intent : 메세지 시스템

     public void startActivity(Intent intent)메서드를 사용한다.

  - 암시적 인텐트

    Intent(String action[,Uri,uri]) : uri에 다음의 단어들이 들어가 있으면 암시적으로 연결// tel: =>전화걸기, geo:=>구글맵연결,http:=>인터넷연결 //

  - 명시적 인텐트
 

@@@  메인.java @@@

package com.android.to;

import android.os.*;
import android.app.*;
import android.content.*;
import android.view.*;
import android.widget.*;

public class Androidto extends Activity {
    /** Called when the activity is first created. */
 TextView mText;
 final static int ACT_EDIT = 0; //작업구분을 할 상수 선언,
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mText=(TextView)findViewById(R.id.text01);
        Button btnEdit=(Button)findViewById(R.id.button01);
        btnEdit.setOnClickListener(new Button.OnClickListener(){
         @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent=new Intent(Androidto.this,CopyOfAndroidto.class);//어디에(처음화면) , 무엇을(서브)
          startActivityForResult(intent,ACT_EDIT); //값을 주고 다시 받아오기 위해서 startActivityForResult을 사용
   }
        });
    }
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
     switch(requestCode){
     case ACT_EDIT: //작업구분값을 확인. 다른 액티비티에서의 결과값에 따라
      if(resultCode==RESULT_OK){
       mText.setText(data.getStringExtra("TextOut"));
      }
     }
    }
}

@@@ 서브.java @@@

package com.android.to;

import android.os.*;
import android.app.*;
import android.content.*;
import android.view.*;
import android.widget.*;

public class CopyOfAndroidto extends Activity {
    /** Called when the activity is first created. */
 EditText mEdit;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
       
        mEdit=(EditText)findViewById(R.id.edit);
       
        Intent intent=getIntent();
        mEdit.setText(intent.getStringExtra("TextIn"));
       
        Button btnOK=(Button)findViewById(R.id.ok);
        btnOK.setOnClickListener(new Button.OnClickListener(){
         @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent=new Intent();
    intent.putExtra("TextOut", mEdit.getText().toString());//값을 보내줌 메인으로
    setResult(RESULT_OK,intent);//결과값과 상수값을 반환
            finish(); //종료
   }
        });
        Button btnCancle=(Button)findViewById(R.id.cancle);
        btnCancle.setOnClickListener(new Button.OnClickListener(){
         @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    
    setResult(RESULT_CANCELED); //결과값은 반환하지 않고 화면만 전환 됨
    finish();
           
   }
        });
    }
}

 

@@@ 메인 .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:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
     <TextView
   android:id="@+id/text01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="text"/>
  <Button
   android:id="@+id/button01"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="button" />
</LinearLayout>

 

@@@ 서브.xml@@@

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 <EditText
  android:id="@+id/edit"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
  <Button
   android:id="@+id/ok"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="OK" />
   <Button
   android:id="@+id/cancle"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Cancle"/>
   
</LinearLayout>



Comments