Michael R. Bloomberg, the billionaire former mayor of New York City who is considering a presidential run, appeared in Annapolis on Tuesday to chide Washington over the government shutdown and talk about the importance of compromise.

He gave the keynote speech Tuesday at a leadership conference at the U.S. Naval Academy in Annapolis, praising the late senator John McCain — an academy alumnus — for his independence and willingness to work across the aisle.

Bloomberg, once a Republican and then a political independent, registered last year as a Democrat, stoking speculation about whether he will run for president.

Following meetings with Maryland lawmakers and state Attorney General Brian E. Frosh (D), Bloomberg spoke at a news conference about the importance of “balancing” different priorities. He called the partial federal government shutdown, now in its fifth week, a “disgrace.”

“You can’t be an absolutist . . . life is much more complex than that,” he said. “I think everybody should come together. If you have one issue that you really care about, you don’t shut everything down if you can’t get your way.”

Asked about the timing of an announcement about 2020, Bloomberg said: “Well, I said I’d take a look at it in January, February. We’re still in January.”

The three-term mayor of New York has emerged as a vocal critic of President Trump. He donated millions to Democrats in November’s midterm elections, and has spent millions more, dating back years, on gun control initiatives,

On Tuesday, he came to Annapolis with John Feinblatt, president of Everytown for Gun Safety, an organization that advocates for gun control legislation. Bloomberg praised Maryland’s gun laws, including a “red flag” law approved by legislators last year that allows a relative, spouse, legal guardian or roommate to seek a court order to keep a person who is considered dangerous from possessing a gun.

The state — which last year saw a mass shooting at the Capital Gazette newsroom in Annapolis and a campus shooting at Great Mills High School in Southern Maryland — also banned the manufacture, use and sale of bump stocks, the devices used to accelerate the firing of semiautomatic weapons.

Bloomberg, who as mayor backed controversial stop-and-frisk tactics by police, said he supported the proposed creation of an armed private police force at Baltimore’s Johns Hopkins University, his alma mater.

Hopkins would be the only private university in the state to have its own police force, and the plan has drawn criticism from some lawmakers and students. But Bloomberg called it “kind of ridiculous” not to have an armed police force on the campus, given Baltimore’s high homicide rates.

During a sometimes pointed question-and-answer session at the Naval Academy, where 5,000 midshipmen and guests gathered to hear him speak, Bloomberg defended his record on stop-and-frisk, saying New York City police “certainly” did not stop people based on their race.

“We have to stop the carnage,” said Bloomberg, who would be more conservative than many in the Democratic field, which so far includes Sens. Kamala D. Harris (Calif.), Elizabeth Warren (Mass.) and Kirsten Gillibrand (N.Y.).

He sharply criticized the media — which he described as too focused on “clicks and eyeballs” — and had harsh words for fellow Democrats, whom he accused of hypocrisy for raising more money than Republicans even as some leaders spoke against big donors.

“It’s exactly the reverse of what you would believe if you listen to them crying poverty and saying, ‘Big money is wrong,’ ” Bloomberg said.