服务的四种启动方式
2017-05-23 10:50
155 查看
第一种:使用startService启动服务
第二种:使用Receiver方式启动服务
第三种:使用bindService方式启动服务
第四种:通过AIDL方式使用远程服务
我将通过一个具体的android实例来分别讲解这四种服务启动方式。该项目的实现效果如下:
主页面为一个imageView和四个Button按钮,分别通过四个按钮跳转到不同的activity,每一个activity对应一种服务的启动方式。
这是第一个Activity:
包含一个imageView和五个button按钮。分别实现音乐的播放,停止,暂停,关闭和退出功能。
第一个Activity实现代码如下:
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PlayMusic extends Activity implements OnClickListener{
private static final String TAG="PlayMusic";
private Button playBtn,stopBtn,pauseBtn,exitBtn,closeBtn;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.music_service);
setTitle("Music Service");
findView();
bindButton();
}
private void findView(){
playBtn=(Button) findViewById(R.id.play);
stopBtn=(Button) findViewById(R.id.stop);
pauseBtn=(Button) findViewById(R.id.pause);
exitBtn=(Button) findViewById(R.id.exit);
closeBtn=(Button) findViewById(R.id.close);
}
private void bindButton(){
playBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
exitBtn.setOnClickListener(this);
closeBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int op=-1;
Intent intent=new Intent("com.lqh.servicetest.MusicService");
switch (v.getId()) {
case R.id.play:
Log.d(TAG,"onClick:playing music");
op=1;
break;
case R.id.pause:
Log.d(TAG,"onClick:pausing music");
op=2;
break;
case R.id.stop:
Log.d(TAG,"onClick:stoping music");
op=3;
break;
case R.id.close:
Log.d(TAG,"onClick:close");
this.finish();
break;
case R.id.exit:
Log.d(TAG,"onClick:exit");
stopService(intent);
this.finish();
return;
}
Bundle bundle=new Bundle();
bundle.putInt("op", op);
intent.putExtras(bundle);
startService(intent);
}
}建议大家通过上面的这种方式来书写代码。将findViewById()独立出来,便于以后的维护。然后分别设置监听。创建Intent对象,设置action,用于启动服务。这里只需要保证创建Intent()时传入的action参数和AndroidManifest.xml中注册服务的action一致即可。配置文件通过标志变量op来指定实现不同的功能。然后通过Bundle对象存入op标志,然后通过startService()传入intent对象,即可启动服务。
接下来我们来分析服务代码:
import android.R.integer;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MusicService extends Service {
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
Toast.makeText(this, "启动音乐播放器", Toast.LENGTH_SHORT).show();
if(mediaPlayer==null){
mediaPlayer=MediaPlayer.create(this, R.raw.tmp);
mediaPlayer.setLooping(false);
}
}
public void onDestroy(){
Toast.makeText(this, "停止音乐播放器", Toast.LENGTH_SHORT).show();
if(mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.release();
}
}
public void onStart(Intent intent,int startId){
if(intent!=null){
Bundle bundle=intent.getExtras();
if(bundle!=null){
int op=bundle.getInt("op");
switch (op) {
case 1:
play();
break;
case 2:
pause();
break;
case 3:
stop();
break;
}
}
}
}
public void play(){
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
public void pause(){
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
public void stop(){
if(mediaPlayer!=null){
mediaPlayer.pause();
mediaPlayer.seekTo(0);
}
}
}在服务代码中,首先创建一个MediaPlayer对象。这是android内置的音乐服务类。通过mediaPlayer对象调用相应的方法即可实现不同的音乐播放器功能。由于我们是通过startService()启动服务。所以可以忽略OnBind()方法。通过startService()启动服务的步骤如下:
onCreate()->onStart()->Service running->onDestroy()->Service stop
首先在onCreate()中我们先判断mediaPlayer对象是否为空,如果为空则创建一个媒体播放器对象用于播放音频文件,然后使用指定的音频文件初始化播放器。这里我用的音频文件名为tmp,MP3格式。然后设置为不循环播放。
然后onStart()中,创建Bundle对象用于保存从PlayMusic传过来的标志位op。然后分别通过op的值启动不同的方法。然后重写一下这些方法。即可实现音乐的播放,暂停,停止等功能。
最后。在AndroidManifest.xml中注册一下服务MusicService和Activity即可。
<activity android:name=".PlayMusic"/>
<service
android:enabled="true"
android:name=".MusicService"
>
<intent-filter>
<action android:name="com.lqh.servicetest.MusicService"/>
</intent-filter>
</service>
其他三种服务启动方式我将在后面的博文中展示。
第二种:使用Receiver方式启动服务
第三种:使用bindService方式启动服务
第四种:通过AIDL方式使用远程服务
我将通过一个具体的android实例来分别讲解这四种服务启动方式。该项目的实现效果如下:
主页面为一个imageView和四个Button按钮,分别通过四个按钮跳转到不同的activity,每一个activity对应一种服务的启动方式。
这是第一个Activity:
包含一个imageView和五个button按钮。分别实现音乐的播放,停止,暂停,关闭和退出功能。
第一个Activity实现代码如下:
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PlayMusic extends Activity implements OnClickListener{
private static final String TAG="PlayMusic";
private Button playBtn,stopBtn,pauseBtn,exitBtn,closeBtn;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.music_service);
setTitle("Music Service");
findView();
bindButton();
}
private void findView(){
playBtn=(Button) findViewById(R.id.play);
stopBtn=(Button) findViewById(R.id.stop);
pauseBtn=(Button) findViewById(R.id.pause);
exitBtn=(Button) findViewById(R.id.exit);
closeBtn=(Button) findViewById(R.id.close);
}
private void bindButton(){
playBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
exitBtn.setOnClickListener(this);
closeBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int op=-1;
Intent intent=new Intent("com.lqh.servicetest.MusicService");
switch (v.getId()) {
case R.id.play:
Log.d(TAG,"onClick:playing music");
op=1;
break;
case R.id.pause:
Log.d(TAG,"onClick:pausing music");
op=2;
break;
case R.id.stop:
Log.d(TAG,"onClick:stoping music");
op=3;
break;
case R.id.close:
Log.d(TAG,"onClick:close");
this.finish();
break;
case R.id.exit:
Log.d(TAG,"onClick:exit");
stopService(intent);
this.finish();
return;
}
Bundle bundle=new Bundle();
bundle.putInt("op", op);
intent.putExtras(bundle);
startService(intent);
}
}建议大家通过上面的这种方式来书写代码。将findViewById()独立出来,便于以后的维护。然后分别设置监听。创建Intent对象,设置action,用于启动服务。这里只需要保证创建Intent()时传入的action参数和AndroidManifest.xml中注册服务的action一致即可。配置文件通过标志变量op来指定实现不同的功能。然后通过Bundle对象存入op标志,然后通过startService()传入intent对象,即可启动服务。
接下来我们来分析服务代码:
import android.R.integer;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MusicService extends Service {
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onCreate(){
Toast.makeText(this, "启动音乐播放器", Toast.LENGTH_SHORT).show();
if(mediaPlayer==null){
mediaPlayer=MediaPlayer.create(this, R.raw.tmp);
mediaPlayer.setLooping(false);
}
}
public void onDestroy(){
Toast.makeText(this, "停止音乐播放器", Toast.LENGTH_SHORT).show();
if(mediaPlayer!=null){
mediaPlayer.stop();
mediaPlayer.release();
}
}
public void onStart(Intent intent,int startId){
if(intent!=null){
Bundle bundle=intent.getExtras();
if(bundle!=null){
int op=bundle.getInt("op");
switch (op) {
case 1:
play();
break;
case 2:
pause();
break;
case 3:
stop();
break;
}
}
}
}
public void play(){
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();
}
}
public void pause(){
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.pause();
}
}
public void stop(){
if(mediaPlayer!=null){
mediaPlayer.pause();
mediaPlayer.seekTo(0);
}
}
}在服务代码中,首先创建一个MediaPlayer对象。这是android内置的音乐服务类。通过mediaPlayer对象调用相应的方法即可实现不同的音乐播放器功能。由于我们是通过startService()启动服务。所以可以忽略OnBind()方法。通过startService()启动服务的步骤如下:
onCreate()->onStart()->Service running->onDestroy()->Service stop
首先在onCreate()中我们先判断mediaPlayer对象是否为空,如果为空则创建一个媒体播放器对象用于播放音频文件,然后使用指定的音频文件初始化播放器。这里我用的音频文件名为tmp,MP3格式。然后设置为不循环播放。
然后onStart()中,创建Bundle对象用于保存从PlayMusic传过来的标志位op。然后分别通过op的值启动不同的方法。然后重写一下这些方法。即可实现音乐的播放,暂停,停止等功能。
最后。在AndroidManifest.xml中注册一下服务MusicService和Activity即可。
<activity android:name=".PlayMusic"/>
<service
android:enabled="true"
android:name=".MusicService"
>
<intent-filter>
<action android:name="com.lqh.servicetest.MusicService"/>
</intent-filter>
</service>
其他三种服务启动方式我将在后面的博文中展示。
相关文章推荐
- tomcat非安装方式,添加windows服务启动方式
- 命令行方式 启动和停止 windows服务
- tomcat7.0无法以windows服务的方式启动
- activity四种方式启动
- 用快捷方式启动停止服务
- 命令行更改服务启动方式
- [解决办法]Apache服务启动失败:以一种访问权限不允许的方式做了一个访问套接字的尝试
- Ubuntu10.10,ubuntu11.04开机服务启动优化 基于事件的启动方式
- ArcSDE 服务启动后又停止的解决方式一例
- 开机启动和其他服务的自动启动方式。
- Oracle数据库的四种启动方式
- svn的启动方式设置为windos服务
- tomcat非安装方式,添加windows服务启动方式
- Ubuntu10.10,ubuntu11.04开机服务启动优化 基于事件的启动方式
- 『转』启动/关闭oracle服务有三种方式
- Windows Service开发日志四(用程序设置服务的运行状态及启动方式)
- 以Windows服务方式启动MySQL
- Oracle数据库的四种启动方式
- 服务方式启动svn服务器
- 启动/关闭oracle服务有三种方式