原文地址::http://www.pocketdigi.com/20110303/197.html
今天学习到Service,在用Service下载文件时,一个问题就是Service没有界面,如何通知用户当前下载的进度,Service直接向Activity传数据有点麻烦,于是想到了用Broadcast,Service广播,Activity负责接收,再对接收到的数据进行处理,就达到了我们的目的。下面是今天这个程序的运行效果:
先注册Receiver,然后开始Service,上面的TextView和ProgressBar就会随着Service传过来的值变,解除注册Receiver或者结束Service后不会再变。
Service代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
| package com.services;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class TestService extends Service {
boolean isStop=false;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i("TAG","绑定");
return null;
}
public void onCreate(){
Log.i("TAG","Services onCreate");
super.onCreate();
}
public void onStart(Intent intent,int startId){
Log.i("TAG","Services onStart");
super.onStart(intent, startId);
new Thread(){//新建线程,每隔1秒发送一次广播,同时把i放进intent传出
public void run(){
int i=0;
while(!isStop){
Intent intent=new Intent();
intent.putExtra("i", i);
i++;
intent.setAction("android.intent.action.test");//action与接收器相同
sendBroadcast(intent);
Log.i("TAG",String.valueOf(i));
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
@Override
public void onDestroy() {
Log.i("TAG","Services onDestory");
isStop=true;//即使service销毁线程也不会停止,所以这里通过设置isStop来停止线程
super.onDestroy();
}
} |
Activity代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
| package com.services;
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.ProgressBar;
import android.widget.TextView;
public class main extends Activity {
/** Called when the activity is first created. */
Button b1,b2,b3,b4;
TestService mService;
ProgressBar pb;
MyReceiver receiver;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);
b3=(Button)findViewById(R.id.b3);
b4=(Button)findViewById(R.id.b4);
b1.setOnClickListener(l1);
b2.setOnClickListener(l2);
b3.setOnClickListener(l3);
b4.setOnClickListener(l4);
pb=(ProgressBar)findViewById(R.id.pb);
tv=(TextView)findViewById(R.id.tv);
}
public class MyReceiver extends BroadcastReceiver {
//自定义一个广播接收器
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("OnReceiver");
Bundle bundle=intent.getExtras();
int a=bundle.getInt("i");
pb.setProgress(a);
tv.setText(String.valueOf(a));
//处理接收到的内容
}
public MyReceiver(){
System.out.println("MyReceiver");
//构造函数,做一些初始化工作,本例中无任何作用
}
}
OnClickListener l1=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(new Intent(main.this, TestService.class));
//开始服务
}
};
OnClickListener l2=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(new Intent(main.this, TestService.class));
//结束服务
}
};
OnClickListener l3=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//注册接收器
receiver=new MyReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction("android.intent.action.test");
main.this.registerReceiver(receiver,filter);
}
};
OnClickListener l4=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
main.this.unregisterReceiver(receiver);
//解除注册接收器
}
};
} |