安卓提供的Service(服务)组件不直接用户进行交互,能够长期在后台运行,使用的方式有俩种,一种是启动方式,另一种是绑定方式,简单的说就是启动方式,即使当前Activity已经退出了,服务也不会停止,除非在应用关闭服务;绑定方式可以将服务绑定到Activity中,随着Activity的关闭而停止。
截图:
代码:
(主Activity)
1 package com.example.musicaactivity; 2 3 import android.app.Activity; 4 import android.content.ComponentName; 5 import android.content.Context; 6 import android.content.Intent; 7 import android.content.ServiceConnection; 8 import android.os.Bundle; 9 import android.os.IBinder; 10 import android.view.Menu; 11 import android.view.MenuItem; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.widget.Button; 15 16 17 public class MainActivity extends Activity { 18 Button ob1,ob2,ob3,ob4; //四个按钮 19 Intent intent; // 20 MyService servicemusic; //服务 21 ServiceConnection con=new ServiceConnection() { 22 @Override 23 public void onServiceConnected(ComponentName name,IBinder service) { //服务绑定成功执行该方法(回调函数将服务对象实例赋给servicemusic) 24 // TODO Auto-generated method stub 25 servicemusic=((MyService.LocalBinder)service).getService(); 26 27 } 28 @Override 29 public void onServiceDisconnected(ComponentName name) { //服务意外终止时执行此方法 30 // TODO Auto-generated method stub 31 servicemusic=null; 32 } 33 34 35 }; 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.activity_main); 40 ob1=(Button) findViewById(R.id.bo1); 41 ob2=(Button) findViewById(R.id.bo2); 42 ob3=(Button) findViewById(R.id.bo3); 43 ob4=(Button) findViewById(R.id.bo4); 44 intent=new Intent(getApplicationContext(),MyService.class); //要启动的服务 45 46 ob1.setOnClickListener(new OnClickListener() { //按钮添加点击 47 48 @Override 49 public void onClick(View v) { 50 // TODO Auto-generated method stub 51 startService(intent); //第一种方式启动服务 52 53 } 54 }); 55 ob2.setOnClickListener(new OnClickListener() { 56 57 @Override 58 public void onClick(View v) { 59 // TODO Auto-generated method stub 60 stopService(intent); //停止服务 61 62 } 63 }); 64 ob3.setOnClickListener(new OnClickListener() { 65 66 @Override 67 public void onClick(View v) { 68 // TODO Auto-generated method stub 69 bindService(intent,con,Context.BIND_AUTO_CREATE); //第二种绑定方式启动服务 70 //当服务绑定后会执行con中的方法 71 72 } 73 }); 74 ob4.setOnClickListener(new OnClickListener() { 75 76 @Override 77 public void onClick(View v) { 78 // TODO Auto-generated method stub 79 unbindService(con); //解除绑定 80 servicemusic=null; 81 82 83 } 84 }); 85 86 87 88 89 90 } 91 92 93 94 }
(Service服务)
package com.example.musicaactivity; import android.app.Service; import android.content.Intent; import android.content.ServiceConnection; import android.media.MediaPlayer; import android.os.Binder; import android.os.IBinder; import android.widget.Toast; public class MyService extends Service { IBinder binder=new LocalBinder(); //用于onBind(方法返回) MediaPlayer mp; //音乐播放类 public class LocalBinder extends Binder{ MyService getService() { // TODO Auto-generated method stub return MyService.this; } } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); mp=MediaPlayer.create(getApplicationContext(),R.raw.sleep); //创建音乐播放 } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. //throw new UnsupportedOperationException("Not yet implemented"); mp.start(); //启动音乐播放 Toast.makeText(this,"绑定好了" , 2).show(); return binder; } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); Toast.makeText(this,"开始播放" , 2).show(); mp.start(); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub Toast.makeText(this,"解除绑定" , 2).show(); mp.stop(); return false; } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mp.stop(); Toast.makeText(this,"停止播放" , 2).show(); } }
(布局)
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.example.musicaactivity.MainActivity" > 6 7 8 9 10 11 <TextView 12 android:id="@+id/textView1" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:layout_alignParentTop="true" 16 android:layout_alignRight="@+id/bo4" 17 android:layout_marginTop="36dp" 18 android:text="音乐播放服务" /> 19 <Button 20 android:id="@+id/bo2" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:layout_below="@+id/bo1" 24 android:text="停止音乐播放" /> 25 26 <Button 27 android:id="@+id/bo3" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:layout_alignRight="@+id/bo2" 31 android:layout_below="@+id/bo2" 32 android:text="绑定音乐播放" /> 33 <Button 34 android:id="@+id/bo4" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:layout_alignRight="@+id/bo3" 38 android:layout_below="@+id/bo3" 39 android:text="解绑音乐播放" /> 40 41 <Button 42 android:id="@+id/bo1" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:layout_alignRight="@+id/textView1" 46 android:layout_below="@+id/textView1" 47 android:text="开启音乐播放" /> 48 49 </RelativeLayout>