轉自:http://www.cnblogs.com/ycxyyzw/p/3875544.htmlhtml
第一種方法:java
遇到一個問題,須要在一個activity中控制另外一個acitivity作一些更新,沒想到傳遞handler的方法,經過如下方式解決。android
1.在MyAPP中定義屬性handlerapp
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;
}
}
|
二、在主activity 中給MyAPP的屬性handler賦值ide
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);
}
});
}
|
三、在另外一個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();
}
});
}
|
第二種方法:post
看到做者的這篇文章不錯,就轉了過來。兩個Activity之間能夠經過發送廣播來控制另一個activity的UI更新,好比團購APP裏:我下了個訂單以後,那麼我能夠經過發送廣播通知「個人訂單」裏面的內容內容自動更新。this
再重複一遍我遇到的問題,就是在MainActivity裏面打開AnotherActivity去執行一些操做,相應的改變MainActivity裏的一些佈局或者執行一些動做,最開始想到的就是把MainActivity的Handler直接傳給AnotherActivity,好像不可行,就有了這篇和上一篇文章。url
上一篇方案一是經過重寫application來在兩個activity之間共享Handler的,今天這個方案是經過廣播機制解決原本想要經過傳遞handler來實現的功能,算是Activity之間傳遞Handler問題的變通方案,spa
其實很簡單,就是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();
}
});
}
}
|