当前位置: 代码迷 >> Android >> android的容易理解(原创)
  详细解决方案

android的容易理解(原创)

热度:30   发布时间:2016-05-01 13:56:27.0
android的简单理解(原创)

学习了两天的android的,仅仅是简单的入门


但是,对于android有了一点了解,作为自己的笔记吧~


1.环境搭建:用到了jdk(配置环境)、eclipse、sdk、adt
?????? adt是eclipse的一个插件,百度上这么说:“在Eclipse编译IDE环境中,安ADT,为Android开发提供开发工具的升级或者变更,简单理解为在Eclipse下开发工具的升级下载工具。”

?

?

?

????? sdk是一般是一些被软件工程师用于为特定的软件包、软件框架、硬件平台、操作系统等建立应用软件的开发工具的集合。
????? 在Android中,他为开发者提供了库文件以及其他开发所用到的工具。简单理解为开发工具包集合,是整体开发中所用到的工具包,
????? 如果你不用Eclipse作为你的开发工具,你就不需要下载ADT,只下载SDK即可开发。

?

2.搭建好了环境之后,就可以开发了
? 2.1新建立一个android project,这个project里有几个地方:
????? =》R.java这个我们不用动,我们在string.xml和main.xml里定义的变量就会被自动加载到这个里面,
????????? public static final class string
????????? public static final class id
????????? 所有的都是public static final int
类型的,已经将其他的类型转为int类型的代码如:
????????? public static final int button_pause=0x7f050002;
????????? #######################

下面是音乐播放器的R.java代码:

package mymusicplay.aubergine;

public final class R {
??? public static final class attr {
??? }
??? public static final class drawable {
??????? public static final int ic_launcher=0x7f020000;
??? }
??? public static final class id {
??????? public static final int button_pause=0x7f050002;
??????? public static final int button_play=0x7f050001;
??????? public static final int button_reset=0x7f050003;
??????? public static final int button_stop=0x7f050004;
??????? public static final int filename=0x7f050000;
??? }
??? public static final class layout {
??????? public static final int main=0x7f030000;
??? }
??? public static final class string {
??????? public static final int app_name=0x7f040001;
??????? public static final int button_continue=0x7f040006;
??????? public static final int button_pause=0x7f040005;
??????? public static final int button_play=0x7f040004;
??????? public static final int button_reset=0x7f040007;
??????? public static final int button_stop=0x7f040008;
??????? public static final int filename=0x7f040002;
??????? public static final int filenoexist=0x7f040003;
??????? public static final int hello=0x7f040000;
??? }
}


? 2.2我们需要做的是:
????? 在main.xml设置页面的布局
????? 在string.xml里定义我们会用到的文本类型的数据(如按钮上的显示的字)
????? 在src下的java文件里写业务逻辑,android里的就是一个activity到另一个activity,但是,简单的就是一个activity实习的
??????? 每个activity都有自己的生命周期,每个生命周期会触发不同的方法。

#############################

下面是main.xml的数据

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
??? android:layout_width="fill_parent"
??? android:layout_height="fill_parent"
??? android:orientation="vertical" >

??? <TextView
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:text="@string/filename" />

??? <EditText
??????? android:id="@+id/filename"
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:text="Lindsey Ray - Better Off.mp3" />

??? <LinearLayout
??????? android:layout_width="fill_parent"
??????? android:layout_height="wrap_content"
??????? android:orientation="horizontal" >

??????? <Button
??????????? android:id="@+id/button_play"
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="wrap_content"
??????????? android:text="@string/button_play" />

??????? <Button
??????????? android:id="@+id/button_pause"
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="wrap_content"
??????????? android:text="@string/button_pause" />

??????? <Button
??????????? android:id="@+id/button_reset"
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="wrap_content"
??????????? android:text="@string/button_reset" />

??????? <Button
??????????? android:id="@+id/button_stop"
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="wrap_content"
??????????? android:text="@string/button_stop" />
??? </LinearLayout>

</LinearLayout>

?

#############################

下面是string.xml的数据

<?xml version="1.0" encoding="utf-8"?>
<resources>

??? <string name="hello">Hello World, MainActivity!</string>
??? <string name="app_name">AudioPlayer</string>
??? <string name="filename">请输入音乐文件名:</string>
??? <string name="filenoexist">音乐文件不存在</string>
??? <string name="button_play">播放</string>
??? <string name="button_pause">暂停</string>
??? <string name="button_continue">继续</string>
??? <string name="button_reset">重播</string>
??? <string name="button_stop">停止</string>

</resources>

?

最后是音乐播放器的代码:

package mymusicplay.aubergine;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyMusicPlayerActivity extends Activity {
?private EditText filenameText;
?private MediaPlayer mediaPlayer;
?private String path;
?private boolean pause;
?private int position;

[email protected]
?public void onCreate(Bundle savedInstanceState) {

??// 程序自动生成
??super.onCreate(savedInstanceState);
??setContentView(R.layout.main);

??// 获得activity中的对象
??filenameText = (EditText) this.findViewById(R.id.filename);
??Button playButton = (Button) this.findViewById(R.id.button_play);
??Button pauseButton = (Button) this.findViewById(R.id.button_pause);
??Button resetButton = (Button) this.findViewById(R.id.button_reset);
??Button stopButton = (Button) this.findViewById(R.id.button_stop);

??// 创建音乐播放器
??mediaPlayer = new MediaPlayer();

??// 创建按钮点击事件的监听
??ButtonClickListener listener = new ButtonClickListener();

??// 将监听事件放上去
??playButton.setOnClickListener(listener);
??pauseButton.setOnClickListener(listener);
??resetButton.setOnClickListener(listener);
??stopButton.setOnClickListener(listener);
?}

?/**
? * 当播放器处于后台时会触发此生命周期方法
? */
[email protected]
?protected void onPause() {
??if (mediaPlayer.isPlaying()) {
???position = mediaPlayer.getCurrentPosition();// 记录当前音乐播放器播放到的位置
???mediaPlayer.stop();// 停止播放(其实是暂停)
??}
??super.onPause();
?}

?/**
? * 当播放器重新回到前台时会触发此生命周期方法
? */
[email protected]
?protected void onResume() {
??if (path != null && position > 0) {
???play(position);// 从暂停的位置开始接着播放
???position = 0;// 并将位置设置为开始
??}
??super.onResume();
?}

?/**
? * 当播放器关闭时触发此生命周期方法
? */
[email protected]
?protected void onDestroy() {
??// 释放资源
??mediaPlayer.release();
??super.onDestroy();
?}

?private final class ButtonClickListener implements OnClickListener {
??// 需要覆盖的方法
??public void onClick(View v) {
???switch (v.getId()) {
???// 播放按钮
???case R.id.button_play:
????String filename = filenameText.getText().toString();
????File file = new File(Environment.getExternalStorageDirectory(),
??????filename);// 正在播放的音乐文件
????if (file.exists()) {
?????path = file.getAbsolutePath();
?????play(0);
????} else {
?????path = null;
?????Toast.makeText(getApplicationContext(),
???????R.string.filenoexist, Toast.LENGTH_LONG).show();
????}
????break;

???case R.id.button_pause:
????if (mediaPlayer.isPlaying()) {
?????mediaPlayer.pause();
?????((Button) v).setText(R.string.button_continue);
?????pause = true;
????} else {
?????((Button) v).setText(R.string.button_pause);
?????if (pause) {
??????mediaPlayer.start();
??????pause = false;
?????}
????}
????break;
???case R.id.button_reset:
????if (mediaPlayer.isPlaying()) {
?????mediaPlayer.seekTo(0);
????} else {
?????if (path != null) {
??????play(0);
?????}
????}
????break;
???case R.id.button_stop:
????if (mediaPlayer.isPlaying()) {
?????mediaPlayer.stop();
????}
????break;
???}

??}
?}

?/**
? * 播放音乐
? *
? * @param pos
? */
?public void play(int pos) {
??try {
???mediaPlayer.reset();
???mediaPlayer.setDataSource(path);
???mediaPlayer.setOnPreparedListener(new PreparedListener(pos));
???mediaPlayer.prepare();
??} catch (Exception e) {
???e.printStackTrace();
??}
?}

?/**
? * 监听音乐文件缓冲完成的监听
? *
? * @author?k
? *
? */
?private final class PreparedListener implements OnPreparedListener {

??private int pos;

??public PreparedListener(int pos) {
???this.pos = pos;
??}

??public void onPrepared(MediaPlayer mp) {
???mediaPlayer.start(); // 开始播放音乐
???if (pos > 0) {
????mediaPlayer.seekTo(pos);
???}
??}

?}
}

?

?

注意:

这样我们在工程上选择=》run android application就会将我们的工程部署上去,然后,

弹出界面,在点击主菜单在主菜单就看到了我们的应用名称为“AudioPlayer”的应用图标,上面的string.xml中的appname可以配置其他名称。
???????

?

具体参看附件:

  相关解决方案