实验三 Task 打电话界面跳转


一、实验目的


    理解Task的含义




二、实验内容


    该应用的activity1有三个按钮,一个按钮跳转到Activity2,一个按钮跳转到直接拨打电话的页面,最后一个跳转到拨号界面,打电话这个功能不属于本应用,我们假设它为应用一。
    首先默认进入本应用的activity1,点击button1后跳转到activity2,再点击activity2的按钮后跳转到activity1,再点击button2跳转到直接拨打电话页面,点击返回,再点击button3跳转到拨号界面,最后依次点返回按钮,大家看Task栈效果。




三、实验结果图


    最后依次返回的顺序是我们所打开的界面的反顺序,可见Task栈先进后出、后进先出的特点。


android activity Dialog样式底部弹出 activity界面跳转_android

android activity Dialog样式底部弹出 activity界面跳转_ide_02

android activity Dialog样式底部弹出 activity界面跳转_xml_03

android activity Dialog样式底部弹出 activity界面跳转_xml_04




四、实验代码


FirstActivity:


package com.test3.task;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class FirstActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_first);		
	}
	
	public void toSecond(View view){
		Intent intent = new Intent (this,SecondActivity.class);
		startActivity(intent);
	}
	
	public void toCall(View view){
		Intent intentPhone = new Intent(Intent.ACTION_DIAL);
		intentPhone.setData(Uri.parse("tel:10086"));
		startActivity(intentPhone);
	}
	
	public void toDial(View view){
		Intent intentDail= new Intent(Intent.ACTION_DIAL);  
		intentDail.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);		
		startActivity(intentDail); 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.first, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

SecondActivity:


package com.test3.task;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class SecondActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
	}

	public void toFirst(View view){
		Intent intent = new Intent (this,FirstActivity.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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

activity_first.xml:


<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="com.test3.task.FirstActivity" >

    <Button
        android:id="@+id/btntoSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="118dp"
        android:onClick="toSecond"
        android:text="@string/to_Second" />

    <Button
        android:id="@+id/btntoCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="toCall"
        android:text="@string/to_call" />

    <Button
        android:id="@+id/btntoDial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btntoCall"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:onClick="toDial"
        android:text="@string/to_dial" />

</RelativeLayout>

activity_second.xml:


<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="com.test3.task.SecondActivity" >

    <Button
        android:id="@+id/btntoFirst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="toFirst"
        android:text="@string/to_First" />

</RelativeLayout>


五、存在的问题

    跳转至拨号界面有很多种方法,我试了好几种,可是只有我写出的这种在我的模拟器上可以正确运行。我参考的大神说的那些方法不知道问题出在哪,还需要进一步研究。






实验四 Activity的生命周期


一、实验目的


(1)观察Activity生命周期经历的主要方法

(2)如果使用LogCat观察输出,掌握如何设置LogFilter

(3)观察何时调用onPause方法?

(4)观察何时调用onStop方法?

二、实验内容


    编写两种不同的Activity(NormalActivity、DialogActivity),通过跳转至不同的Activity来观察MainActivity的生命周期,注意个方法调用的时间。


三、实验结果图


android activity Dialog样式底部弹出 activity界面跳转_xml_05

android activity Dialog样式底部弹出 activity界面跳转_xml_06

android activity Dialog样式底部弹出 activity界面跳转_ide_07






以下是过程:打开应用->点击Start NormalActivity->返回->点击Start DialogActivity->返回->退出应用,输出的info,可见,在两次点击按钮第二次返回时调用了onPause()函数,在打开NormalActivity结束应用是调用了onStop()函数。充分展示了Activity生命周期的两个分支。


android activity Dialog样式底部弹出 activity界面跳转_xml_08




三、实验代码




MainActivity:


package com.test4.lifecycle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.i("MainActivity","onCreat");
	}
	
	public void toNormal(View view){
		Intent intentNormal = new Intent (this, NormalActivity.class);
		startActivity(intentNormal);
	}
	
	public void toDialog(View view){
		Intent intentDialog = new Intent (this, DialogActivity.class);
		startActivity(intentDialog);
	}	

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("MainActivity","onDestroy");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.i("MainActivity","onPause");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.i("MainActivity","onRestart");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.i("MainActivity","onResume");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.i("MainActivity","onStart");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	Log.i("MainActivity","onStop");
	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


NormalActivity和 DialogActivity并没有进行改动。



activity_main:


<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="com.test4.lifecycle.MainActivity" >

    <Button
        android:id="@+id/toNormal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="136dp"
        android:onClick="toNormal"
        android:text="@string/toNormal" />

    <Button
        android:id="@+id/toDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toNormal"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp"
        android:onClick="toDialog"
        android:text="@string/toDialog" />

</RelativeLayout>


activity_normal:


<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="com.test4.lifecycle.NormalActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Normal_output"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>


activity_dialog:


<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="com.test4.lifecycle.DialogActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Dialog_output" 
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>


AndroidManifest.xml(清单文件中的一部分):


<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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=".NormalActivity"
            android:label="@string/title_activity_normal" >
        </activity>
        <activity
            android:name=".DialogActivity"
            android:theme="@android:style/Theme.Dialog"
            android:label="@string/title_activity_dialog" >
        </activity>
    </application>

五、存在的问题


    虽然知道了Activity的生命周期的两个分支,但是除了这个例子外,不知道还有什么其他的触发另一种分支的条件,对于Activity的生命周期的了解不是很深入透彻,需要继续花时间。




PS:本篇的具体知识点会在自学笔记中讲解