为什么 runOnUiThread() 在服务内部不起作用?
Why does runOnUiThread() not work inside Service?
我正在创建一个需要每分钟更新值的应用程序,即使该应用程序不是 运行。
当然,我已经设置了一个简单的 Service
来做到这一点。我设置了调试消息来告诉我 Service
何时开始、何时更新(每分钟)以及何时关闭。当 runOnUiThread()
方法中的值更新时,我也会收到一条消息。除了 runOnUiThread()
中的一条消息,我的所有消息都已激活。我做错了什么(当然有)?我需要更改什么?
代码:
@Override
public void handleMessage(Message message) {
try {
if (!serviceStarted) {
serviceStarted = true;
serviceTest = true;
while (serviceStarted) {
new MainActivity().runOnUiThread(new Runnable() {
public void run() {
OverviewFragment.refresh(getApplicationContext());
System.out.println("yay");
}
});
Thread.sleep(((1 /* minutes */) * 60 * 1000));
System.out.println("Updated values through service.");
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
stopSelf(message.arg1);
}
您不能通过调用 new 创建 Activity。它不能以这种方式正确初始化。
此外,服务默认在 UI 线程上 运行。所以没有必要这样做,除非你在其中创建一个线程。如果您是 - 运行OnUIThread 只是用于将 运行nable 发布到处理程序的语法糖。所以你可以这样做。
尝试使用处理程序或 LocalBroadcastManager 向 activity 发送消息。
看到这个问题:Accessing UI thread handler from a service
您可以在 Handler
到 post 中使用 Looper.getMainLooper()
一个 Runnable
来执行您要执行的任何操作。
一个不错的选择,就像 jinghong 提到的那样,是使用广播——换句话说,使用不同的模式。
So there's no need to do that, unless you're creating a Thread inside
of it
Gabe Sechan 的回答是正确的。
但是如果您使用的是单独的线程,则不要使用以下代码:
new MainActivity().runOnUiThread(new Runnable() {
public void run() {
OverviewFragment.refresh(getApplicationContext());
System.out.println("yay");
}
});
试试,这个代码:
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
OverviewFragment.refresh(getApplicationContext());
System.out.println("yay");
}
});
Caution: A service runs in the main thread of its hosting process—the
service does not create its own thread and does not run in a separate
process (unless you specify otherwise).
我正在创建一个需要每分钟更新值的应用程序,即使该应用程序不是 运行。
当然,我已经设置了一个简单的 Service
来做到这一点。我设置了调试消息来告诉我 Service
何时开始、何时更新(每分钟)以及何时关闭。当 runOnUiThread()
方法中的值更新时,我也会收到一条消息。除了 runOnUiThread()
中的一条消息,我的所有消息都已激活。我做错了什么(当然有)?我需要更改什么?
代码:
@Override
public void handleMessage(Message message) {
try {
if (!serviceStarted) {
serviceStarted = true;
serviceTest = true;
while (serviceStarted) {
new MainActivity().runOnUiThread(new Runnable() {
public void run() {
OverviewFragment.refresh(getApplicationContext());
System.out.println("yay");
}
});
Thread.sleep(((1 /* minutes */) * 60 * 1000));
System.out.println("Updated values through service.");
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
stopSelf(message.arg1);
}
您不能通过调用 new 创建 Activity。它不能以这种方式正确初始化。
此外,服务默认在 UI 线程上 运行。所以没有必要这样做,除非你在其中创建一个线程。如果您是 - 运行OnUIThread 只是用于将 运行nable 发布到处理程序的语法糖。所以你可以这样做。
尝试使用处理程序或 LocalBroadcastManager 向 activity 发送消息。
看到这个问题:Accessing UI thread handler from a service
您可以在 Handler
到 post 中使用 Looper.getMainLooper()
一个 Runnable
来执行您要执行的任何操作。
一个不错的选择,就像 jinghong 提到的那样,是使用广播——换句话说,使用不同的模式。
So there's no need to do that, unless you're creating a Thread inside of it
Gabe Sechan 的回答是正确的。
但是如果您使用的是单独的线程,则不要使用以下代码:
new MainActivity().runOnUiThread(new Runnable() {
public void run() {
OverviewFragment.refresh(getApplicationContext());
System.out.println("yay");
}
});
试试,这个代码:
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
OverviewFragment.refresh(getApplicationContext());
System.out.println("yay");
}
});
Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise).