开发者

Bring activity to front itself

I want to bring the activty front when Timer ticked even if activity is paused.

Thanks a lot..

My code is below:

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";
}

}


Can't you use AlarmManager? For what I see, you need it to be running even if your app is not (after onPause, and possibly onDestroy).

If you don't need it, then you could try spawning the same activity with startActivity and using some of the available flags. It's not clear to me what you really need, when you need, so try looking at the following flags: FLAG_ACTIVITY_REORDER_TO_FRONT, FLAG_ACTIVITY_SINGLE_TOP, FLAG_ACTIVITY_CLEAR_TOP... Did you read all the flags? I'm sure one of them (or a set) will do what you want.


To bring your Activity to the foreground, you need to do the following:

Intent intent = new Intent(context, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

MyRootActivity must be the root Activity of your app (the one with ACTION=MAIN and CATEGORY=DEFAULT).

The important thing is that you must do this from a non-Actvity Context. The means you need to do it from either a BroadcastReceiver, or from a Service.

See also this answer and the interesting comment thread

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜