码迷,mamicode.com
首页 > 其他好文 > 详细

Aactivity和Service之间的通信

时间:2015-09-18 21:58:34      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

一、在activity中定义三个按钮 一个开启服务  一个关闭服务,还有一个是向服务发送广播

当创建出Serevice时先执行Service的onCreate()创建服务后只执行一次 以后每次点击开启服务都不会再执行onCreate()而是去执行onStartCommand()停止服务时执行Service的onDestroy()

 二、在Activity中点击发送广播键会向服务发送广播(本例采用LocalBroadcastManager发送和接受广播)服务接收广播吐司出“接收到activity的广播”。服务是在oncreate()里边创建接受者不在onStartcommand()里边创建是因为每次点击开启服务时都会执行onStartcommand()    activity创建出service时在service中的oncreate()里向activity发送广播   activity在oncreate()里创建出接受者。

三、其实就是双方都有一个发送者和接收者

 

技术分享

 

 

看代码

技术分享
 1 package com.qf.service01;
 2 
 3 import android.app.Activity;
 4 import android.content.BroadcastReceiver;
 5 import android.content.Context;
 6 import android.content.Intent;
 7 import android.content.IntentFilter;
 8 import android.os.Bundle;
 9 import android.support.v4.content.LocalBroadcastManager;
10 import android.view.View;
11 import android.widget.Toast;
12 
13 public class MainActivity extends Activity {
14 
15     Intent serviceIntent;
16     MyReceiver myReceiver;
17     LocalBroadcastManager localBroadcastMgr;//本地广播管理器
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         
23         serviceIntent=new Intent(getApplicationContext(),MyService.class);
24         localBroadcastMgr=LocalBroadcastManager.getInstance(getApplicationContext());
25         myReceiver=new MyReceiver();
26         localBroadcastMgr.registerReceiver(myReceiver, new IntentFilter("com.qf.broadcast.disen_service"));
27     }
28     
29     
30     class MyReceiver extends BroadcastReceiver{
31         @Override
32         public void onReceive(Context context, Intent intent) {
33             Toast.makeText(MainActivity.this, "收到service的广播", Toast.LENGTH_SHORT).show();
34             
35         }
36     }
37 
38     public void start(View v) {
39         startService(serviceIntent);
40     }
41 
42     public void stop(View v) {
43         stopService(serviceIntent);
44     }
45     public void startService(View v) {
46         localBroadcastMgr=LocalBroadcastManager.getInstance(getApplicationContext());
47         Intent intent1=new Intent("com.qf.broadcast.activity");
48 
49         localBroadcastMgr.sendBroadcast(intent1);
50     }
51 
52 
53     @Override
54     protected void onDestroy() {
55         super.onDestroy();
56         
57         //取消注册本地广播接收器
58         localBroadcastMgr.unregisterReceiver(myReceiver);
59     }
60 }
MainActivity.java
技术分享
 1 package com.qf.service01;
 2 
 3 import android.app.Service;
 4 import android.content.BroadcastReceiver;
 5 import android.content.Context;
 6 import android.content.Intent;
 7 import android.content.IntentFilter;
 8 import android.os.IBinder;
 9 import android.support.v4.content.LocalBroadcastManager;
10 import android.util.Log;
11 import android.widget.Toast;
12 
13 public class MyService extends Service {
14 
15     LocalBroadcastManager localBroadcastMgr;//本地广播管理器
16     MyReceiver myReceiver;
17     public void onCreate() { //只执行一次,用于初始化Service
18         super.onCreate();
19         Log.i("debug", "onCreate");
20         
21         myReceiver=new MyReceiver();
22         localBroadcastMgr=LocalBroadcastManager.getInstance(getApplicationContext());
23         localBroadcastMgr.registerReceiver(myReceiver, new IntentFilter("com.qf.broadcast.activity"));
24     }
25     class MyReceiver extends BroadcastReceiver{
26         @Override
27         public void onReceive(Context context, Intent intent) {
28             Toast.makeText(context, "收到activity的广播", Toast.LENGTH_SHORT).show();
29             
30         }
31     }
32     
33     @Override
34     public int onStartCommand(Intent intent, int flags, int startId) {
35         // TODO 每次启动Service都会执行的方法,在此实现核心的功能
36         Log.i("debug", "onStartCommand");        
37     
38          Intent intent1=new Intent("com.qf.broadcast.disen_service");
39         localBroadcastMgr.sendBroadcast(intent1);    
40         
41         return super.onStartCommand(intent, flags, startId);
42     }
43     
44     
45     
46     @Override
47     public IBinder onBind(Intent intent) {
48         return null;
49     }
50     
51     @Override
52     public void onDestroy() { //只执行一次,用于销毁Service组件
53         super.onDestroy();
54         Log.i("debug", "onDestroy");
55         localBroadcastMgr.unregisterReceiver(myReceiver);
56     }
57 
58 }
Service.java

 

技术分享
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <Button
12         android:id="@+id/btn1Id"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:onClick="start"
16         android:text="启动服务" />
17     
18     <Button
19         android:id="@+id/btn2Id"
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:onClick="stop"
23         android:text="停止服务" 
24         android:layout_below="@id/btn1Id"/>
25     <Button
26         android:id="@+id/btn3Id"
27         android:layout_width="wrap_content"
28         android:layout_height="wrap_content"
29         android:onClick="startService"
30         android:layout_below="@id/btn2Id"
31         android:text="向服务发广播" />
32 
33 </RelativeLayout>
activity_main.xml
技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.qf.service01"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
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.qf.service01.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         
26         <!-- 注册Service组件 -->
27        <service android:name="com.qf.service01.MyService"/>
28     </application>
29 
30 </manifest>
AndroidManifest.xml

 

Aactivity和Service之间的通信

标签:

原文地址:http://www.cnblogs.com/bimingcong/p/4820386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!