Service实例-播放mp3音乐

[color=red]1.在main.xml中加入如下四个按钮[/color]
<TextView  
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/button1"
android:text="开启音乐播放服务"/>

<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/button2"
android:text="停止音乐播放服务"/>

<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/button3"
android:text="绑定音乐播放服务"/>

<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/button4"
android:text="解绑定音乐播放服务"/>


[color=red]2.在res目录下新建raw目录,并拷入mp3文件,我的是www.mp3
创建如下Service[/color]
public class HelloService extends Service {

MediaPlayer mp;//定义音乐播放器变量
private final static String TAG = "HelloService";

@Override
//绑定服务 通过 bindService 方法启动服务时调用
public IBinder onBind(Intent intent) {
Toast.makeText(this, "onBind(Intent intent)",Toast.LENGTH_SHORT).show();
mp.start();
return null;
}

@Override
//解绑定服务 通过 unbindService 方法解绑定服务时调用
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "onUnbind(Intent intent)",Toast.LENGTH_SHORT).show();

mp.stop();
return false;
}

@Override
//创建服务 该服务不存在需要被创建时被调用,不管startService()还是bindService()都会在启动时调用该方法
public void onCreate() {
Toast.makeText(this, "onCreate()",Toast.LENGTH_SHORT).show();
//创建一个音乐播放器对象
mp = MediaPlayer.create(this.getApplicationContext(), R.raw.www);
//设置可以重复播放
mp.setLooping(true);
}

@Override
//停止服务服务 通过 onDestroy 方法停止服务时调用
public void onDestroy() {
Toast.makeText(this, "onDestroy()",Toast.LENGTH_SHORT).show();
mp.stop();

}

@Override
//开始服务 通过 onStart 方法开始服务时调用
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "onStart",Toast.LENGTH_SHORT);
mp.start();

}

}

[color=red]3.在AndroidManifest.xml文件中注册Service[/color]
	<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" 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 -->
<service android:name=".service.HelloService"></service>
</application>


[color=red]4.Activity中的代码[/color]

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取页面组件
Button button1 = (Button) this.findViewById(R.id.button1);
Button button2 = (Button) this.findViewById(R.id.button2);
Button button3 = (Button) this.findViewById(R.id.button3);
Button button4 = (Button) this.findViewById(R.id.button4);

// 定义服务链接对象 -要启动哪个服务
final ServiceConnection sc = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(MainActivity.this,
"ServiceConnection onServiceDisconnected",
Toast.LENGTH_SHORT).show();

}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(MainActivity.this,
"ServiceConnection onServiceConnected",
Toast.LENGTH_SHORT).show();

}
};
// 定义监听事件
OnClickListener ocl = new OnClickListener() {

//在意图中指定服务
Intent intent = new Intent(MainActivity.this, HelloService.class);

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
startService(intent);
break;
case R.id.button2:
stopService(intent);
break;
case R.id.button3:
// 绑定启动要指定服务链接对象,绑定的操作选项
bindService(intent, sc, Context.BIND_AUTO_CREATE);
break;
case R.id.button4:
unbindService(sc);
break;
}

}
};
//为按钮加点击事件
button1.setOnClickListener(ocl);
button2.setOnClickListener(ocl);
button3.setOnClickListener(ocl);
button4.setOnClickListener(ocl);
}
}


[color=red]5.从这个例子中我们可以体会到Context.startService()方法和Context.bindService()方法的区别[/color]
在模拟器中点击[color=blue]开启音乐播放服务[/color]后,播放音乐,此时点击右侧的回退按钮,音乐仍然播放。但是通过[color=blue]绑定音乐播放服务[/color]启动的音乐,在点击回退按钮后,音乐也停止了播放,说明通过这种方式启动的服务是与Context绑定的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值