关闭

Activity生命周期应用实例

标签: 生命周期androidActivity应用实例
217人阅读 评论(0) 收藏 举报
分类:

 了解了Activity的生命周期,下面我们写一个实例更进一步的学习怎么使用生命周期。

一  需求:

(1)有两个Activity,MainActivity和ActivityTwo  ,其中MainActivity中有一个按钮btnSkip,

(2)启动MainActivity之后播放音乐

(3)点击btnSkip跳转到ActivityTwo  ,此时音乐播放暂停

(4)按Back返回MainActivity,音乐开始播放

(5)再按Back退出MainActivity,销毁播放器

二  实现

(1)首先我们需要在工程目录的res文件夹下面新建一raw文件夹,然后把一个音频文件粘贴到此文件夹下,如图所示


(2)正式编写代码 主要是MainActivity.java

package com.example.activitytest;

import android.app.Activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
	private Button  btnSkip;
	private MediaPlayer mediaPlayer;
	private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSkip = (Button)this.findViewById(R.id.btnButton);
        if(savedInstanceState!=null){
        	String str = savedInstanceState.getString("name");
        }
        Log.e("TAG", "MainActivity onCreate() method is executed");
        mediaPlayer = MediaPlayer.create(this,R.raw.huoyuanjia);//获取了音频文件的地址信息
        mediaPlayer.start();
        btnSkip.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,ActivityTwo.class);
				startActivity(intent);
			}
		});
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    	// TODO Auto-generated method stub
    	super.onSaveInstanceState(outState);
    	outState.putString("name", "zhuminquan");
    	Log.e("TAG", "MainActivity onSaveInstanceState() method is executed");
    }
    
    
    @Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.e("TAG", "MainActivity onStart() method is executed");
	}
    
    @Override
    protected void onRestart() {
    	// TODO Auto-generated method stub
    	if(position!=0){
    		mediaPlayer.seekTo(position);
    		mediaPlayer.start();
    	}
    	super.onRestart();
    	Log.e("TAG", "MainActivity onRestart() method is executed");
    }
    
    @Override
    protected void onPause() {
    	// TODO Auto-generated method stub
    	if(mediaPlayer.isPlaying()){
    		mediaPlayer.pause();
    		position = mediaPlayer.getCurrentPosition();
    	}
    	super.onPause();
    	Log.e("TAG", "MainActivity onPause() method is executed");
    }
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	Log.e("TAG", "MainActivity onResume() method is executed");
    }
    
    @Override
    protected void onStop() {
    	// TODO Auto-generated method stub
    	super.onStop();
    	Log.e("TAG", "MainActivity onStop() method is executed");
    }
    
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	Log.e("TAG", "MainActivity onDestroy() method is executed");
    	if(mediaPlayer!=null){
    		mediaPlayer.release();
    		mediaPlayer = null;
    	}
    }


}





1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6333次
    • 积分:357
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条