Android多线程:使用IBinder播放MP3音乐

一、架构设计

一个典型的IPC框架是:

 

在上图中,MyActivity直接使用IBinder接口里调用MyService的功能。

1> 当MyActivity跨进程启动了MyService时,MyService创建了消息处理线程,同时诞生线程的MQ和Loop。MyService同时创建MyBinder对象。

2> MyService调用MyBinder的构造函数去诞生MyBinder对象。接着Binder框架的构造函数调用JNI本地模块的init()函数,并将MyBinder对象的指针传入到JNI本地模块。

3> 接着,AMS(Activity Manager Service)调用MyService的onBinder()函数,回传了MyBinder对象,就建立了C/C++层的JavaBBinder对象。

4> 接着,AMS便回传JavaBBinder的IBinder接口给MyActivity。至此,MyActivity就能调用IBinder接口了。

5> MyActivity调用回传的IBinder接口对象执行transact函数发送启动或停止播放MediaPlayer的请求,负责执行MyBinder的onTransact()函数通过MyService对象的Handler成员发送消息给MyService的消息处理线程,有消息处理线程去播放mp3音乐。

6> 在执行播放音乐的过程中,MyService里的小线程通过BroadcastReceiver发送广播消息给MyActivity,将播放状态字符串显示在画面上。

附图:


二、完整的程序代码

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.misoo.pk01"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.misoo.pk01.MyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:process=".remote" >
            <intent-filter>
                <action android:name="com.misoo.pk01.REMOTE_SERVICE" />
            </intent-filter>
        </service>
    </application>

</manifest>
MyBinder实现代码:

package com.misoo.pk01;

import android.os.Binder;
import android.os.Message;
import android.os.Parcel;
import android.os.RemoteException;

public class MyBinder extends Binder {
	
	protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
			throws RemoteException {
		switch(code){
		case 1:
			String obj = "play";
			Message message = MyService.h.obtainMessage(1,1,1,obj);
			MyService.h.sendMessage(message);
			break;
		case 2:
			obj = "stop";
			message = MyService.h.obtainMessage(1, 1, 1, obj);
			MyService.h.sendMessage(message);
			break;
		}
		return true;
	}
}
MyService实现代码:

package com.misoo.pk01;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class MyService extends Service implements Runnable {
	private IBinder mBinder = null;
	private Thread th1;
	public static Handler h;
	private MediaPlayer mediaPlayer = null;
	public static Context context;
	private final String MY_S_EVENT = new String(
			"com.misoo.pk01.MyService.MY_S_EVENT");

	public void onCreate() {
		super.onCreate();
		context = this;
		mBinder = new MyBinder();

		th1 = new Thread(this);
		th1.start();
	}

	public IBinder onBind(Intent arg0) {
		return mBinder;
	}

	public void run() {
		Looper.prepare();
		h = new EventHandler(Looper.myLooper());
		Looper.loop();
	}

	class EventHandler extends Handler {
		public EventHandler(Looper looper) {
			super(looper);
		}

		public void handleMessage(Message msg) {
			String obj = (String) msg.obj;
			if (obj.contains("play")) {
				if (mediaPlayer != null)
					return;
				Intent in = new Intent(MY_S_EVENT);
				in.putExtra("key", 0);
				context.sendBroadcast(in);

				mediaPlayer = MediaPlayer.create(context, R.raw.san);

				try {
					mediaPlayer.start();
				} catch (IllegalStateException e) {
					Log.e("Play", "error:" + e.getMessage(), e);
				}
			} else if (obj.contains("stop")) {
				if (mediaPlayer != null) {
					Intent in = new Intent(MY_S_EVENT);
					in.putExtra("key", 1);
					context.sendBroadcast(in);

					mediaPlayer.stop();
					mediaPlayer.release();
					mediaPlayer = null;
				}
			}
		}
	}
}

MyActivity实现代码:

package com.misoo.pk01;

import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyActivity extends Activity implements OnClickListener {
	private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
	private final int MP = LinearLayout.LayoutParams.MATCH_PARENT;
	private Button btn0, btn1, btn2;
	public TextView tv;
	private IBinder ib = null;
	private final String MY_S_EVENT = new String(
			"com.misoo.pk01.MyService.MY_S_EVENT");
	protected final IntentFilter filter = new IntentFilter(MY_S_EVENT);
	private BroadcastReceiver receiver = new MyIntentReceiver();

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);

		btn0 = new Button(this);
		btn0.setId(101);
		btn0.setText("play");
		btn0.setOnClickListener(this);

		btn1 = new Button(this);
		btn1.setId(102);
		btn1.setText("stop");
		btn1.setOnClickListener(this);

		btn2 = new Button(this);
		btn2.setId(103);
		btn2.setText("exit");
		btn2.setOnClickListener(this);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(135,
				60);
		params.topMargin = 10;
		layout.addView(btn0, params);
		layout.addView(btn1, params);
		layout.addView(btn2, params);

		tv = new TextView(this);
		tv.setText("Ready");
		LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(MP,
				WC);
		params2.topMargin = 10;
		layout.addView(tv, params2);

		setContentView(layout);

		registerReceiver(receiver, filter);
		bindService(new Intent("com.misoo.pk01.REMOTE_SERVICE"), mConnection,
				Context.BIND_AUTO_CREATE);

	}

	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceDisconnected(ComponentName name) {
		}

		public void onServiceConnected(ComponentName name, IBinder service) {
			ib = service;
		}
	};

	public void onClick(View v) {
		switch (v.getId()) {
		case 101:
			Parcel data = Parcel.obtain();
			Parcel reply = Parcel.obtain();
			try {
				ib.transact(1, data, reply, 0);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			break;
		case 102:
			data = Parcel.obtain();
			reply = Parcel.obtain();
			try {
				ib.transact(2, data, reply, 0);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			break;
		case 103:
			finish();
			break;
		}
	}

	class MyIntentReceiver extends BroadcastReceiver {
		public void onReceive(Context context, Intent intent) {
			int bn = intent.getIntExtra("key", -1);
			if (bn == 0)
				tv.setText("Playing");
			else
				tv.setText("Stop.");
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值