編輯:關於Android編程
本文章介紹MediaPlayer本地音樂播放器,而當應用程序不再位於前台且沒有正在使用它的活動時,為了確保音頻繼續播放,我們需要建立一個服務Service。
Activity與綁定服務Service之間的交互是本文章的重點(這裡需要說明一點的是,Activity不能直接訪問服務對象中的方法,所以才有了我們一下的介紹,這也是為服務的安全等方面的考慮)。

直接上代碼:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20140524/2014052409101446.jpg" alt="\">
布局文件:activity_main:
package com.multimediademo6audioservice;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
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;
public class MainActivity extends Activity implements OnClickListener {
private Button startServiceButton, stopServiceButton, fastBackward,
fastForward;
private Intent intentService;
private SimpleAudioService simpleAudioService;
boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
/**
* 實例化組件
*/
private void init() {
startServiceButton = (Button) findViewById(R.id.startServiceButton);
stopServiceButton = (Button) findViewById(R.id.stopServiceButton);
fastBackward = (Button) findViewById(R.id.fastBackward);
fastForward = (Button) findViewById(R.id.fastForward);
startServiceButton.setOnClickListener(this);
stopServiceButton.setOnClickListener(this);
fastBackward.setOnClickListener(this);
fastForward.setOnClickListener(this);
intentService = new Intent(this, SimpleAudioService.class);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
/***
* 開啟
*/
case R.id.startServiceButton:
startService(intentService);
/**
* 通過一條bindService命令建立了與服務的連接,且該bindService命令把這個對象(
* 下面的ServiceConnection接口)命名為serviceConnection。
*/
bindService(intentService, serviceConnection,
Context.BIND_AUTO_CREATE);
flag = true;
break;
/**
* 關閉
*/
case R.id.stopServiceButton:
if (flag) {
unbindService(serviceConnection);
stopService(intentService);
flag = false;
}
break;
/**
* 快退
*/
case R.id.fastBackward:
if (flag) {
simpleAudioService.backwardFun();
}
break;
/**
* 快進
*/
case R.id.fastForward:
if (flag) {
simpleAudioService.forwardFun();
}
break;
default:
break;
}
}
/**
* serviceConnection是一個ServiceConnection類型的對象,它是一個接口,用於監聽所綁定服務的狀態
*/
private ServiceConnection serviceConnection = new ServiceConnection() {
/**
* 點擊開啟按鈕,會調用serviceConnection對象中的onServiceConnected方法。
* 向該方法傳遞一個IBinder對象
* ,其實際是從服務本身創建和提交的。這個IBinder對象將是SimpleAudioServiceBinder類型,我們將在服務中創建它。
* 它將有一個方法用於返回我們的服務本身,成為getService,這樣我們就可以對該方法返回的對象直接進行操作。
*/
@Override
public void onServiceConnected(ComponentName name, IBinder sasBinder) {
simpleAudioService = ((SimpleAudioService.SimpleAudioServiceBinder) sasBinder)
.getService();
}
/**
* 該方法用於處理與服務斷開連接時的情況。
*/
@Override
public void onServiceDisconnected(ComponentName name) {
simpleAudioService = null;
}
};
}
package com.multimediademo6audioservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
/**
* 開啟的服務
*
*/
public class SimpleAudioService extends Service implements OnCompletionListener {
private String TAG = "zhongyao";
private MediaPlayer player;
public class SimpleAudioServiceBinder extends Binder {
SimpleAudioService getService() {
return SimpleAudioService.this;
}
}
private final IBinder sasBinder = new SimpleAudioServiceBinder();
@Override
public IBinder onBind(Intent intent) {
return sasBinder;
}
/**
* 在MainActivity中點擊開啟後調用startService開啟服務時,
* 會調用如下的兩個方法:onCreate(),onStartCommand()
*/
@Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.good);
player.setOnCompletionListener(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
if (!player.isPlaying()) {
player.start();
}
return super.onStartCommand(intent, flags, startId);
}
/**
* 調用stopService停止服務時,會調用onDestroy()方法。
*/
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
if (player.isPlaying()) {
player.stop();
}
player.release();
}
/**
* 播放完以後調用
*/
@Override
public void onCompletion(MediaPlayer mp) {
stopSelf();
}
/**
* 服務中方法:快退
*/
public void backwardFun() {
if (player.isPlaying()) {
player.seekTo(player.getCurrentPosition() - 2500);
}
}
/**
* 服務中方法:快進
*/
public void forwardFun() {
if (player.isPlaying()) {
player.seekTo(player.getCurrentPosition() + 2500);
}
}
}
點擊下載源碼
使用HttpURLConnection實現在android客戶端和服務器之間傳遞對象
一般情況下,客戶端和服務端的數據交互都是使用json和XML,相比於XML,json更加輕量級,並且省流量,但是,無論我們用json還是用xml,都需要我們先將數據封裝成
[android] 天氣app布局練習
[android] 天氣app布局練習主要練習一下RelativeLayout和LinearLayout
Android開發-Retrofit-AndroidStudio(二)Json
文字說明都在代碼和圖片上了。唯一要注意的是,Json或者圖片等工程類包,需要自己去官網下載~~另外,這邊還用到一個Google的注解jar包~~ &
Android客戶端性能優化(魅族資深工程師毫無保留奉獻)
本文由魅族科技有限公司資深Android開發工程師degao(嵌入式企鵝圈原創團隊成員)撰寫,是degao在嵌入式企鵝圈發表的第一篇原創文章,毫無保留地總結分享其在領導魅