(Android第一行代码) 使用 Intent 在活动之间穿梭

 Intent:

   一:使用显式Intent.

         由主活动跳转到其他活动.

  使用步骤:

           first_layout.xml

              <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/button_1"       

                           android:layout_width="match_parent"        

                           android:layout_height="wrap_content"   

                           android:text="Button 1"        

                              /> 

    </LinearLayout>

              public class firstActivity extends Activity {  

                       @Override 

               protected void onCreate(Bundle savedInstanceState) {

                       super.onCreate(savedInstanceState);  

                       requestWindowFeature(Window.FEATURE_NO_TITLE);  

                      setContentView(R.layout.first_layout); 

                       Button button1 = (Button) findViewById(R.id.button_1);

                       button1.setOnClickListener(new OnClickListener() {  

                       @Override

                      public void onClick(View v){

     } 

     });

  }

          1. 新建一个 second_layout.xml

                 <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/button_2"       

                           android:layout_width="match_parent"        

                           android:layout_height="wrap_content"   

                           android:text="Button 2"        

                              /> 

    </LinearLayout>

               2.新建活动 SecondActivity继承自 Activity。

                public class SecondActivity extends Activity {  

                       @Override 

               protected void onCreate(Bundle savedInstanceState) {

                       super.onCreate(savedInstanceState);  

                       requestWindowFeature(Window.FEATURE_NO_TITLE);  

                      setContentView(R.layout.second_layout);  }
}

              3.最后在 AndroidManifest.xml中为 SecondActivity进行注册。

           <application  

                   android:allowBackup="true"    

                   android:icon="@drawable/ic_launcher"    

                   android:label="@string/app_name" 

                   android:theme="@style/AppTheme" >   

                   <activity   

                         android:name=".FirstActivity"  

                         android:label="This is FirstActivity" >      

                         <intent-filter>          

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

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

                          </intent-filter> 

               </activity>    

               <activity android:name=".SecondActivity" > 

               </activity>

       </application>

          第二个活动已经创建完成.

          4.用显示Intent去启动这第 二个活动.

             修改 FirstActivity中按钮的点击事件,

                button1.setOnClickListener(new OnClickListener() {

                       @Override 

                 public void onClick(View v) {

                  //我们首先构建出了一个 Intent,传入 FirstActivity.this作为上下文,传入 SecondActivity.class 作为目标活动

                      Intent intent = new Intent(FirstActivity.this, SecondActivity.class);

                      startActivity(intent);

                   }

             });


二.使用隐式 Intent

    使用步骤:(相比于显式 Intent,隐式 Intent 则含蓄了许多,它并不明确指出我们想要启动哪一个活 动,而是指定了一系列更为抽象的action和category等信息,然后交由系统去分析这个Intent, 并帮我们找出合适的活动去启动。)

        1.通过在<activity>标签下配置<intent-filter>的内容,可以指定当前活动能够响应的 action 和 category。

          打开 AndroidManifest.xml

          <activity android:name=".SecondActivity" >   

               <intent-filter>      

                 //只有<action>和<category>中的内容同时能够匹 配上 Intent中指定的 action和 category时,这个活动才能响应该 Intent。

                        <action android:name="com.example.activitytest.ACTION_START" />   

                //android.intent.category.DEFAULT 是一种默认的 category 

                        <category android:name="android.intent.category.DEFAULT" />

                  </intent-filter>

            </activity>

        2.修改 FirstActivity中按钮的点击事件

             button1.setOnClickListener(new OnClickListener() {

                @Override

             public void onClick(View v) {  

                //表 明我们想要启动能够响应 com.example.activitytest.ACTION_START 这个 action的活动

                         Intent intent = new Intent("com.example.activitytest.ACTION_START");  

                        startActivity(intent);

                    }

              });

 


三   更多隐式 Intent 的用法

  使用隐式 Intent,我们不仅可以启动自己程序内的活动,还可以启动其他程序的活动

1.比如说你的应用程序中需要展示一个网页,这时你没有必要自己去实现一个浏览器(事实上也不太可能),而是只需要调用 系统的浏览器来打开这个网页就行了。

               button1.setOnClickListener(new OnClickListener() { 

                    @Override

                public void onClick(View v) { 

             //这里我们首先指定了 Intent的 action是 Intent.ACTION_VIEW,这是一个 Android系统内 置的动作,其常量值为 android.intent.action.VIEW

               Intent intent = new Intent(Intent.ACTION_VIEW);  

               intent.setData(Uri.parse("http://www.baidu.com"));  

               startActivity(intent); 

          } });

  2.我们的程序中利用tel调用系统拨号界面。

            button1.setOnClickListener(new OnClickListener() { 

                  @Override

              public void onClick(View v) { 

              Intent intent = new Intent(Intent.ACTION_DIAL); 

              intent.setData(Uri.parse("tel:10086")); 

              startActivity(intent); 

              }

            });

四 向下一个活动传递数据

    Intent中提供了一系列 putExtra()方法的重载,可 以把我们想要传递的数据暂存在 Intent 中,启动了另一个活动后,只需要把这些数据再从 Intent 中取出就可以了

  1.。比如说 FirstActivity 中有一个字符串,现在想把这个字符串传递到 SecondActivity

button1.setOnClickListener(new OnClickListener() { 

            @Override 

         public void onClick(View v) {  

         String data = "Hello SecondActivity"; 

         Intent intent = new Intent(FirstActivity.this, SecondActivity.class); 

      //使用了putExtra.传数据。

         intent.putExtra("extra_data", data);  

         startActivity(intent); 

        }

});

2. 然后我们在 SecondActivity中将传递的数据取出。

public class SecondActivity extends Activity { 

          @Override

        protected void onCreate(Bundle savedInstanceState) { 

               super.onCreate(savedInstanceState);

               requestWindowFeature(Window.FEATURE_NO_TITLE);  

               setContentView(R.layout.second_layout);  

         //使用getIntent获得intent对象

               Intent intent = getIntent(); 

         //使用getStringExtra(键值)得到传递过来的数据。

               String data = intent.getStringExtra("extra_data"); 

              Log.d("SecondActivity", data);  }
 
}

五.返回数据给上一个活动

   使用 startActivityForResult()这个方法期望在活动销毁的时候能够返回一个结果给上一个活 动。

1.修改 FirstActivity 中按钮 的点击事件。

   button1.setOnClickListener(new OnClickListener() { 

          @Override 

       public void onClick(View v) { 

            Intent intent = new Intent(FirstActivity.this, SecondActivity.class); 

    //startActivityForResult()方法接收两个参数,第一个参数还是 Intent,第二个参数是请求 码1.

           startActivityForResult(intent, 1); 

        }

    });

2.由于我们是使用 startActivityForResult()方法来启动 SecondActivity的,在 SecondActivity 被销毁之后会回调上一个活动的 onActivityResult()方法,因此我们需要在 FirstActivity 中重 写这个方法来得到返回的数据。

  @Override

 //参数requestCode即我们在启动活动时传 入的请求码1.

 //第二个参数 resultCode,即我们在SecondActivtiy返回数据时传入的处理结果

//第三个参数 data, 即携带着返回数据的 Intent

  protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

             switch (requestCode) {

            case 1:  

                    if (resultCode == RESULT_OK) { 

                        String returnedData = data.getStringExtra("data_return");   

                       Log.d("FirstActivity", returnedData);  

                   } 

                break; 

               default: 

           }

    }

3.接下来我们在 SecondActivity 中给按钮注册点击事件,并 在点击事件中添加返回数据的逻辑.

  public class SecondActivity extends Activity { 

          @Override 

        protected void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState); 

              requestWindowFeature(Window.FEATURE_NO_TITLE);

             setContentView(R.layout.second_layout); 

             Button button2 = (Button) findViewById(R.id.button_2); 

             button2.setOnClickListener(new OnClickListener() {

                     @Override  

                 public void onClick(View v) { 

                            Intent intent = new Intent(); 

                   //我们还是构建了一个 仅仅是用于传递数据Intent它没有指定任何的“意图”。

                            intent.putExtra("data_return", "Hello FirstActivity");    

                  /*setResult()方法专门用于向上一个活动返回数据的。

                      第一个参数用于向上一个活动返回处理结果一般只使用 RESULT_OK 或 RESULT_CANCELED 这两个值

                      第二个参数则是把带有数据的 Intent 传递回去

                      然后调用 了 finish()方法来销毁当前活动。*/

                             setResult(RESULT_OK, intent);   

                             finish();   

                     }  

                }); 

                    } 
           }

     SecondActivity已经成功返回数据给 FirstActivity了。

 4.如果用户在 SecondActivity中并不是通过点击按钮,而是通过按下 Back键回到 FirstActivity.,我们需要重写 onBackPressed()方法,

            @Override

        public void onBackPressed() { 

            Intent intent = new Intent();

            intent.putExtra("data_return", "Hello FirstActivity");

           setResult(RESULT_OK, intent); 

          finish();

 }

这样的话,当用户按下 Back 键,就会去执行 onBackPressed()方法中的代码,我们在这 里添加返回数据的逻辑就行了。

 

 

           

 

原文地址:https://www.cnblogs.com/weichenji/p/6099854.html