How to run activity in background in Android

2019-02-08 12:44发布

In my application, I need to send the text message to the user after a certain interval. For that I need to run the message sending code in background. I am using the alarm manager to start activity at specific time.What to do? Is there another way to do this?

5条回答
Viruses.
2楼-- · 2019-02-08 13:19

For those who want to do it with an Activity, and not by using a service (there can be situations when this is necessary), I did it by starting the Activity the usual way, and then in the Activity's onCreate I sent it to the background by calling moveTaskToBack(true) .

See Sending a running application to background programmatically

If you use this method, you should add an extra info to the intent by which you start the Activity, so that it knows in onCreate that it must send itself to the background. You do this by calling the Intent's putExtra method. In the Activity's onCreate, you get the calling Intent with getIntent, then retrieve the info you put there with getStringExtra, for instance (I used a string extra info). See the documentation for the Intent class.

Update august 2018: Note, however, that on some devices this can make your Activity blink (that is, briefly appear) on the screen before it's sent to the background. It appears that the system treats the notification to send the task to the background way too late. If you call the method in onCreate, it should have plenty of time to know about it before it draws the Activity window. IMO this is a system flaw, it shouldn't happen like this but it does.
So, anyway, if this is a concern and if you can, you'd probably better do it with a service.

查看更多
我命由我不由天
3楼-- · 2019-02-08 13:27

It is recommended to use Services to achieve that goal, but I've encountered similar situation when in need for something from another app - and the intent for the service wasn't exported, so the only way to start the service is to start the activity - and to keep the user in my app I need to keep my activity in the foreground. In that case it's possible to start the other activity in an AsyncTask, and then in onPostExecute restart myself.

    (new AsyncTask<Void, Void, Void>() {
        boolean starting = false;
        @Override
        protected Void doInBackground(Void... params) {
            if(!externalServiceRunning()){
                Intent externalIntent = getPackageManager().getLaunchIntentForPackage("com.external...");
                if (externalIntent != null) {
                    starting = true;
                    startActivity(externalIntent);
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if(starting) {
                Intent myApp = getPackageManager().getLaunchIntentForPackage("com.my.myapp");
                if (myApp != null) {
                    startActivity(myApp);
                }
            }
        }
    }).execute();
查看更多
别忘想泡老子
4楼-- · 2019-02-08 13:30

This might help.. Add this to android manifest..:

uses-permission android:name="android.permission.ACCESS_BACKGROUND_SERVICE"

Also use AsyncClass for your long/background processes..Like this.:

http://developer.android.com/reference/android/os/AsyncTask.html

查看更多
疯言疯语
5楼-- · 2019-02-08 13:38

yes.you should be using Service to achieve this.They can run in background till explicitly closed. IntentService is another alternative

http://developer.android.com/guide/topics/fundamentals/services.html

查看更多
做个烂人
6楼-- · 2019-02-08 13:40

You could look into using Services which are basicly UI-less Activities.

查看更多
登录 后发表回答