Shakespeare's reason for including all the military activity in his play is complicated. It has to to with King Hamlet's ghost. Shakespeare wanted to have the Ghost come to Elsinore to tell Hamlet that he was murdered by his brother and Hamlet's uncle, Claudius, and to get young Hamlet to pledge himself to take revenge. Shakespeare wanted to establish that the actor playing the Ghost was indeed a ghost and that he looked like the dead King Hamlet; and the playwright wanted to do this before the Ghost and Hamlet got together in Scenes 4 and 5 of Act 1.
But how could he make the audience understand that the actor playing the Ghost was a ghost and not just another character in the play? He did not want the Ghost to be wearing a shroud, and he did not have a way of making the actor look luminiscent. Even if he could make the actor luminescent, it would not be effective with the play being presented in daylight. So Shakespeare decided to make the actor look "different" by having him wear armor with a helmet. He would have instructed the actor to walk in a "ghostlike" manner, which would probably mean stalking slowly, looking straight ahead, and ignoring the three men observing him--Horatio, Marcellus, and Bernardo. These actors would tell each other, and at the same time tell the audience, that this was obviously a ghost and that he looked exactly like the dead King Hamlet. The Ghost would probably move silently because he would be wearing felt slippers, whereas when the other actors walked they would make the usual clumping on the boards.
But Shakespeare did not want anyone in his audience to suspect that the Ghost was there to talk to his son. He wanted the Ghost's message to come as a great surprise in Scene 5, when the Ghost and Hamlet were alone. So the playwright invented spurious reasons for the Ghost's presence in order to mislead the audience. Mainly, Shakespeare wanted to make the audience think that the Ghost was concerned about a possible war with Norway, which would explain why the actor was wearing armor. But Shakespeare invented a number of other reasons why the Ghost might have appeared at this time. These reasons are contained in the questions Horatio asks after he is urged to speak to the Ghost.
If thou hast any sound, or use of voice,
Speak to me;
If there be any good thing to be done,
That may to thee do ease and grace to me,
Speak to me;
If thou art privy to thy country's fate,
Which, happily, foreknowing may avoid,
Or if thou hast uphoarded in thy life
Extorted treasure in the womb of earth,
For which, they say, you spirits oft walk in death,
Speak of it! stay, and speak!
Horatio offers every conceivable reason for the Ghost's visit except the real one. Shakespeare wants to establish that Horatio, Marcellus, and Bernardo do not suspect the real reason for the Ghost's visit either.
Shakespeare had to invent a military problem as part of his way of misleading the audience. It turns out to be a false alarm. Claudius complains to the Norwegian king about Fortinbras' threatening movements, the Norwegian king orders Fortinbras to cease and desist, and the whole problem is revolved by Act 2, Scene 2. Voltimand, one of the ambassadors to Norway, tells Claudius:
Most fair return of greetings and desires.
Upon our first, he sent out to suppress
His nephew's levies; which to him appear'd
To be a preparation 'gainst the Polack,
But better look'd into, he truly found
It was against your Highness; whereat griev'd,
That so his sickness, age, and impotence
Was falsely borne in hand, sends out arrests
On Fortinbras; which he, in brief, obeys,
Receives rebuke from Norway, and, in fine,
Makes vow before his uncle never more
To give the assay of arms against your Majesty.
Whereon old Norway, overcome with joy,
Gives him three thousand crowns in annual fee
And his commission to employ those soldiers,
So levied as before, against the Polack;
With an entreaty, herein further shown,
That it might please you to give quiet pass
Through your dominions for this enterprise,
On such regards of safety and allowance
As therein are set down.
So the whole war scare was a false alarm, a red herring, a tempest in a teapot. Obviously the military threat was invented to mislead the audience about the Ghost's presence. It also explains why the Ghost is wearing armor, but the armor is intended to make the actor recognizable as the ghost of Hamlet's father whenever he appears. Shakespeare is successful in diverting and misleading the audience. The Ghost's message comes as a total surprise and shock when Hamlet hears it in Scene 5 of Act 1.
Since Shakespeare has introduced Fortinbras and his army, albeit it entirely offstage, he decided to keep their presence in Denmark as a sort of subplot. In the end Fortinbras talks about claiming the Danish throne, and the dying Hamlet nominates him for that title. All this may be intended to show that the big war scare in Act 1 had some little substance to it.