一、Activity的生命周期:
示例1:
Main.java
package com.wwtiot.ozee3; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Main extends Activity { private Button btnDial, btnPage2, btnFinish; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取资源文件中的界面组件 btnDial = (Button) findViewById(R.id.btnDial); btnPage2 = (Button) findViewById(R.id.btnPage2); btnFinish = (Button) findViewById(R.id.btnFinish); // 为3个按钮组件设置共享listener btnDial.setOnClickListener(myListener); btnPage2.setOnClickListener(myListener); btnFinish.setOnClickListener(myListener); Toast.makeText(getApplicationContext(), "执行onCreate(1)", Toast.LENGTH_SHORT).show(); } private Button.OnClickListener myListener = new Button.OnClickListener() { @Override public void onClick(View v) { if (v.getId() == R.id.btnFinish) { finish(); } else if (v.getId() == R.id.btnPage2) { Intent intent = new Intent(); intent.setClass(getApplicationContext(), Second.class); startActivity(intent); } else if (v.getId() == R.id.btnDial) { Uri uri = Uri.parse("tel:1234567"); Intent intent = new Intent(Intent.ACTION_DIAL, uri); startActivity(intent); } } }; @Override protected void onStart() { super.onStart(); Toast.makeText(getApplicationContext(), "执行onStart(1)", Toast.LENGTH_SHORT).show(); } @Override protected void onResume() { super.onResume(); Toast.makeText(getApplicationContext(), "执行onResume(1)", Toast.LENGTH_SHORT).show(); } @Override protected void onRestart() { super.onRestart(); Toast.makeText(getApplicationContext(), "执行onRestart(1)", Toast.LENGTH_SHORT).show(); } @Override protected void onPause() { super.onPause(); Toast.makeText(getApplicationContext(), "执行onPause(1)", Toast.LENGTH_SHORT).show(); } @Override protected void onStop() { super.onStop(); Toast.makeText(getApplicationContext(), "执行onStop(1)", Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { super.onDestroy(); Toast.makeText(getApplicationContext(), "执行onDestroy(1)", Toast.LENGTH_SHORT).show(); } @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; } }
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" 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=".Main" > <Button android:id="@+id/btnDial" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="调用拨号按钮" /> <Button android:id="@+id/btnPage2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="启动另一个自定义的Activity" /> <Button android:id="@+id/btnFinish" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="finish()结束" /> </LinearLayout>
Second.java
package com.wwtiot.ozee3; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Second extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); Button btnHome = (Button) findViewById(R.id.btnHome); btnHome.setOnClickListener(myListener); Toast.makeText(getApplicationContext(), "执行onCreate(2)", Toast.LENGTH_SHORT).show(); } private Button.OnClickListener myListener = new Button.OnClickListener() { @Override public void onClick(View v) { finish(); } }; @Override protected void onStart() { super.onStart(); Toast.makeText(getApplicationContext(), "执行onStart(2)", Toast.LENGTH_SHORT).show(); } @Override protected void onResume() { super.onResume(); Toast.makeText(getApplicationContext(), "执行onResume(2)", Toast.LENGTH_SHORT).show(); } @Override protected void onRestart() { super.onRestart(); Toast.makeText(getApplicationContext(), "执行onRestart(2)", Toast.LENGTH_SHORT).show(); } @Override protected void onPause() { super.onPause(); Toast.makeText(getApplicationContext(), "执行onPause(2)", Toast.LENGTH_SHORT).show(); } @Override protected void onStop() { super.onStop(); Toast.makeText(getApplicationContext(), "执行onStop(2)", Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { super.onDestroy(); Toast.makeText(getApplicationContext(), "执行onDestroy(2)", Toast.LENGTH_SHORT).show(); } @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; } }
second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" 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=".Second" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二个程序!" /> <Button android:id="@+id/btnHome" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="返回主程序" /> </LinearLayout>
示例2:
Main.java
package com.wwtiot.ozee4; import android.media.MediaPlayer; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; public class Main extends Activity { public MediaPlayer player = new MediaPlayer(); private Button btnPause, btnContinue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); player = MediaPlayer.create(getApplicationContext(), R.raw.moon_night); // 连续播放 player.setLooping(true); // 音乐播放 player.start(); // 获取资源文件中的界面组件 btnPause = (Button) findViewById(R.id.btnPause); btnContinue = (Button) findViewById(R.id.btnContinue); // 为Button组件设置共享listener btnPause.setOnClickListener(myListener); btnContinue.setOnClickListener(myListener); } private Button.OnClickListener myListener = new Button.OnClickListener() { @Override public void onClick(View v) { if (v.getId() == R.id.btnPause) { player.pause(); } if (v.getId() == R.id.btnContinue) { player.start(); } } }; // 音乐播放暂停 // @Override // protected void onPause() { // super.onPause(); // player.pause(); // } // 音乐继续播放 // @Override // protected void onResume() { // super.onResume(); // player.start(); // } // 释放 // @Override // protected void onDestroy() { // super.onDestroy(); // player.release(); // } @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; } }
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_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=".Main" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btnPause" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="暂停" /> <Button android:id="@+id/btnContinue" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="播放" /> </LinearLayout>