複数のActivityとIntent

73449 ワード

IntentはAndroidアプリケーションコンポーネントの1つであり、Androidシステムには次のような意図が含まれています.
最も重要な内容はaction(アクション)とdata(データ)です
Component nameはどのActivityを起動するかを示します
 
FirstActivity.java
 1 import android.os.Bundle;

 2 import android.app.Activity;

 3 import android.content.Intent;

 4 import android.view.View;

 5 import android.view.View.OnClickListener;

 6 import android.widget.Button;

 7 

 8 public class FirstActivity extends Activity {

 9     private Button firstButton;

10     @Override

11     protected void onCreate(Bundle savedInstanceState) {

12         super.onCreate(savedInstanceState);

13         setContentView(R.layout.activity_first);

14         

15         firstButton = (Button)findViewById(R.id.firstButton);

16         firstButton.setText(R.string.firstButton);

17         firstButton.setOnClickListener(new ButtonListener());

18     }

19     

20     class ButtonListener implements OnClickListener{

21 

22         @Override

23         public void onClick(View v) {

24             Intent intent = new Intent();

25             /*setClass  :

26                             Context  ,Context    ,Activity Context    ,    ,   Activity          Context  

27                             Class  ,      ,       Activity  Class  

28             */

29             intent.setClass(FirstActivity.this, SecondActivity.class);

30             FirstActivity.this.startActivity(intent);

31         }

32         

33     }

34 

35 }

 FirstActivity.xml
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 2     xmlns:tools="http://schemas.android.com/tools"

 3     android:layout_width="match_parent"

 4     android:layout_height="match_parent"

 5     android:paddingBottom="@dimen/activity_vertical_margin"

 6     android:paddingLeft="@dimen/activity_horizontal_margin"

 7     android:paddingRight="@dimen/activity_horizontal_margin"

 8     android:paddingTop="@dimen/activity_vertical_margin"

 9     tools:context=".FirstActivity" 

10     android:orientation="vertical">

11 

12     <TextView

13         android:layout_width="wrap_content"

14         android:layout_height="wrap_content"

15         android:text="@string/first" />

16     

17     <Button

18         android:id="@+id/firstButton"

19         android:layout_width="fill_parent"

20         android:layout_height="wrap_content"/>

21 </LinearLayout>

 
SecondActivity.java
 1 import android.app.Activity;

 2 import android.content.Intent;

 3 import android.os.Bundle;

 4 import android.view.View;

 5 import android.view.View.OnClickListener;

 6 import android.widget.Button;

 7 

 8 public class SecondActivity extends Activity{

 9     private Button SecondtButton;

10     @Override

11     protected void onCreate(Bundle savedInstanceState) {

12         super.onCreate(savedInstanceState);

13         setContentView(R.layout.activity_second);

14         SecondtButton = (Button)findViewById(R.id.SecondtButton);

15         SecondtButton.setText(R.string.secondtButton);

16         SecondtButton.setOnClickListener(new ButtonListener());

17         

18     }

19     class ButtonListener implements OnClickListener{

20 

21         @Override

22         public void onClick(View v) {

23             Intent intent = new Intent();

24             intent.setClass(SecondActivity.this, ThirdActivity.class);

25             SecondActivity.this.startActivity(intent);

26             finish();//    Activity      Activity     Activity

27         }

28         

29     }

30 }

 SecondActivity.xml
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3     android:layout_width="match_parent"

 4     android:layout_height="match_parent"

 5     android:orientation="vertical" >

 6     

 7     

 8     <TextView

 9         android:layout_width="fill_parent"

10         android:layout_height="wrap_content"

11         android:text="@string/second"

12         ></TextView>

13     

14     

15     <Button

16         android:id="@+id/SecondtButton"

17         android:layout_width="fill_parent"

18         android:layout_height="wrap_content"

19         ></Button>

20 </LinearLayout>

 
ThirdActivity.java
 1 import android.app.Activity;

 2 import android.content.Intent;

 3 import android.net.Uri;

 4 import android.os.Bundle;

 5 import android.view.View;

 6 import android.view.View.OnClickListener;

 7 import android.widget.Button;

 8 

 9 public class ThirdActivity extends Activity{

10     private Button ThirdButton;

11     protected void onCreate(Bundle savedInstanceState) {

12         super.onCreate(savedInstanceState);

13         setContentView(R.layout.activity_third);

14         

15         ThirdButton = (Button)findViewById(R.id.ThirdActivity);

16         ThirdButton.setText(R.string.thirdtButton);

17         ThirdButton.setOnClickListener(new ButtonListener());

18     }

19     class ButtonListener implements OnClickListener{

20         public void onClick(View v) {

21             

22              Uri uri = Uri.parse("smsto://0800000123");    

23              Intent it = new Intent(Intent.ACTION_SENDTO, uri);    

24              it.putExtra("sms_body", "The SMS text");    

25              ThirdActivity.this.startActivity(it);   

26         }

27     }

28 }

 ThirdActivity.xml
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3     android:layout_width="match_parent"

 4     android:layout_height="match_parent"

 5     android:orientation="vertical" >

 6     <TextView

 7         android:layout_width="fill_parent"

 8         android:layout_height="wrap_content"

 9         android:text="@string/third"

10     />

11     

12     <Button

13         android:id="@+id/ThirdActivity"

14         android:layout_width="fill_parent"

15         android:layout_height="wrap_content"

16         />

17 

18 </LinearLayout>

AndroidManifest.xml
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"

 3     package="com.mars.activity06"

 4     android:versionCode="1"

 5     android:versionName="1.0" >

 6 

 7     <uses-sdk

 8         android:minSdkVersion="4"

 9         android:targetSdkVersion="18" />

10 

11     <application

12         android:allowBackup="true"

13         android:icon="@drawable/ic_launcher"

14         android:label="@string/app_name"

15         android:theme="@style/AppTheme" >

16         <activity

17             android:name="com.mars.activity06.FirstActivity"

18             android:label="@string/app_name" >

19             <intent-filter>

20                 <action android:name="android.intent.action.MAIN" />

21 

22                 <category android:name="android.intent.category.LAUNCHER" />

23             </intent-filter>

24         </activity>

25         <!--        Activity        <activity/>    Activity                name lable name     Activity             lable     -->

26         <activity android:name="com.mars.activity06.SecondActivity" android:label="@string/second"/>

27         <activity android:name="com.mars.activity06.ThirdActivity" android:label="@string/third"/>"

28     </application>

29 

30 </manifest>

 string.xml
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <resources>

 3 

 4     <string name="app_name">Activity06</string>

 5     <string name="action_settings">Settings</string>

 6     <string name="first">FirstActivity</string>

 7     <string name="firstButton">FirstButton</string>

 8     <string name="second">SecondActivity</string>

 9     <string name="secondtButton">SecondButton</string>

10     <string name="third">ThirdActivity</string>

11     <string name="thirdtButton">ThirdButton</string>

12 

13 </resources>

 
 
 
Activity間でIntentオブジェクトを介してデータを渡すことができます
putExtra()シリーズメソッドを使用してIntentオブジェクトにデータを格納する
getXXXXExtra()シリーズメソッドを使用してIntentオブジェクトからデータを取り出す
MainActivity.java
 1 import android.app.Activity;

 2 import android.content.Intent;

 3 import android.os.Bundle;

 4 import android.view.View;

 5 import android.view.View.OnClickListener;

 6 import android.widget.Button;

 7 

 8 public class MainActivity extends Activity {

 9     //         

10     private Button myButton;

11     @Override

12     //       onCreate  ,Activity             

13     protected void onCreate(Bundle savedInstanceState) {

14         super.onCreate(savedInstanceState);

15         setContentView(R.layout.main);

16         

17         myButton = (Button) findViewById(R.id.myButton);

18         myButton.setOnClickListener(new myButtonListener());

19         

20     }

21     //        ,              (           ,                )

22     class myButtonListener implements OnClickListener{

23 

24         //       ,         。          ,    onClick   

25         public void onClick(View v) {

26             

27             Intent intent = new Intent();//    Intent  

28             

29             intent.putExtra("number", "13112266075");// Intent           ,          Activity             ,    .    ,         ,      

30             

31             //  Intent      Activity,     this,     this         ,   myButtonListener    

32             /*

33              * setClass           Context  

34              * Context    ,Activity Context    ,    ,   Activity  ,        Context  

35              * */

36             intent.setClass(MainActivity.this, OtherActivity.class);

37             //  Intent        Activity

38             MainActivity.this.startActivity(intent);

39             

40             

41             

42             /*

43             //   4           Activity,

44             Uri uri = Uri.parse("smsto://0800000123");    

45             Intent intent = new Intent(Intent.ACTION_SENDTO, uri);    

46             intent.putExtra("sms_body", "The SMS text");    

47             startActivity(intent);

48          */

49         }

50     }

51 }

 main.xml
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 2     xmlns:tools="http://schemas.android.com/tools"

 3     android:orientation="vertical"

 4     android:layout_width="match_parent"

 5     android:layout_height="match_parent"

 6     android:paddingBottom="@dimen/activity_vertical_margin"

 7     android:paddingLeft="@dimen/activity_horizontal_margin"

 8     android:paddingRight="@dimen/activity_horizontal_margin"

 9     android:paddingTop="@dimen/activity_vertical_margin"

10     tools:context=".MainActivity" >

11 

12     <!--           Buttin(  )  ,         ID,  ID     R.java     -->

13     <Button

14         android:id="@+id/myButton"

15         android:layout_width="fill_parent"

16         android:layout_height="wrap_content"

17         android:text="@string/Button"

18         />

19 

20 </LinearLayout>

 OtherActivity.java
 1 import android.app.Activity;

 2 import android.content.Intent;

 3 import android.os.Bundle;

 4 import android.widget.TextView;

 5 

 6 public class OtherActivity extends Activity{

 7     private TextView myTextView;

 8     protected void onCreate(Bundle savedInstanceState) {

 9         

10         super.onCreate(savedInstanceState);

11         setContentView(R.layout.other);

12         //      Activity       Intent  

13         Intent intent = getIntent();

14         // Intent    key  value

15         String number = intent.getStringExtra("number");//          ,    ,       Activity      ,               number    

16         

17         myTextView = (TextView) findViewById(R.id.myTextView);

18         myTextView.setText(number);

19         

20     }

21     

22 }

 other.xml
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3     android:layout_width="match_parent"

 4     android:layout_height="match_parent"

 5     android:orientation="vertical" >

 6     

 7     

 8     <TextView

 9         android:id="@+id/myTextView"

10         android:layout_width="fill_parent"

11         android:layout_height="wrap_content"

12         android:text="@string/TextView"

13         />"

14 

15 </LinearLayout>

 AndriodManifest.xml
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"

 3     package="com.mars.conver_activity"

 4     android:versionCode="1"

 5     android:versionName="1.0" >

 6 

 7     <uses-sdk

 8         android:minSdkVersion="8"

 9         android:targetSdkVersion="18" />

10 

11     <application

12         android:allowBackup="true"

13         android:icon="@drawable/ic_launcher"

14         android:label="@string/app_name"

15         android:theme="@style/AppTheme" >

16         <activity

17             android:name="com.mars.conver_activity.MainActivity"

18             android:label="@string/app_name" >

19             <intent-filter>

20                 <action android:name="android.intent.action.MAIN" />

21 

22                 <category android:name="android.intent.category.LAUNCHER" />

23             </intent-filter>

24         </activity>

25         <activity android:name="com.mars.conver_activity.OtherActivity" android:label="@string/OtherActivity"/>"

26     </application>

27 

28 </manifest>

 string.xml
<?xml version="1.0" encoding="utf-8"?>

<resources>



    <string name="app_name">Activity  </string>

    <string name="action_settings">Settings</string>

    <string name="Button">start next Button</string>

    <string name="TextView">I am coming</string>

    <string name="OtherActivity">OtherActivity</string>

    



</resources>

 
 
 
MainActivity.java
 1 import android.app.Activity;

 2 import android.content.Intent;

 3 import android.os.Bundle;

 4 import android.view.View;

 5 import android.view.View.OnClickListener;

 6 import android.widget.Button;

 7 

 8 public class MainActivity extends Activity {

 9     private Button button;

10     protected void onCreate(Bundle savedInstanceState) {

11         super.onCreate(savedInstanceState);

12         setContentView(R.layout.activity_main);

13         

14         button = (Button) findViewById(R.id.button);

15         button.setOnClickListener(new ButtonListener());

16     }

17     class ButtonListener implements OnClickListener{

18         public void onClick(View v) {

19             Intent intent = new Intent();

20             intent.putExtra("com.mars.second_intent.Age", 20);

21             intent.putExtra("com.mars.second_intent.Number", "13112266075");

22             intent.setClass(MainActivity.this, OtherActivity.class);

23             MainActivity.this.startActivity(intent);

24         }

25     } 

26 }

 
activity_main.xml
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

 2     xmlns:tools="http://schemas.android.com/tools"

 3     android:layout_width="match_parent"

 4     android:layout_height="match_parent"

 5     android:paddingBottom="@dimen/activity_vertical_margin"

 6     android:paddingLeft="@dimen/activity_horizontal_margin"

 7     android:paddingRight="@dimen/activity_horizontal_margin"

 8     android:paddingTop="@dimen/activity_vertical_margin"

 9     tools:context=".MainActivity" >

10 

11     <TextView

12         android:id="@+id/textview1"

13         android:layout_width="wrap_content"

14         android:layout_height="wrap_content"

15         android:text="@string/hello_world" />

16     

17     <Button 

18         android:id="@+id/button"

19         android:layout_width="wrap_content"

20         android:layout_height="wrap_content"

21         android:layout_below="@id/textview1"

22         android:text="     Activity"

23         />

24 

25 </RelativeLayout>

OtherActivity.java
 1 import android.app.Activity;

 2 import android.content.Intent;

 3 import android.os.Bundle;

 4 import android.widget.TextView;

 5 

 6 public class OtherActivity extends Activity{

 7     private TextView textview;

 8     protected void onCreate(Bundle savedInstanceState) {

 9         super.onCreate(savedInstanceState);

10         setContentView(R.layout.other);

11         Intent intent = getIntent();

12         String str = intent.getStringExtra("com.mars.second_intent.Number");

13         int age = intent.getIntExtra("com.mars.second_intent.Age", 10);

14         

15         textview = (TextView) findViewById(R.id.textview);

16         textview.setText("Number="+str+"age="+age);

17     }

18 }

 
other.xml
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3     android:layout_width="match_parent"

 4     android:layout_height="match_parent"

 5     android:orientation="vertical" >

 6     

 7     

 8     <TextView 

 9         android:id="@+id/textview"

10         android:layout_width="wrap_content"

11         android:layout_height="wrap_content"

12         android:text="zhognguo"

13         />

14 

15 </LinearLayout>

 
 AndriodManifest.xml
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"

 3     package="com.mars.second_intent"

 4     android:versionCode="1"

 5     android:versionName="1.0" >

 6 

 7     <uses-sdk

 8         android:minSdkVersion="17"

 9         android:targetSdkVersion="18" />

10 

11     <application

12         android:allowBackup="true"

13         android:icon="@drawable/ic_launcher"

14         android:label="@string/app_name"

15         android:theme="@style/AppTheme" >

16         <activity

17             android:name="com.mars.second_intent.MainActivity"

18             android:label="@string/app_name" >

19             <intent-filter>

20                 <action android:name="android.intent.action.MAIN" />

21 

22                 <category android:name="android.intent.category.LAUNCHER" />

23             </intent-filter>

24         </activity>

25         

26         <activity 

27             android:name="com.mars.second_intent.OtherActivity"

28             android:label="   Activity"

29             ></activity>

30     </application>

31 

32 </manifest>

 
string.xml
<?xml version="1.0" encoding="utf-8"?>

<resources>



    <string name="app_name">Second_Intent</string>

    <string name="action_settings">Settings</string>

    <string name="hello_world">Hello world!</string>



</resources>