์ถ์ฒ:
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์ ๋ณํ๊ฐ ์๊ธฐ๋ ๊ธฐ๋ฅ์ ์ค๋ฅ๊ฐ ์๊ธด๋ค.
์ถ์ฒ:
์์ผ (Socket) - 3. ์์ผ ์ฌ์ฉํ๊ธฐ (2)
1. ์คํ ๊ฒฐ๊ณผ ํ๋ฉด 2. ๊ตฌํ - SocketServer ์์คํ ๋ฆฌ์์ค๊ฐ ์ ์ผ๋ฉด ์์คํ ์ด ์กํฐ๋นํฐ๋ฅผ ๊ฐ์ ๋ก ์ข ๋ฃ์ํฌ ์ ์๋ค. ์ด๋ ๊ฒ ๋๋ฉด ์๋ฒ๊ฐ ์ข ๋ฃ๊ฐ ๋๊ธฐ ๋๋ฌธ์ ์๋ฒ๋ฅผ ์กํฐ๋นํฐ๊ฐ ์๋ ์๋น์ค๋ก
qlyh8.tistory.com