๐Ÿ‘จ๐Ÿผ‍๐Ÿ’ป๊ฐœ๋ฐœ/์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค

์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค - ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์„œ๋น„์Šค ์‹คํ–‰

Janger 2021. 12. 19. 07:50
728x90

์ถœ์ฒ˜: 

https://stackoverflow.com/questions/46716069/how-to-make-background-process-in-android-studio

 

How To Make Background Process in Android Studio

hi guys thank you for answering my question,i have made an android app in android studio i want to make funtion when i close the app the function start automatically in background is there any way ...

stackoverflow.com

 

[์•กํ‹ฐ๋น„ํ‹ฐ]

public class App extends Application {

    private static App instance;
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();

        App.context = getApplicationContext();

        startService(new Intent(this, YourBackgroundService.class));
    }
}

 

[ํด๋ž˜์Šค]

public class YourBackgroundService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);
    }
}

 

[AndroidManifest.xml]

<service android:name=".YourBackgroundService" />

 

 

 

 

์ฐธ๊ณ  2) 

public class YourBackgroundService extends Service {

 @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
 
    @Override
    public void onCreate() {
        super.onCreate();
        ServerThread thread = new ServerThread();
        thread.start();
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    
    
    class ServerThread extends Thread {
    ...
    }

    
}

 

๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ ๊ธฐ๋Šฅ๋“ค์€ onCreate ํด๋ž˜์Šค์— ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค. 

โ€ป ๋‹จ Toast ๋ฌธ์ž ๊ฐ™์€ View์— ๋ณ€ํ™”๊ฐ€ ์ƒ๊ธฐ๋Š” ๊ธฐ๋Šฅ์€ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธด๋‹ค. 

 

 

์ถœ์ฒ˜: 

https://qlyh8.tistory.com/88

 

์†Œ์ผ“ (Socket) - 3. ์†Œ์ผ“ ์‚ฌ์šฉํ•˜๊ธฐ (2)

1. ์‹คํ–‰ ๊ฒฐ๊ณผ ํ™”๋ฉด      2. ๊ตฌํ˜„ - SocketServer ์‹œ์Šคํ…œ ๋ฆฌ์†Œ์Šค๊ฐ€ ์ ์œผ๋ฉด ์‹œ์Šคํ…œ์ด ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ๊ฐ•์ œ๋กœ ์ข…๋ฃŒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์„œ๋ฒ„๊ฐ€ ์ข…๋ฃŒ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„๋ฅผ ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ์•„๋‹Œ ์„œ๋น„์Šค๋กœ

qlyh8.tistory.com

 

728x90