简体   繁体   中英

How can I keep an android app running in background

I am making a simple scare your friend app. You have to press a button and then set a minute timer that will then bring up classic exorsist icon and scream on screen. I tried putting android:persistent="true" , but it didn't work... Here's my activity:

package com.odysseus.myapp;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    MediaPlayer scareMusic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button startTimer = (Button) findViewById(R.id.btimerStart);
        scareMusic = MediaPlayer.create(MainActivity.this, R.raw.monster_scream);

        startTimer.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Thread scareTimer = new Thread(){
                    public void run(){
                        try{
                            sleep(5000);
                            Intent activityIntent = new Intent("com.odysseus.myapp.SCARER");
                            startActivity(activityIntent);
                        }catch(InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                };
                scareTimer.start();
            }
        });

    }
}

I am really new to android so don't just say use a service or something because I don't know what that is. Other answers I found were too advanced for me so please explain as much as you can!

There's no way to truly make your app immune to shutdown. The attribute "android:persistent" gets ignored for all apps that are not System apps.

That being said, to make sure that the application fires the intent after the given time, you'll probably have to place the launching code in a serivce (if even possible then).

To use services is not really hard. Just create a new class and add extends Service . When you're done doing that you should add this method:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {      
        //Your code here
        return START_STICKY;    
    }

Inside this method you can launch your media player. To stop the service you just put stopSelf() in the onDestroy() . Good luck!

Instead of using Activity you can use a Service that always run in the background. See this answer for how to create a app that just has an activity. Android app with Service only . As an work around you can create an Activity no content view or transparent layout, then in this activity start the service and then quickly close the activity using finish() .

Now in the Service you can use the exact code that you are trying to use in an Activity. But remember to stop the Service after showing com.odysseus.myapp.SCARER .

Update :- In your com.odysseus.myapp.SCARER activity after showing the code you can use the following command to stop the Service.

stopService(new Intent(this, service.class));

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