zoukankan      html  css  js  c++  java
  • 第92章、广播之三通过广播启动服务(从零开始学Android)

    转自:http://blog.csdn.net/jianghuiquan/article/details/8641152

    Service(服务)在Android中地位是至关重要的,我们可以通过Activity与Broadcast(广播)启动Service(服务),我们本章学习如何通过广播Broadcast启动服务Service。

      也许你会说,能用Activity启动,干嘛要用广播呢?——且听电话监听、短信监听再作分解!

    一、设计界面

      1、布局文件

      打开res/layout/activity_main.xml文件。
      输入以下代码:

    [html] view plaincopy
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2.   
    3. <LinearLayout   
    4.     xmlns:android="http://schemas.android.com/apk/res/android"   
    5.     android:orientation="vertical"   
    6.     android:layout_width="fill_parent"   
    7.     android:layout_height="fill_parent">  
    8.   
    9.     <Button  
    10.         android:id="@+id/send"  
    11.         android:layout_width="wrap_content"  
    12.         android:layout_height="wrap_content"  
    13.         android:text="通过Broadcast启动Service" />  
    14.   
    15. </LinearLayout>  


    二、程序文件

      1、创建“src/com.genwoxue.broadcastservice/ServiceUtil.java”文件。
      然后输入以下代码:

    [java] view plaincopy
    1. package com.genwoxue.broadcastservice;  
    2.   
    3. import android.app.Service;  
    4. import android.content.Intent;  
    5. import android.os.IBinder;  
    6. import android.util.Log;  
    7.   
    8. public class ServiceUtil extends Service{  
    9.       
    10.     private static final String TAG="AboutService";   
    11.     @Override  
    12.     public IBinder onBind(Intent intent){  
    13.         return null;  
    14.     }  
    15.       
    16.     @Override  
    17.     public void onCreate(){  
    18.         Log.i(TAG,"服务:onCreate()");  
    19.     }  
    20.       
    21.     //启动  
    22.     @Override  
    23.     public int onStartCommand(Intent intent,int flags,int startId){  
    24.         Log.i(TAG, "服务启动:onStart()=>Intent"+intent+",startID="+startId);  
    25.         return Service.START_CONTINUATION_MASK;  
    26.     }  
    27.       
    28.     @Override  
    29.     public void onDestroy(){  
    30.         Log.i(TAG,"服务:onDestroy()");  
    31.     }  
    32.   
    33. }  

      2、创建“src/com.genwoxue.broadcastservice/BroadcastReceiverUtil.java”文件。
      然后输入以下代码:

    [java] view plaincopy
    1. package com.genwoxue.broadcastservice;  
    2.   
    3. import android.content.BroadcastReceiver;  
    4. import android.content.Context;  
    5. import android.content.Intent;  
    6.   
    7. public class BroadcastReceiverUtil extends BroadcastReceiver{  
    8.     @Override  
    9.     public void onReceive(Context context,Intent intent){  
    10.         //广播接收器(接收方)判断Action为“com.genwoxue.action.ABOUTSERVICE”则启动服务  
    11.         if("com.genwoxue.action.ABOUTSERVICE".equals(intent.getAction())){  
    12.             context.startService(new Intent(context,ServiceUtil.class));  
    13.         }  
    14.           
    15.     }  
    16. }  

      3、打开“src/com.genwoxue.broadcastservice/MainActivity.java”文件。
      然后输入以下代码:

    [java] view plaincopy
    1. package com.genwoxue.broadcastservice;  
    2.   
    3.   
    4. import android.os.Bundle;  
    5. import android.view.View;  
    6. import android.view.View.OnClickListener;  
    7. import android.widget.Button;  
    8. import android.app.Activity;  
    9. import android.content.Intent;  
    10. import android.content.IntentFilter;  
    11.   
    12. public class MainActivity extends Activity {  
    13.   
    14.     private BroadcastReceiverUtil util=null;  
    15.     private Button btnSend=null;  
    16.       
    17.     @Override  
    18.     protected void onCreate(Bundle savedInstanceState) {  
    19.         super.onCreate(savedInstanceState);  
    20.         setContentView(R.layout.activity_main);  
    21.           
    22.         btnSend=(Button)super.findViewById(R.id.send);  
    23.         btnSend.setOnClickListener(new OnClickListener(){  
    24.             @Override  
    25.             public void onClick(View v){  
    26.                 //发送广播:其Action为“com.genwoxue.action.ABOUTSERVICE”  
    27.                 Intent intent=new Intent("com.genwoxue.action.ABOUTSERVICE");  
    28.                 MainActivity.this.sendBroadcast(intent);  
    29.                   
    30.                   
    31.                 //实例化广播过滤器(只过滤其Action为"com.genwoxue.action.ABOUTSERVICE")  
    32.                 IntentFilter filter=new IntentFilter("com.genwoxue.action.ABOUTSERVICE");  
    33.                 //实例化广播接收器(接收方)  
    34.                 util=new BroadcastReceiverUtil();  
    35.                 //注册BroadcastReceiver:参数为接收器与过滤器  
    36.                 MainActivity.this.registerReceiver(util, filter);  
    37.             }  
    38.         });  
    39.     }  
    40.       
    41.     @Override  
    42.     protected void onStop(){  
    43.         //停止广播  
    44.         super.unregisterReceiver(util);  
    45.         super.onStop();  
    46.           
    47.         //停止服务  
    48.         Intent intent=new Intent(MainActivity.this,ServiceUtil.class);  
    49.         MainActivity.this.stopService(intent);  
    50.     }  
    51.   
    52. }  


    三、配置文件

      打开“AndroidManifest.xml”文件。

      然后输入以下代码:

    [java] view plaincopy
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="com.genwoxue.broadcastservice"  
    4.     android:versionCode="1"  
    5.     android:versionName="1.0" >  
    6.   
    7.     <uses-sdk  
    8.         android:minSdkVersion="10"  
    9.         android:targetSdkVersion="15" />  
    10.   
    11.     <application  
    12.         android:allowBackup="true"  
    13.         android:icon="@drawable/ic_launcher"  
    14.         android:label="@string/app_name"  
    15.         android:theme="@style/AppTheme" >  
    16.       <activity  
    17.             android:name="com.genwoxue.broadcastservice.MainActivity"  
    18.             android:label="@string/app_name" >  
    19.             <intent-filter>  
    20.                 <action android:name="android.intent.action.MAIN" />  
    21.                 <category android:name="android.intent.category.LAUNCHER" />  
    22.             </intent-filter>  
    23.         </activity>  
    24.         <service android:name="com.genwoxue.broadcastservice.ServiceUtil" />  
    25.     </application>  
    26.   
    27. </manifest>  


    注意:需要在AndroidManifest.xml文件中添加<service...>:

      <service android:name="com.genwoxue.broadcastservice.ServiceUtil" />

    四、运行结果

      Activity发送广播—>广播接收方启动Service服务

       

      

      


  • 相关阅读:
    VS2015中SharedProject与可移植类库(PCL)项目
    Windows.Web.Http.HttpClient.GetStringAsync 总是返回相同的结果
    博客园新闻WP8.1客户端
    Webpack 2 视频教程 001
    快速零配置迁移 API 适配 iOS 对 IPv6 以及 HTTPS 的要求
    免费的 Vue.js 入门与进阶视频教程
    Webpack 3 中的新特性
    使用可视化图表对 Webpack 2 的编译与打包进行统计分析
    React.js 开发参见问题 Q&A
    官方 React 快速上手脚手架 create-react-app
  • 原文地址:https://www.cnblogs.com/walccott/p/4957605.html
Copyright © 2011-2022 走看看