将活动带到自己前面
问题描述:
即使活动暂停,我也想在计时器打勾时将活动前沿带到活动前面。将活动带到自己前面
非常感谢..
我的代码如下:
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";
}
}
答
就能将您的活动的前景,你需要做到以下几点:
Intent intent = new Intent(context, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
MyRootActivity
必须是你的应用程序(具有ACTION = MAIN和类别=默认值)的根系活力。
重要的是,你必须从非Actvity上下文做到这一点。这意味着你需要从BroadcastReceiver
或Service
这样做。
答
你不能使用AlarmManager
?对于我所看到的,即使您的应用程序不是(在onPause之后,可能还有onDestroy之后),也需要它运行。
如果你不需要它,那么你可以尝试产生与startActivity
相同的活动,并使用一些可用的标志。我不清楚你真的需要什么,当你需要的时候,试着看看下面的标志:FLAG_ACTIVITY_REORDER_TO_FRONT
,FLAG_ACTIVITY_SINGLE_TOP
,FLAG_ACTIVITY_CLEAR_TOP
......你看过all the flags吗?我相信其中一个(或一组)会做你想做的。
见我不认为这是可能的,并且有很好的理由,因为这将是相当恼人的一些活动,以不断弹出不是预期的时候。 – Noel
@Noel它可能很烦人,但它也有可能:-) –