intent 和 Broadcast Receiver之间的通信
2011-08-02 10:07
225 查看
工作原理是通过封装一个service启动的一个线程产生的随机数封装到intent对象传递给Activity,Activity接受到后讲结果输出到屏幕。
java的代码:
package jm.out;
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.view.View;//引入相关包
import android.view.View.OnClickListener;//引入相关包
import android.widget.Button;//引入相关包
import android.widget.TextView;//引入相关包
//继承自Activity的子类
public class IntentBroadcastActivity extends Activity {
public static final int CMD_STOP_SERVICE = 0;
Button btnStart;//开始服务Button对象应用
Button btnStop;//停止服务Button对象应用
TextView tv;//TextView对象应用
DataReceiver dataReceiver;//BroadcastReceiver对象
@Override
public void onCreate(Bundle savedInstanceState) {//重写onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//设置显示的屏幕
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
tv = (TextView)findViewById(R.id.tv);
btnStart.setOnClickListener(new OnClickListener() {//为按钮添加点击事件监听
@Override
public void onClick(View v) {//重写onClick方法
Intent myIntent = new Intent(IntentBroadcastActivity.this, jm.out.MyService.class);
IntentBroadcastActivity.this.startService(myIntent);//发送Intent启动Service
}
});
btnStop.setOnClickListener(new OnClickListener() {//为按钮添加点击事件监听
@Override
public void onClick(View v) {//重写onClick方法
Intent myIntent = new Intent();//创建Intent对象
myIntent.setAction("wyf.wpf.MyService");
myIntent.putExtra("cmd", CMD_STOP_SERVICE);
sendBroadcast(myIntent);//发送广播
}
});
}
private class DataReceiver extends BroadcastReceiver{//继承自BroadcastReceiver的子类
@Override
public void onReceive(Context context, Intent intent) {//重写onReceive方法
double data = intent.getDoubleExtra("data", 0);
tv.setText("Service的数据为:"+data);
}
}
@Override
protected void onStart() {//重写onStart方法
dataReceiver = new DataReceiver();
IntentFilter filter = new IntentFilter();//创建IntentFilter对象
filter.addAction("wyf.wpf.Sample_3_6");
registerReceiver(dataReceiver, filter);//注册Broadcast Receiver
super.onStart();
}
@Override
protected void onStop() {//重写onStop方法
unregisterReceiver(dataReceiver);//取消注册Broadcast Receiver
super.onStop();
}
}
service类:
package jm.out;
import android.app.Service;//引入相关包
import android.content.BroadcastReceiver;//引入相关包
import android.content.Context;//引入相关包
import android.content.Intent;//引入相关包
import android.content.IntentFilter;//引入相关包
import android.os.IBinder;//引入相关包
//继承自Service的子类
public class MyService extends Service{
CommandReceiver cmdReceiver;
boolean flag;
@Override
public void onCreate() {//重写onCreate方法
flag = true;
cmdReceiver = new CommandReceiver();
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {//重写onBind方法
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {//重写onStartCommand方法
IntentFilter filter = new IntentFilter();//创建IntentFilter对象
filter.addAction("jm.out.MyService");
registerReceiver(cmdReceiver, filter);//注册Broadcast Receiver
doJob();//调用方法启动线程
return super.onStartCommand(intent, flags, startId);
}
//方法:
public void doJob(){
new Thread(){
public void run(){
while(flag){
try{//睡眠一段时间
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
Intent intent = new Intent();//创建Intent对象
intent.setAction("jm.out.IntentBroadcastActivity");
intent.putExtra("data", Math.random());
sendBroadcast(intent);//发送广播
}
}
}.start();
}
private class CommandReceiver extends BroadcastReceiver{//继承自BroadcastReceiver的子类
@Override
public void onReceive(Context context, Intent intent) {//重写onReceive方法
int cmd = intent.getIntExtra("cmd", -1);//获取Extra信息
if(cmd == IntentBroadcastActivity.CMD_STOP_SERVICE){//如果发来的消息是停止服务
flag = false;//停止线程
stopSelf();//停止服务
}
}
}
@Override
public void onDestroy() {//重写onDestroy方法
this.unregisterReceiver(cmdReceiver);//取消注册的CommandReceiver
super.onDestroy();
}
}
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/btnStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动服务" />
<Button android:id="@+id/btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止服务" />
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="等待来自Service的数据" />
</LinearLayout>
androidmanifest.xml的配置:
<?xml version="1.0" encoding="utf-8"?>
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wyf.wpf" android:versionCode="1" android:versionName="1.0">
-<application android:icon="@drawable/icon"
android:label="@string/app_name">
-<activity android:name=".Sample_3_6"
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=".MyService"
android:process=":remote">
-<intent-filter>
<actionandroid:name="wyf.wpf.MyService"
/>
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
要注意的是红色代码,只有这样才可以让service在activity退出后还能运行,保证了主线程停止,service还能在后台运行。
java的代码:
package jm.out;
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.view.View;//引入相关包
import android.view.View.OnClickListener;//引入相关包
import android.widget.Button;//引入相关包
import android.widget.TextView;//引入相关包
//继承自Activity的子类
public class IntentBroadcastActivity extends Activity {
public static final int CMD_STOP_SERVICE = 0;
Button btnStart;//开始服务Button对象应用
Button btnStop;//停止服务Button对象应用
TextView tv;//TextView对象应用
DataReceiver dataReceiver;//BroadcastReceiver对象
@Override
public void onCreate(Bundle savedInstanceState) {//重写onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//设置显示的屏幕
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
tv = (TextView)findViewById(R.id.tv);
btnStart.setOnClickListener(new OnClickListener() {//为按钮添加点击事件监听
@Override
public void onClick(View v) {//重写onClick方法
Intent myIntent = new Intent(IntentBroadcastActivity.this, jm.out.MyService.class);
IntentBroadcastActivity.this.startService(myIntent);//发送Intent启动Service
}
});
btnStop.setOnClickListener(new OnClickListener() {//为按钮添加点击事件监听
@Override
public void onClick(View v) {//重写onClick方法
Intent myIntent = new Intent();//创建Intent对象
myIntent.setAction("wyf.wpf.MyService");
myIntent.putExtra("cmd", CMD_STOP_SERVICE);
sendBroadcast(myIntent);//发送广播
}
});
}
private class DataReceiver extends BroadcastReceiver{//继承自BroadcastReceiver的子类
@Override
public void onReceive(Context context, Intent intent) {//重写onReceive方法
double data = intent.getDoubleExtra("data", 0);
tv.setText("Service的数据为:"+data);
}
}
@Override
protected void onStart() {//重写onStart方法
dataReceiver = new DataReceiver();
IntentFilter filter = new IntentFilter();//创建IntentFilter对象
filter.addAction("wyf.wpf.Sample_3_6");
registerReceiver(dataReceiver, filter);//注册Broadcast Receiver
super.onStart();
}
@Override
protected void onStop() {//重写onStop方法
unregisterReceiver(dataReceiver);//取消注册Broadcast Receiver
super.onStop();
}
}
service类:
package jm.out;
import android.app.Service;//引入相关包
import android.content.BroadcastReceiver;//引入相关包
import android.content.Context;//引入相关包
import android.content.Intent;//引入相关包
import android.content.IntentFilter;//引入相关包
import android.os.IBinder;//引入相关包
//继承自Service的子类
public class MyService extends Service{
CommandReceiver cmdReceiver;
boolean flag;
@Override
public void onCreate() {//重写onCreate方法
flag = true;
cmdReceiver = new CommandReceiver();
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {//重写onBind方法
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {//重写onStartCommand方法
IntentFilter filter = new IntentFilter();//创建IntentFilter对象
filter.addAction("jm.out.MyService");
registerReceiver(cmdReceiver, filter);//注册Broadcast Receiver
doJob();//调用方法启动线程
return super.onStartCommand(intent, flags, startId);
}
//方法:
public void doJob(){
new Thread(){
public void run(){
while(flag){
try{//睡眠一段时间
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
Intent intent = new Intent();//创建Intent对象
intent.setAction("jm.out.IntentBroadcastActivity");
intent.putExtra("data", Math.random());
sendBroadcast(intent);//发送广播
}
}
}.start();
}
private class CommandReceiver extends BroadcastReceiver{//继承自BroadcastReceiver的子类
@Override
public void onReceive(Context context, Intent intent) {//重写onReceive方法
int cmd = intent.getIntExtra("cmd", -1);//获取Extra信息
if(cmd == IntentBroadcastActivity.CMD_STOP_SERVICE){//如果发来的消息是停止服务
flag = false;//停止线程
stopSelf();//停止服务
}
}
}
@Override
public void onDestroy() {//重写onDestroy方法
this.unregisterReceiver(cmdReceiver);//取消注册的CommandReceiver
super.onDestroy();
}
}
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/btnStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动服务" />
<Button android:id="@+id/btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止服务" />
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="等待来自Service的数据" />
</LinearLayout>
androidmanifest.xml的配置:
<?xml version="1.0" encoding="utf-8"?>
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wyf.wpf" android:versionCode="1" android:versionName="1.0">
-<application android:icon="@drawable/icon"
android:label="@string/app_name">
-<activity android:name=".Sample_3_6"
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=".MyService"
android:process=":remote">
-<intent-filter>
<actionandroid:name="wyf.wpf.MyService"
/>
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
要注意的是红色代码,只有这样才可以让service在activity退出后还能运行,保证了主线程停止,service还能在后台运行。
相关文章推荐
- 使用EventBus在Activity与BroadcastReceiver之间进行通信
- Service、BroadcastReceiver、Activity之间的通信
- Android中Activity与Service通信——Broadcast、Receiver、Intent、Bundle
- Android中Activity与Service通信——Broadcast、Receiver、Intent、Bundle
- 使用Intent实现界面之间双向数据通信
- Intent实现Broadcast Receiver 实例
- activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver解释
- Android Intent,handler,BroadcastReceiver,ContentObserver区别
- Android前台画面和后台service之间通信的方法之Broadcast
- Service Receiver Activity 之间的通信
- 简要解释一下activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver .
- 简要解释一下activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver
- Intent、Bundle——实现Activity之间的通信
- Android学习08Android应用程序组件之间的通信Intent和IntentFilter
- 利用IntentService实现在broadcastreceiver中比较耗时的任务
- Android编程五要诀:Activity、Service、BroadcastReceiver、ContentProvider、Intent
- 简要解释一下activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver
- 利用Intent进行Activity之间的通信(2)
- Android中BroadCast与Activity之间的通信
- 简要解释一下activity、 intent 、intent filter、service、Broadcast、BroadcaseReceiver