8.1.3 在BroadcastReceiver中启动Service

2010-06-21 16:57 李宁 中国水利水电出版社 字号:T | T
一键收藏,随时查看,分享好友!

《Android/OPhone开发完全讲义》第8章Android服务,本章主要介绍了Android系统 中的服务(Service)技术。Service是Android中4个应用程序组件之一。在Android系统内部提供了很多的系统服务,通过这些系统 服务,可以实现更为复杂的功能,例如,监听来电、重力感应等。本节为大家介绍在BroadcastReceiver中启动Service。

AD:

8.1.3  在BroadcastReceiver中启动Service

本节的例子代码所在的工程目录是srcch08ch08_startupservice

在8.1.1节和8.1.2节都是先启动了一个Activity,然后在Activity中启动服务。如果是这样,在启动服务时必须要先启动一个 Activity。在很多时候这样做有些多余,阅读完第7章的内容,会发现实例43可以利用Broadcast Receiver在Android系统启动时运行一个Activity。也许我们会从中得到一些启发,既然可以在Broadcast Receiver中启动Activity,为什么不能启动Service呢?说做就做,现在让我们来验证一下这个想法。

先编写一个服务类,这个服务类没什么特别的,仍然使用前面两节编写的MyService类即可。在AndroidManifest.xml文件中配置MyService类的代码也相同。

下面来完成最关键的一步,就是建立一个BroadcastReceiver,代码如下:

  1. package net.blogjava.mobile.startupservice;  
  2.  
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6.  
  7. public class StartupReceiver extends BroadcastReceiver  
  8. {  
  9.     @Override  
  10.     public void onReceive(Context context, Intent intent)  
  11.     {  
  12.         //  启动一个Service  
  13.         Intent serviceIntent = new Intent(context, MyService.class);          
  14.         context.startService(serviceIntent);          
  15.         Intent activityIntent = new Intent(context, MessageActivity.class);  
  16.         //  要想在Service中启动Activity,必须设置如下标志  
  17.         activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  18.         context.startActivity(activityIntent);  
  19.     }  

在StartupReceiver类的onReceive方法中完成了两项工作:启动服务和显示一个Activity来提示服务启动成功。其中 MessageActivity是一个普通的Activity类,只是该类在配置时使用了"@android:style/Theme.Dialog"主 题,因此,如果服务启动成功,会显示如图8.4所示的信息。

 
图8.4  在BroadcastReceiver中启动服务

如果安装本例后,在重新启动模拟器后并未出现如图8.4所示的信息提示框,最大的可能是没有在AndroidManifest.xml文件中配置BroadcastReceiver和Service,下面来看一下AndroidManifest.xml文件的完整代码。

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     package="net.blogjava.mobile.startupservice"
     android:versionCode="1" 
  4.     android:versionName="1.0"
  5.     <application android:icon="@drawable/icon" 
    android:label="@string/app_name"
  6.         <activity android:name=".MessageActivity" 
    android:theme="@android:style/Theme.Dialog"
  7.             <intent-filter>                  
  8.                 <category android:name="android.
    intent.category.LAUNCHER" /> 
  9.             </intent-filter
  10.         </activity
  11.         <receiver android:name="StartupReceiver"
  12.             <intent-filter
  13.                 <action android:name="android.
    intent.action.BOOT_COMPLETED" /> 
  14.                 <category android:name="android.
    intent.category.LAUNCHER" /> 
  15.             </intent-filter
  16.         </receiver
  17.         <service android:enabled="true" android:name=".MyService" /> 
  18.     </application
  19.     <uses-sdk android:minSdkVersion="3" /> 
  20.     <uses-permission android:name="android.
    permission.RECEIVE_BOOT_COMPLETED" /> 
  21. </manifest

现在运行本例,然后重启一下模拟器,看看LogCat视图中是否输出了相应的日志信息。

原文地址:https://www.cnblogs.com/xgjblog/p/3927823.html