서비스 클래스
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(getApplicationContext(), "MSG onCreate SERVICE", Toast.LENGTH_LONG).show();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "MSG onStartCommand SERVICE", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Toast.makeText(getApplicationContext(), "MSG STOP SERVICE", Toast.LENGTH_LONG).show();
super.onDestroy();
}
}
에 대한 간단한 코드이며, 여기에이 서비스
startService(new Intent(this, MyService.class));
new Timer().schedule(new TimerTask() {
@Override
public void run() {
startService(new Intent(getApplicationContext(), MyService.class));
}
}, 5000);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
stopService(new Intent(getApplicationContext(), MyService.class));
}
}, 10000);
이 잘 작동하는지 테스트하기위한 코드입니다보십시오. 또한이 코드를 매니페스트에 추가하십시오.
<service android:name=".MyService" />