Service(服务)在Android中地位是至关重要的,我们可以通过Activity与Broadcast(广播)启动Service(服务),我们本章学习如何通过广播Broadcast启动服务Service。
也许你会说,能用Activity启动,干嘛要用广播呢?——且听电话监听、短信监听再作分解!
一、设计界面
1、布局文件
打开res/layout/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>
二、程序文件
1、创建“src/com.genwoxue.broadcastservice/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()"); } }
2、创建“src/com.genwoxue.broadcastservice/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)); } } }
3、打开“src/com.genwoxue.broadcastservice/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); } }
三、配置文件
打开“AndroidManifest.xml”文件。
然后输入以下代码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.genwoxue.broadcastservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="15" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.genwoxue.broadcastservice.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 android:name="com.genwoxue.broadcastservice.ServiceUtil" /> </application> </manifest>
注意:需要在AndroidManifest.xml文件中添加<service...>:
<service android:name="com.genwoxue.broadcastservice.ServiceUtil" />
四、运行结果
Activity发送广播—>广播接收方启动Service服务