android實習程序——音樂播放器(簡單播放)

做一下知識點的準備
android四大組件
actvity
service
廣播接收者
內容提供者


MVC
activity c
View     v

界面是如何呈現的?
Window
通過getWindow()方法來獲得窗口

將view讓入窗口呈現
setContentView(view);

xml是一種數據的封裝格式
字符串
如何把字符串轉換成view對象
反射技術--解析xml

在android中是用什麼解析的
Inflater
用LayoutInflater.from(this)來獲取Inflater

用inflate(R.layout.main, null);

-----------------------
在自定義view時需要傳入一個應用程序上下文對象

書---
書的封面--作者,出版社,發行日期。。。。。。

我在給你書的時候只撕給你書中的一頁紙,你能得到這些信息嗎?

把書頁和封面全部裝訂在一起就行

組合
---------------------------
如何給你張白紙,你如何將它變成美麗的畫


在android中的界面就是畫出來的
onDraw

Canvas畫布
Paint畫筆

所有的類名首字母大寫

總結:
activity
window
View
三者之間的關係?

Inflater的作用?

Context的作用?

如何自定義view?

---------------------------

Adapter的作用

做軟件就是要進行數據的處理
處理完了還要展示給用戶

在寫adapter要關注的兩點
數據源
界面
ArrayAdapter()
SimpleAdapter()

---------------------------
setContentView(R.layout.main)有什麼缺點
它用的是反射,一定會消耗資源,性能不好,不適合需要高性能的情況,也不適合複雜界面

遊戲

2個項目
1108班學員做的
簡單通訊錄
五子棋
---------------------------
要有良好的java基礎
空指針
java是面向對象的設計語言
是靠對象和類來調用方法來完成業務邏輯的。

service本身是和Activity在同一線程的
如果要使service能執行異步的操作必須在service中開啓新的線程。

那問題來了,爲什麼不在Activity中直接開啓線程,而要用到service。

因爲service的級別要高,不容易被系統殺死。


android中的進程等級
Foreground process 前臺進程
Visible process 可見進程
Service process 服務進程
Background process 後臺進程
Empty process 空進程


android在系統資源不夠時會自動殺死某些進程

Service有兩種啓動方式
startService--無需交互自動執行時使用
bindService--需要進行交互時

ServiceConnection服務連接器

創建一個服務

在onStart方法中建立於服務的連接
綁定服務
bindService(_Intent,
     conn, 
     BIND_AUTO_CREATE);

連接器有兩個方法
一個在服務連接時掉用
傳入一個IBinder用於我們連接服務的鑰匙

服務連接後會調用IBinder方法
在這個方法中創建媒體播放器
MediaPlayer.create(this, R.raw.hetang);
將鑰匙返回去
鑰匙的功能是將服務本身返回
供外部使用

在Activiy接收服務對象
從鑰匙中拿到我們要的對象
((MyBinder)service).getKey()

得到了服務的實例以後我們就可以使用服務中的方法,來調動媒體播放器了


!!!!
得到服務的目的就是要調用服務的方法來控制音樂的播放
!!!!

記得在onDestroy中要解除綁定

一個在服務斷開連接時調用

======================================
音樂播放器
MusicMediaActivity.java
package com.tarena.Music;

import com.tarena.Music.MusicService.MyBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class MusicMediaActivity extends Activity {
    private ImageButton mButton;
    private MusicService mMusicService;
    
    private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//從鑰匙中拿出服務對象
mMusicService=((MyBinder)service).getKey();
}
};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mButton=(ImageButton)findViewById(R.id.pause);
        mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mMusicService.start();
}
});
    }
    
    @Override
    protected void onStart() {
     super.onStart();
     Intent _Intent = 
     new Intent(this,MusicService.class);
     //三個參數
     //第一個是Intent
     //第二個是服務連接器,用於和服務的連接
     //第三個是標誌位,用於開啓服務的方式
     //BIND_AUTO_CREATE表示綁定時自動連接
     bindService(_Intent,
     conn, 
     BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onDestroy() {
     unbindService(conn);
     super.onDestroy();
    }
}
-----------------------------------------------
musicService.java
package com.tarena.Music;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MusicService extends Service {
private MediaPlayer mMediaPlayer;
@Override
public IBinder onBind(Intent intent) {
mMediaPlayer= MediaPlayer.create(this, R.raw.hetang);
Log.i("MyLog","服務已綁定");
return new MyBinder();
}
/**
 * 封裝連接服務的鑰匙類
 * 這個類的功能就是把服務類暴露給外部使用。
 
 * @author Administrator
 *
 */
class MyBinder extends Binder{
/**
 * @return將服務本身這個實例返回
 */
public MusicService getKey(){
return MusicService.this;
}
}
/**
 * 音樂開始
 */
public void start(){
if (!mMediaPlayer.isPlaying()) {
Log.i("MyLog","music start");
mMediaPlayer.start();
}
}
//暫停
//停止

}

--------------------------------------
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@drawable/background">
    
    <ImageButton
    android:id="@+id/initImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/init"  
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>
    
    
       <ListView
        android:id="@+id/music_listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_marginBottom="70dp"
        android:layout_below="@id/initImage"
        >
    </ListView> 
         

       <SeekBar
        android:id="@+id/SeekBar1"       
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout"/>

    <LinearLayout
           android:id="@+id/linearLayout"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal" 
           android:layout_alignParentBottom="true"
           android:gravity="bottom"
           android:layout_marginLeft="54dp"
          android:layout_marginRight="48dp"
          android:layout_marginBottom="4dp"
           >
     
<ImageButton
android:id="@+id/prev"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/prev"  
    android:gravity="left"
    android:layout_marginRight="40dp"/>
    <ImageButton
    android:id="@+id/pause"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/pause"  
    android:layout_marginRight="40dp"/>
      
    <ImageButton
    android:id="@+id/next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/next"  
android:gravity="right"
android:layout_gravity="right"/>
           
       </LinearLayout>

 

</RelativeLayout>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章