实验二_任务三_理解Task

本文介绍了一个具体的Android应用案例,展示了如何使用Activity和Intent实现页面间的跳转,并通过实例解释了Task的概念及其工作原理。

实验2 Activity Intent

1. 实验目的

1. 掌握如何创建一个Activity

2. 掌握如何在Activity中添加控件并进行事件处理

3. 掌握如何通过Intent启动另外一个Activity

4. 掌握如何通过Intent在多个Activity之间传递数据 

5. 掌握Activity的生命周期

2. 实验仪器

装有Android开发环境的计算机


任务3:理解Task

1.        运行效果图

 

    app1的activity1有两个按钮,一个按钮跳转到Activity2,另外一个按钮跳转到打电话的页面,打电话这个功能不属于app1,我们假设它为app2。

    首先默认进入app1的activity1,点击button1后跳转到activity2,再点击activity2的按钮后跳转到activity1,再点击button2跳转到打电话页面,最后依次点返回按钮,大家看Task栈效果。

2.      训练目标

理解Task的含义

主活动MainActivity:
[java] view plain copy
package com.example.thirdlab;   
import android.net.Uri;  
import android.os.Bundle;  
import android.app.Activity;  
import android.content.Intent;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
  
public class MainActivity extends Activity  
{  
    private Button button1;  
    private Button button2;  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        button1 = (Button)findViewById(R.id.but_no1);  
        button2 = (Button)findViewById(R.id.but_no2);  
        button1.setOnClickListener(new OnClickListener()  
        {  
              
            @Override  
            public void onClick(View arg0)  
            {  
                // TODO Auto-generated method stub  
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);  
                startActivity(intent);  
            }  
        });  
        button2.setOnClickListener(new OnClickListener()  
        {  
              
            @Override  
            public void onClick(View arg0)  
            {  
                // TODO Auto-generated method stub  
                Intent intent1=new Intent(Intent.ACTION_DIAL);  
                intent1.setData(Uri.parse("tel:10086"));  
                startActivity(intent1);  
            }  
        });  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu)  
    {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);  
        return true;  
    }  
  
}  




主活动布局:
[html] view plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  
  
    <Button   
        android:id="@+id/but_no1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/text_but1"  
        />  
    <Button   
        android:id="@+id/but_no2"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_below="@id/but_no1"  
        android:text="@string/text_but2"  
        />  
         
         
  
</RelativeLayout>  


第二个活动:
[java] view plain copy
package com.example.thirdlab;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.content.Intent;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
  
public class SecondActivity extends Activity  
{  
 private Button button;  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_second);  
        button = (Button) findViewById(R.id.but2_no1);  
        button.setOnClickListener(new OnClickListener()  
        {  
              
            @Override  
            public void onClick(View arg0)  
            {  
                // TODO Auto-generated method stub  
                Intent intent = new Intent(SecondActivity.this,MainActivity.class);  
                startActivity(intent);  
            }  
        });  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu)  
    {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.second, menu);  
        return true;  
    }  
  
}  


第二个布局:
[html] view plain copy
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".SecondActivity" >  
  
    <Button   
        android:id="@+id/but2_no1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/text_but3"  
        />  
  
</RelativeLayout>  

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值