/**
* 测试服务
*
* @time 下午02:40:27
* @author retacn yue
* @Email [email protected]
*/
public class SercviceDemoActivity extends Activity implements OnClickListener {
private static final String MY_S_EVENT = "cn.yue.servicedemo.MyService.MY_S_SEVENT";
Button btn_start, btn_stop, btn_exit;
TextView txv_info;
private IBinder binder = null;
private BroadcastReceiver broadcastReceiver = new MyIntentReceiver();
private final IntentFilter filter = new IntentFilter(MY_S_EVENT);


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
// 注册广播
registerReceiver(broadcastReceiver, filter);


// 绑定服务
bindService(new Intent("cn.yue.servicedemo.REMOTE_SERVICE"), connection, Context.BIND_AUTO_CREATE);
}


private ServiceConnection connection = new ServiceConnection() {


@Override
public void onServiceDisconnected(ComponentName name) {


}


@Override
public void onServiceConnected(ComponentName name, IBinder iBinder) {
binder = iBinder;
}
};


/**
* 实例化控件
*/
private void findView() {
btn_start = (Button) this.findViewById(R.id.btn_play);
btn_start.setOnClickListener(this);
btn_stop = (Button) this.findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(this);
btn_exit = (Button) this.findViewById(R.id.btn_exit);
btn_exit.setOnClickListener(this);
txv_info = (TextView) this.findViewById(R.id.txv_info);
txv_info.setText("ready");
}


@Override
public void onClick(View v) {
Log.i("tag", "onClick");
if (v == btn_start) {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
try {
binder.transact(1, data, reply, 0);
} catch (RemoteException e) {
e.printStackTrace();
}
} else if (v == btn_stop) {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
try {
binder.transact(2, data, reply, 0);
} catch (RemoteException e) {
e.printStackTrace();
}
} else if (v == btn_exit) {
finish();
}


}


/**
* 自定义广播接收
*/
class MyIntentReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
int bNum = intent.getIntExtra("key", -1);
if (bNum == 0) {
txv_info.setText("Playing");
} else {
txv_info.setText("stop");
}
}


}

}

/**
* 简单播放控制服务
*
* @time 下午02:43:29
* @author retacn yue
* @Email [email protected]
*/
public class MyService extends Service implements Runnable {
private IBinder myBinder = null;
private Thread myThread;
public static Handler myHandler;
private MediaPlayer myPlayer;
private final String MY_S_SEVENT = "cn.yue.servicedemo.MyService.MY_S_SEVENT";
private Context myContext;


@Override
public IBinder onBind(Intent intent) {
return myBinder;
}


@Override
public void onCreate() {
super.onCreate();
myContext = this;
myBinder = new MyBinder();
// 启动线程接收控制信息
myThread = new Thread(this);
myThread.start();


}


@Override
public void run() {
Looper.prepare();
myHandler = new EventHandle(Looper.myLooper());
Looper.loop();
}


/**
*
*/
class EventHandle extends Handler {


public EventHandle(Looper looper) {
super(looper);
}


@Override
public void handleMessage(Message msg) {
// 取得 message信息
String tmpStr = (String) msg.obj;
if (tmpStr.contains("play")) {
if (null != myPlayer) {
return;
}
Intent intent = new Intent(MY_S_SEVENT);
intent.putExtra("key", 0);
myContext.sendBroadcast(intent);
myPlayer = MediaPlayer.create(myContext, R.raw.geek_in_the_pink);
myPlayer.start();
} else if (tmpStr.contains("stop")) {
if (null != myPlayer) {

Intent intent = new Intent(MY_S_SEVENT);
intent.putExtra("key", 1);
myContext.sendBroadcast(intent);
myPlayer.stop();
myPlayer.release();
myPlayer = null;
}
}


}
}


}


/**
* 自定义binder对像
*
* @time 下午02:55:23
* @author retacn yue
* @Email [email protected]
*/
public class MyBinder extends Binder {


@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
switch (code) {
case 1:
// 向子线程中发送控制信息
String obj = "play";
Message message = MyService.myHandler.obtainMessage(1, 1, 1, obj);
MyService.myHandler.sendMessage(message);
break;
case 2:
// 向子线程中发送控制信息
String obj1 = "stop";
Message message1 = MyService.myHandler.obtainMessage(1, 1, 1, obj1);
MyService.myHandler.sendMessage(message1);
break;
}


return true;
}


}

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


<uses-sdk android:minSdkVersion="10" />


<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SercviceDemoActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />


<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注册播放控制信息android:process 指定进程名程 -->
<service
android:name=".MyService"
android:process=":remote" >
<intent-filter >
<action android:name="cn.yue.servicedemo.REMOTE_SERVICE" />
</intent-filter>
</service>
</application>


</manifest>

<?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" >


<Button
android:id="@+id/btn_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play" />


<Button
android:id="@+id/btn_stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop" />


<Button
android:id="@+id/btn_exit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Exit" />


<TextView
android:id="@+id/txv_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="info" />


</LinearLayout>

更多相关文章

  1. Android线程池(二)
  2. [Android] 任意时刻从子线程切换到主线程的实现原理及加强版
  3. Android中handler的作用与线程
  4. 读取联系人信息
  5. 获取Android各类系统相关信息的接口实现代码
  6. Android获取地理位置信息(GPS/NETWORK)
  7. android(8)(获取手机系统内存和SD卡内存信息)
  8. js判断移动终端浏览器版本信息
  9. Android studio 编译异常信息记录

随机推荐

  1. 常用函数类型 常用数据类型
  2. .使用制作简版计算器与.九九乘法表
  3. PS合成技巧、改日期时间、修图改字、换背
  4. 在虚拟机中运行 Linux 的十大优点
  5. js对象、函数、日期对象----简易日历
  6. JavaScript格式的计算器以及乘法表
  7. JS 简单计算器、九九乘法表实例演示
  8. 文件本质与作用、关键字的学习
  9. Unity程序结合云渲染技术在手机中使用要
  10. CSS:模态框定位实战