从服务发送一条简单消息到 Activity
Sending a simple message from Service to Activity
我正在尝试找到一种方法将整数值从我的服务发送到我的 MainActivity,但我无法通过 Internet 上的教程弄明白。我正在使用处理程序接收我从服务中的线程发送的消息。
我在服务中的线程代码:
private class ServiceThread extends Thread {
@Override
public void run() {
super.run();
try{
Log.i("Service", "TASK PERFORMED");
int x = 0;
for (int i = 0; i < 20; i++){
x += (random.nextInt(20) + 20);
}
theInteger = x/20;
Message msg = Message.obtain();
msg.what = MY_RND_INT;
msg.arg1 = theInteger;
handler.sendMessage(msg);
}catch(Exception e){
e.getMessage();
}
}
}
我尝试接收消息的代码:
public Messenger mMessenger = new Messenger(new IncomingHandler());
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SimpleService.MY_RND_INT:
rndInt.setText(msg.arg1);
break;
default:
super.handleMessage(msg);
}
}
}
操作方法如下:
服务端
// Supposing that your value is an integer declared somewhere as: int myInteger;
private void sendMessage() {
// The string "my-message" will be used to filer the intent
Intent intent = new Intent("my-message");
// Adding some data
intent.putExtra("my-integer", myInteger);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
Activity边
@Override
public void onResume() {
super.onResume();
// This registers messageReceiver to receive messages.
LocalBroadcastManager.getInstance(this)
.registerReceiver(messageReceiver, new IntentFilter("my-message"));
}
// Handling the received Intents for the "my-integer" event
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
int yourInteger = intent.getIntExtra("my-integer", -1); // -1 is going to be used as the default value
}
};
@Override
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(messageReceiver);
super.onPause();
}
我正在尝试找到一种方法将整数值从我的服务发送到我的 MainActivity,但我无法通过 Internet 上的教程弄明白。我正在使用处理程序接收我从服务中的线程发送的消息。
我在服务中的线程代码:
private class ServiceThread extends Thread {
@Override
public void run() {
super.run();
try{
Log.i("Service", "TASK PERFORMED");
int x = 0;
for (int i = 0; i < 20; i++){
x += (random.nextInt(20) + 20);
}
theInteger = x/20;
Message msg = Message.obtain();
msg.what = MY_RND_INT;
msg.arg1 = theInteger;
handler.sendMessage(msg);
}catch(Exception e){
e.getMessage();
}
}
}
我尝试接收消息的代码:
public Messenger mMessenger = new Messenger(new IncomingHandler());
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SimpleService.MY_RND_INT:
rndInt.setText(msg.arg1);
break;
default:
super.handleMessage(msg);
}
}
}
操作方法如下:
服务端
// Supposing that your value is an integer declared somewhere as: int myInteger;
private void sendMessage() {
// The string "my-message" will be used to filer the intent
Intent intent = new Intent("my-message");
// Adding some data
intent.putExtra("my-integer", myInteger);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
Activity边
@Override
public void onResume() {
super.onResume();
// This registers messageReceiver to receive messages.
LocalBroadcastManager.getInstance(this)
.registerReceiver(messageReceiver, new IntentFilter("my-message"));
}
// Handling the received Intents for the "my-integer" event
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
int yourInteger = intent.getIntExtra("my-integer", -1); // -1 is going to be used as the default value
}
};
@Override
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(messageReceiver);
super.onPause();
}