Aactivity和Service之间的通信

时间:2024-10-21 20:37:09

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

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

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

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

Aactivity和Service之间的通信

看代码

 package com.qf.service01;

 import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity { Intent serviceIntent;
MyReceiver myReceiver;
LocalBroadcastManager localBroadcastMgr;//本地广播管理器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); serviceIntent=new Intent(getApplicationContext(),MyService.class);
localBroadcastMgr=LocalBroadcastManager.getInstance(getApplicationContext());
myReceiver=new MyReceiver();
localBroadcastMgr.registerReceiver(myReceiver, new IntentFilter("com.qf.broadcast.disen_service"));
} class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainActivity.this, "收到service的广播", Toast.LENGTH_SHORT).show(); }
} public void start(View v) {
startService(serviceIntent);
} public void stop(View v) {
stopService(serviceIntent);
}
public void startService(View v) {
localBroadcastMgr=LocalBroadcastManager.getInstance(getApplicationContext());
Intent intent1=new Intent("com.qf.broadcast.activity"); localBroadcastMgr.sendBroadcast(intent1);
} @Override
protected void onDestroy() {
super.onDestroy(); //取消注册本地广播接收器
localBroadcastMgr.unregisterReceiver(myReceiver);
}
}

MainActivity.java

 package com.qf.service01;

 import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast; public class MyService extends Service { LocalBroadcastManager localBroadcastMgr;//本地广播管理器
MyReceiver myReceiver;
public void onCreate() { //只执行一次,用于初始化Service
super.onCreate();
Log.i("debug", "onCreate"); myReceiver=new MyReceiver();
localBroadcastMgr=LocalBroadcastManager.getInstance(getApplicationContext());
localBroadcastMgr.registerReceiver(myReceiver, new IntentFilter("com.qf.broadcast.activity"));
}
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "收到activity的广播", Toast.LENGTH_SHORT).show(); }
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO 每次启动Service都会执行的方法,在此实现核心的功能
Log.i("debug", "onStartCommand"); Intent intent1=new Intent("com.qf.broadcast.disen_service");
localBroadcastMgr.sendBroadcast(intent1); return super.onStartCommand(intent, flags, startId);
} @Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onDestroy() { //只执行一次,用于销毁Service组件
super.onDestroy();
Log.i("debug", "onDestroy");
localBroadcastMgr.unregisterReceiver(myReceiver);
} }

Service.java

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/btn1Id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="start"
android:text="启动服务" /> <Button
android:id="@+id/btn2Id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="停止服务"
android:layout_below="@id/btn1Id"/>
<Button
android:id="@+id/btn3Id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startService"
android:layout_below="@id/btn2Id"
android:text="向服务发广播" /> </RelativeLayout>

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qf.service01"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.qf.service01.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组件 -->
<service android:name="com.qf.service01.MyService"/>
</application> </manifest>

AndroidManifest.xml

相关文章