上一篇文章中我们启动service的方法是:
startService()
intent = new Intent("android.startService");//区分服务类型 Bundle bundle = new Bundle(); bundle.putInt("op", op); intent.putExtras(bundle); startService(intent); //此处也可以用 intent.setClass(this,StartMusicService.class); 这样intent=new Intent();
bindService
Intent intent = new Intent("android.bindService"); //也可以:Intent intent = new Intent(MainActivity.this,BindMusicService.class); //Context.BIND_AUTO_CREATE :只要Activity和Service绑定了,就会自动创建Service bindService(intent, sc, Context.BIND_AUTO_CREATE);
都是属于直接启动服务的方法。我们也可以通过Activity与Broadcast(广播)间接启动Service(服务),我们本篇文章习如何通过广播Broadcast启动服务Service。
也许你会说,能用Activity启动,干嘛要用广播呢?
我们举例来说:你的音乐播放器的播放、暂停、停止等处理一般会放在一个服务里。我们在听歌时突然来了一个电话,或者拔掉耳机,我们会要求暂停播放音乐。这个效果是怎么做到的呢?来电话、拔耳机系统会向该程序发送一个系统消息。我们可以建一个BroadcastReceiver子类,在注册广播接收器后接收消息了,然后在onReceive里启动服务(暂停处理)。要是我们直接启动服务,就不能针对消息,启动服务了(必须借助广播)。
总言之:通过广播间接启动Service适用情况:要针对系统消息做出处理,而此处理又在Service里。
例子:
activity_main.xml
<?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通过Broadcast启动Service" /> </LinearLayout>
MainActivity.java
package com.genwoxue.broadcastservice; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; public class MainActivity extends Activity { private BroadcastReceiverUtil util=null; private Button btnSend=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSend=(Button)super.findViewById(R.id.send); btnSend.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ //发送广播:其Action为“com.genwoxue.action.ABOUTSERVICE” Intent intent=new Intent("com.genwoxue.action.ABOUTSERVICE"); MainActivity.this.sendBroadcast(intent); //实例化广播过滤器(只过滤其Action为"com.genwoxue.action.ABOUTSERVICE") IntentFilter filter=new IntentFilter("com.genwoxue.action.ABOUTSERVICE"); //实例化广播接收器(接收方) util=new BroadcastReceiverUtil(); //注册BroadcastReceiver:参数为接收器与过滤器 MainActivity.this.registerReceiver(util, filter); } }); } @Override protected void onStop(){ //停止广播 super.unregisterReceiver(util); super.onStop(); //停止服务 Intent intent=new Intent(MainActivity.this,ServiceUtil.class); MainActivity.this.stopService(intent); } }
BroadcastReceiverUtil.java
package com.genwoxue.broadcastservice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class BroadcastReceiverUtil extends BroadcastReceiver{ @Override public void onReceive(Context context,Intent intent){ //广播接收器(接收方)判断Action为“com.genwoxue.action.ABOUTSERVICE”则启动服务 if("com.genwoxue.action.ABOUTSERVICE".equals(intent.getAction())){ context.startService(new Intent(context,ServiceUtil.class)); } } }
ServiceUtil.java
package com.genwoxue.broadcastservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class ServiceUtil extends Service{ private static final String TAG="AboutService"; @Override public IBinder onBind(Intent intent){ return null; } @Override public void onCreate(){ Log.i(TAG,"服务:onCreate()"); } //启动 @Override public int onStartCommand(Intent intent,int flags,int startId){ Log.i(TAG, "服务启动:onStart()=>Intent"+intent+",startID="+startId); return Service.START_CONTINUATION_MASK; } @Override public void onDestroy(){ Log.i(TAG,"服务:onDestroy()"); } }
AndroidManifest.xml中注册:
<service android:name="com.genwoxue.broadcastservice.ServiceUtil" />
运行结果:
参考:http://blog.csdn.net/jianghuiquan/article/details/8641152#