第92章、广播之三通过广播启动服务(从零开始学Android)
2013-03-14 16:24
405 查看
Service(服务)在Android中地位是至关重要的,我们可以通过Activity与Broadcast(广播)启动Service(服务),我们本章学习如何通过广播Broadcast启动服务Service。
也许你会说,能用Activity启动,干嘛要用广播呢?——且听电话监听、短信监听再作分解!
一、设计界面
1、布局文件
打开res/layout/activity_main.xml文件。
输入以下代码:
二、程序文件
1、创建“src/com.genwoxue.broadcastservice/ServiceUtil.java”文件。
然后输入以下代码:
2、创建“src/com.genwoxue.broadcastservice/BroadcastReceiverUtil.java”文件。
然后输入以下代码:
3、打开“src/com.genwoxue.broadcastservice/MainActivity.java”文件。
然后输入以下代码:
三、配置文件
打开“AndroidManifest.xml”文件。
然后输入以下代码:
注意:需要在AndroidManifest.xml文件中添加<service...>:
<service android:name="com.genwoxue.broadcastservice.ServiceUtil" />
四、运行结果
Activity发送广播—>广播接收方启动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服务
相关文章推荐
- 第92章、广播之三通过广播启动服务(从零开始学Android)
- android 音乐播放 启动方式 (3)服务通过发送广播来控制activity显示进度等
- Android 启动Service服务和发送Broadcast广播的常用方法
- Broadcast与service----------通过广播启动服务
- 使用AlarmManager启动广播、服务、页面(Android定时器) 分类: Android 2015-07-14 17:49 13人阅读 评论(0) 收藏
- Android 利用广播接收器启动服务
- Android学习篇章35-Service基础-广播开机启动服务
- Android 监听开机广播启动服务 动态注册时间变化监听广播
- 使用AlarmManager启动广播、服务、页面(Android定时器)
- Android基础总结.doc(第六节、 Activity组件的启动模式、广播、服务组件 )
- android之4.0开机自接受广播,启动服务的问题
- Android 通过广播启动另一个应用的Activity
- Android通过广播接收者调用服务(service)内方法
- Android通过广播接收者调用服务内方法
- Android 4.0及以上版本接收开机广播BOOT_COMPLETED、开机自启动服务
- android:启动服务;广播(最高优先窃听信息)并转发给别人
- Android 4.0及以上版本接收开机广播BOOT_COMPLETED、开机自启动服务
- android 如何通过服务或广播来开启APP,而不是直接点图标
- 通过广播接收者(BroadcastReceiver)或守护线程启动服务,录音机监听电话
- android用服务service与广播broadcastreceiver创建开机启动服务