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

Activity和Service相互通信

时间:2015-01-12 19:18:03      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

在做音乐播放器的时候,要在Activity不绑定Service的前提下(如果绑定了,Activity挂了Service也就挂了)进行相互传输数据,Activity向Service传输数据可以使用Intent,Service向Activity传输数据可以使用广播机制,一般代码如下:
标签: Android SDK

[1].[代码] [Java]代码 跳至 [1]

?
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
1、在Activity中写一个内部类实现广播的接收:
private class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    publicvoid onReceive(Context context, Intent intent) {
        Toast.makeText(MainActivity.this, intent.getStringExtra("back"), Toast.LENGTH_SHORT).show();
    }
}
 
2、并在Activity中动态的注册这个监听器:
private BroadcastReceiver rec = null;
this.rec = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("org.load.action.MSG");/ 这里的Action要和Service发送广播时使用的Action一样
MainActivity.this.registerReceiver(this.rec, filter);  // 动态注册BroadcastReceiver
 
3、在Service中实现广播的发送:
ntent it = new Intent("org.load.action.MSG");// 这里要直接放Action的参数,不能是指定一个对象和一个class,并且这里的Action要和Service发送广播时使用的Action一样
it.putExtra("back", this.msg + ",来自Service");
super.sendBroadcast(it);
 
4、注意:要在Manifest中注册这个Service
 
全部代码:
MainActivity.java:
publicclass MainActivity extends Activity {
    private Button but = null;
    private BroadcastReceiver rec = null;
     
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
        this.but = (Button) super.findViewById(R.id.but);
        this.but.setOnClickListener(new OnClickAdapter());
     
        this.rec = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("org.load.action.MSG");  // 这里的Action要和Service发送广播时使用的Action一样
        MainActivity.this.registerReceiver(this.rec, filter);  // 动态注册BroadcastReceiver
    }
 
    privateclass OnClickAdapter implements OnClickListener {
        @Override
        publicvoid onClick(View v) {
            Intent intent = new Intent(MainActivity.this, MyService.class);
            intent.setAction("org.load.action.ACT");
            intent.putExtra("msg", "这条消息要通过service返回");
            MainActivity.this.startService(intent);
    }
}
 
    privateclass MyBroadcastReceiver extends BroadcastReceiver {
        @Override
        publicvoid onReceive(Context context, Intent intent) {
            Toast.makeText(MainActivity.this, intent.getStringExtra("back"), Toast.LENGTH_SHORT).show();
        }
    }
}
 
MyService.java:
publicclass MyService extends Service {
    private String msg = null;
     
    @Override
    public IBinder onBind(Intent intent) {
        returnnull;
    }
     
    @Override
    publicvoid onCreate() {
        super.onCreate();
    }
     
    @Override
    publicint onStartCommand(Intent intent, int flags, int startId) {
        if("org.load.action.ACT".equals(intent.getAction())) {
            this.msg = intent.getStringExtra("msg");
             
                this.sendBroadcastToActivity();
            }
         
        returnsuper.onStartCommand(intent, flags, startId);
    }
     
    @Override
    publicvoid onDestroy() {
        super.onDestroy();
    }
     
     
    publicvoid sendBroadcastToActivity() {
        Intent it = new Intent("org.load.action.MSG");
        it.putExtra("back", this.msg + ",来自Service");
        super.sendBroadcast(it);
    }
}
  
Manifest.xml:
<service android:name="org.load.test.MyService" />

Activity和Service相互通信

标签:

原文地址:http://blog.csdn.net/u014311037/article/details/42645991

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