<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="나는 main" />
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="전화하기" />
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="문자보내기" />
<Button
android:id="@+id/button4"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="인터넷" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="나는 sub " />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gusfree.intent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".IntentActivity"
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="SubActivity"></activity>
</application>
</manifest>
package com.gusfree.intent;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class IntentActivity extends Activity implements
OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
Log.i("onCreate", "onCreate 시작");
}
@Override
public void onClick(View v) {
Log.w("onClick","버튼 눌렀다");
int id=v.getId();
Log.i("아이디","아이디"+id);
switch(v.getId()){
case R.id.button1 :
//버튼 누르면 Sub로 Activity간 이동하기
Intent intent=new Intent();
intent.setClass(this, SubActivity.class);
//intent에 데이터 binding하기
intent.putExtra("name", "park!");
//startActivity(intent); //이동
//Activity이동하고 되돌아오면 onActivityResult호출
startActivityForResult(intent, 1000); //requestCode
break;
case R.id.button2://전화하기
Intent intent2 =new Intent();
intent2.setAction(Intent.ACTION_CALL);
Uri uri = Uri.parse("tel:0101234578");
intent2.setData(uri);//Uri
startActivity(intent2);//error permission Call_Phone
//Uri : Uniform Resource Identifier
//naver.com => http://www.naver.com/
//"0101234578" => "tel:0101234578"
break;
case R.id.button3:
Intent intent3 =new Intent();
intent3.setAction(Intent.ACTION_SENDTO);
Uri uri2 = Uri.parse("sms:0101234578");
intent3.setData(uri2);//Uri
startActivity(intent3);
break;
case R.id.button4://인터넷 하기
Intent intent4=new Intent();
/* class Intent{
* static String ACTION_VIEW="android.intent.action.VIEW"
* }
* */
intent4.setAction("android.intent.action.VIEW");
intent4.setData(Uri.parse("http://gusfree.tistory.com"));
startActivity(intent4);
break;
}
}
@Override //3번째 파라미터가 인텐트!
//1param =1000, 2param = 777, 3param=theIntent
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if(requestCode==1000){//requestCode사용 예
if(resultCode==777){//resultcode사용예
String id=data.getStringExtra("id");
int month=data.getIntExtra("month", 12);
Toast.makeText(this, id+month, 0).show();
}else{
}
//super.onActivityResult(requestCode, resultCode, data);
}
}
}
package com.gusfree.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SubActivity extends Activity implements OnClickListener{
Intent theInent;//객체 선언
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
//intent에 binding된 데이터 읽기
theInent = getIntent();//(객체 초기화)나를 호출한 인텐트 찾기
String name=theInent.getStringExtra("name");//키
btn.setText(name+btn.getText());
}
@Override
public void onClick(View arg0) {
theInent.putExtra("id", "com.gusfree.intent");
theInent.putExtra("month", 5);
//forResult로 왔을때 사용해야 되는 메서드
setResult(777, theInent);//resultCode
finish();//액티비티 종료
}
}