简体   繁体   中英

How to make Activity running on the home screen?

I'm creating application to show dialog activity each specific time (specified by the user). I used Timer to do this and everything is fine. However when I go back to home screen the dialog activity is not appearing there only keep showing on the main activity because once I open the application again I found number of dialog activities already opened by the timer. So how I can make that dialog activity keep showing any where in the device?

The Code:

                tt = new TimerTask() 
                {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Intent openD = new Intent(MainActivity.this,Dialog.class);
                                openD.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(openD);

                            }
                        });
                    }      
                };
                t = new Timer();
            t.schedule(tt,0,5000);

尝试查看后台服务

Because, when you go to home button your Activity goes into pause state and then destroys hence it doesn't, put your timer logic in a Service and call you activity from there, in this way when you go back to home screen you logic still remain in running state.

Do read lifecycle of Activity & Service in Android

http://developer.android.com/guide/components/services.html

Also as a note, if you want your activity to pop up all the time on certain events, make sure you make your Service persistable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM