`
Aubergine_kang
  • 浏览: 260156 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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;

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

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

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

 /**
  * 当播放器关闭时触发此生命周期方法
  */
 @Override
 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可以配置其他名称。
       

 

具体参看附件:

  • 大小: 102.2 KB
  • 大小: 102.2 KB
分享到:
评论

相关推荐

    Android深入理解Activity!

    Android深入理解Activity,源于《疯狂Android讲义》

    android context理解

    android context理解

    android深入理解Android_卷 二

    android深入理解Android_卷 二,有了这本书,可以把其它所有的android书扔掉了

    深入理解Android(卷3)(带完整书签版).张大伟.pdf

    经典畅销书系“深入理解Android”系列Framework卷完结篇,数十万Android开发工程师翘首以盼  从源代码层面全面、详细剖析了Android 框架UI系统的实现原理和工作机制,以及优秀代码的设计思想,填补市场空白  要想...

    深入理解ANDROID卷3

    资源名称:深入理解ANDROID 卷3内容简介:深入理解Android(卷3)》是Android经典畅销书系(对Android系统源代码的分析最为系统和细致)“深入理解Android”系列Framework卷的第III卷,从源代码的角度,对Android...

    深入理解Android 卷II pdf

    《深入理解Android:卷2》是“深入理解Android”系列的第2本,第1本书上市后获得广大读者高度评价,在Android开发者社群内口口相传。本书不仅继承了第1本书的优点并改正了其在细微处存在的一些不足,而且还在写作的...

    深入理解Android内核设计思想.pdf

    深入理解Android内核设计思想.pdf

    android:对ndk的理解

    android:对andriod ndk的理解

    深入理解Android卷1和卷2.zip

    深入理解Android卷1:一本以情景方式对Android的源代码进行深入分析的书。内容广泛,以对Framework层的分析为主,兼顾Native层和Application层;分析深入,每一部分源代码的分析都力求透彻;针对性强,注重实际应用...

    android简易打地鼠.zip

    android简易打地鼠.zip android简易打地鼠.zip android简易打地鼠.zip android简易打地鼠.zip android简易打地鼠.zip android简易打地鼠.zip android简易打地鼠.zip android简易打地鼠.zip android简易打地鼠.zip ...

    Android简易版聊天室

    本程序包含3个模块,即Android手机客户端、PC服务器端、PC客户端; 能很好的实现3个窗口的通信。。

    深入理解android卷1

    深入理解android卷1,深入理解android卷1,深入理解android卷1

    《深度理解Android:第一卷》

    《深入理解Android:卷I》是一本以情景方式对Android的源代码进行深入分析的书。内容广泛,以对Framework层的分析为主,兼顾Native层和Application层;分析深入,每一部分源代码的分析都力求透彻;针对性强,注重...

    深入理解Android 卷I pdf

    《深入理解Android:卷1》是一本以情景方式对Android的源代码进行深入分析的书。内容广泛,以对Framework层的分析为主,兼顾Native层和Application层;分析深入,每一部分源代码的分析都力求透彻;针对性强,注重...

    《深入理解Android 卷III》PDF版本下载.txt

    《深入理解Android 卷III》PDF版本下载

    android之简易绘图板

    android实现的超简单的绘图板,没有使用双缓冲技术

    深入理解Android 卷二

    深入理解Android 卷二深入理解Android 卷二深入理解Android 卷二

    android 简易camera照相机

    android 简易camera相机,可以实现简单的拍照 & preview功能

    深入理解Android卷一、二、三

    深入理解android卷一、卷二、卷三系统的讲解了Android的启动、Binder、AMS、PWS、AudioFlinger、SurfaceFlinger、WIFI、BT和NFC知识点,适合android进阶的好书。

    深入理解Android(卷3) PDF

    经典畅销书系“深入理解Android”系列Framework卷完结篇,数十万Android开发工程师翘首以盼  从源代码层面全面、详细剖析了Android 框架UI系统的实现原理和工作机制,以及优秀代码的设计思想,填补市场空白  要想...

Global site tag (gtag.js) - Google Analytics