gpt4 book ai didi

android - 将 Activity 放在首位

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:26 24 4
gpt4 key购买 nike

即使 Activity 已暂停,我也想在 Timer 勾选时将 Activity 置于最前面。

非常感谢..

我的代码如下:

package cem.examples.wsAct;

import something....

public class main extends Activity {

TextView tvResult, tvCount;
Button btn;
Timer timer;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// setviews ....
// (find on the layout and bind them to the fields)

timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {
runOnUiThread(new Runnable() {

@Override
public void run() {
// bring activity to front
f_UpdateUI();
}
});
}

}, 1000, 3000);
}

void f_UpdateUI() {
String result = f_RetrieveFromWebService();

// ??? Code... ???
// If the activity is sended to back (how can I get it's state?)
// Bring the activity even if it is paused or stopped (here is the lost part)
}

private String f_RetrieveFromWebService() {
// connect to web service and return string
return "ta ta taaaaa";
}

}

最佳答案

你不能用AlarmManager吗? ?据我所知,即使您的应用程序未运行(在 onPause 之后,可能还有 onDestroy 之后),您也需要它运行。

如果您不需要它,那么您可以尝试使用 startActivity 生成相同的 Activity 并使用一些可用的标志。我不清楚你真正需要什么,什么时候需要,所以请尝试查看以下标志:FLAG_ACTIVITY_REORDER_TO_FRONTFLAG_ACTIVITY_SINGLE_TOPFLAG_ACTIVITY_CLEAR_TOP.. . 你读了吗all the flags ?我确信其中一个(或一组)会做你想做的事。

关于android - 将 Activity 放在首位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6405531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com