add action 方法是添加要接受的intent对象。 相关查看。sporttery 中的 SportteryMainActivity 的广播接受者
在Android中,异步更新UI,通常我们会选用Handler启动线程,或者sendMessage的方式,那么利用Service和广播也可以更新UI的,例子如下: 我们建立一个Service:
package com.andy
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("wyf.wpf.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("wyf.wpf.Sample_3_6");
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 ==
Sample_3_6.CMD_STOP_SERVICE){//如果发来的消息是停止服务
flag = false;//停止线程
stopSelf();//停止服务
}
}
}
@Override
public void onDestroy()
{//重写onDestroy方法
this.unregisterReceiver(cmdReceiver);//取消注册的CommandReceiver
super.onDestroy();
}
}
建立一个activity
package com.andy;//声明包语句
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 Sample_3_6 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(Sample_3_6.this,
wyf.wpf.MyService.class);
Sample_3_6.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();
}
}