第一種方法:
遇到一個問題,需要在一個activity中控制另一個acitivity做一些更新,沒想到傳遞handler的方法,通過以下方式解決。
1.在MyAPP中定義屬性handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package jason.com; import jason.com.MasterActivity.MyHandler; import android.app.Application; /** *
自己實現Application,實現數據共享 *
@author jason */ public class MyAPP extends Application
{ //
共享變量 private MyHandler
handler = null ; //
set方法 public void setHandler(MyHandler
handler) { this .handler
= handler; } //
get方法 public MyHandler
getHandler() { return handler; } } |
2、在主activity 中給MyAPP的屬性handler賦值
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
|
@Override public void onCreate(Bundle
savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); mAPP
= (MyAPP) getApplication(); handler
= new MyHandler(); tv
= (TextView) findViewById(R.id.tv); btn_to
= (Button) findViewById(R.id.btn_to); //
設置監聽器 btn_to.setOnClickListener( new OnClickListener()
{ @Override public void onClick(View
v) { //
設置共享變量 mAPP.setHandler(handler); //
啓動另一個Activity Intent
intent = new Intent(MasterActivity. this , ToChangeViewActivity. class ); startActivity(intent); } }); } |
3、在另一個activity中獲取MyAPP中handler進行傳值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
protected void onCreate(Bundle
savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.show); mAPP
= (MyAPP) getApplication(); //
獲得該共享變量實例 mHandler
= mAPP.getHandler(); findViewById(R.id.btn_chang).setOnClickListener( new OnClickListener()
{ @Override public void onClick(View
v) { //
發送消息 mHandler.sendEmptyMessage(CHANGED); ToChangeViewActivity. this .finish(); } }); } |
第二種方法:
看到作者的這篇文章不錯,就轉了過來。兩個Activity之間可以通過發送廣播來控制另外一個activity的UI更新,比如團購APP裏:我下了個訂單之後,那麼我可以通過發送廣播通知“我的訂單”裏面的內容內容自動更新。
再重複一遍我遇到的問題,就是在MainActivity裏面打開AnotherActivity去執行一些操作,相應的改變MainActivity裏的一些佈局或者執行一些動作,最開始想到的就是把MainActivity的Handler直接傳給AnotherActivity,好像不可行,就有了這篇和上一篇文章。
上一篇方案一是通過重寫application來在兩個activity之間共享Handler的,今天這個方案是通過廣播機制解決本來想要通過傳遞handler來實現的功能,算是Activity之間傳遞Handler問題的變通方案,
其實很簡單,就是Broadcast的應用,替換了原來想要通過共享handler解決的思路。
代碼如下:
MainActivity:
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
|
package jason.broadcastinsteadofhanlderdemo; 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; public class MainActivity extends Activity
{ TextView
textView; Button
sButton; @Override protected void onCreate(Bundle
savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView
= (TextView) findViewById(R.id.show); sButton
= (Button) findViewById(R.id.startAnother); sButton.setOnClickListener( new OnClickListener()
{ @Override public void onClick(View
v) { //
TODO Auto-generated method stub startActivity( new Intent(MainActivity. this ,AnotherActivity. class )); } }); IntentFilter
filter = new IntentFilter(AnotherActivity.action); registerReceiver(broadcastReceiver,
filter); } BroadcastReceiver
broadcastReceiver = new BroadcastReceiver()
{ @Override public void onReceive(Context
context, Intent intent) { //
TODO Auto-generated method stub textView.setText(intent.getExtras().getString( "data" )); } }; protected void onDestroy()
{ unregisterReceiver(broadcastReceiver); }; } |
AnotherActivity:
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
|
package jason.broadcastinsteadofhanlderdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class AnotherActivity extends Activity
{ public static final String
action = "jason.broadcast.action" ; Button
update; @Override protected void onCreate(Bundle
savedInstanceState) { //
TODO Auto-generated method stub super .onCreate(savedInstanceState); setContentView(R.layout.another); update
= (Button) findViewById(R.id.updateMain); update.setOnClickListener( new OnClickListener()
{ @Override public void onClick(View
v) { //
TODO Auto-generated method stub Intent
intent = new Intent(action); intent.putExtra( "data" , "yes
i am data" ); sendBroadcast(intent); finish(); } }); } } |