λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ‘¨πŸΌ‍πŸ’»κ°œλ°œ/μ•ˆλ“œλ‘œμ΄λ“œ μŠ€νŠœλ””μ˜€

μ•ˆλ“œλ‘œμ΄λ“œ μŠ€νŠœλ””μ˜€ - λ°±κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€ μ‹€ν–‰

by Janger 2021. 12. 19.
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
λ°˜μ‘ν˜•