President Obama relayed a message of hope and perseverance while delivering remarks at the memorial service for the five Dallas police officers killed by a sniper July 7. (Video: Reuters)

DALLAS — Visiting yet another city heartbroken by a mass shooting Tuesday, President Obama tried to defuse tensions that have erupted in the past week — first when black men in Louisiana and Minnesota were fatally shot by police, then when a gunman who said he was angry about such police killings opened fire on officers in Dallas.

At a memorial service for the five officers killed here, Obama sought to unify a nation left divided and raw by fatal shootings involving police, for open hearts and understanding from both law enforcement and those protesting against them.

Obama praised the slain officers and sharply criticized any who would paint all police as bigoted or seek violence against them. Yet he also acknowledged the very real fear and pain among black Americans who feel targeted and brutalized by police.

“We ask police to do too much and we ask too little of ourselves,” Obama said during his 40-minute remarks, which capped an emotional service just a mile from where the five officers were killed last week.

Nation remains on edge after police shootings, officer deaths

Even as he spoke of unity, there were still tangible signs of a rift between protesters and police. Obama was interrupted by applause when he spoke in a concert hall filled with law enforcement officials about those officers killed last Thursday, but the families of those officers did not clap when he spoke about the “Black Lives Matter” protests or invoked the killings in Minnesota and Louisiana.

“Your work, the work of police officers across the country, is like no other,” Obama said to the assembled officers. “From the moment you put on that uniform, you have answered a call that at any moment, even in the briefest interaction, may put your life in harm’s way.”

Consoling a nation after a violent episode has become familiar ground for Obama, whose presidency has been marked by tragedies and attacks in places such as San Bernardino; Roseburg, Ore.; Charleston; and, most recently, Orlando.

“I’m not naive,” Obama said. “I’ve spoken at too many memorials during the course of this presidency. I’ve hugged too many families that lost a loved one to senseless violence.”

Before Obama spoke, he was introduced by Dallas Police Chief David Brown, who quoted the Stevie Wonder song “As” in remarks aimed at the relatives of the fallen officers and said: “There’s no greater love than this that these five men gave their lives for all of us.” (When Obama spoke after Brown, he began his address by saying, “I’m so glad I met Michelle first, because she loves Stevie Wonder,” prompting laughter inside the hall.)

Dallas police chief says ‘we’re asking cops to do too much in this country’

Former president George W. Bush, a former Texas governor who moved to Dallas after leaving the White House, made a rare public appearance at the memorial to speak to those who lost loved ones.

“Those of us who love Dallas and call it home have had five deaths in the family,” Bush said. “All of us feel a sense of loss, but not equally. Your loss is unfair. We cannot explain it. We can stand beside you and share your grief.”

Bush at Dallas police memorial: 'We are brothers and sisters' (Video: Reuters)

As Obama would minutes later, Bush evoked the country’s bitter divisions, saying that the nation should not be united by grief or fear. “Too often we judge other groups by their worst examples while judging ourselves by our best intentions, and this has strained our bonds of understanding and common purpose,” Bush said.

Dallas Sgt. Robert Munoz was in the same rookie class as Michael Smith, one of the officers killed last week, and said Smith “was so dedicated to this job and this community.”

Munoz called the shooting “a wake up call” for better relations between police officers and the communities they serve.

“This has been an eye opener for many of us,” Munoz said. “We all have do better. These officers will not have died in vain. Not here in Dallas. Not in this community.”

Obama tells police: ‘I’m your best hope’ for relations with communities of color

Still, Obama’s attempts to meld together the mournful remembrances of a memorial service with calls for understanding those demonstrating around the country did not go over well with everyone in the Morton H. Meyerson Symphony Center.

“The tragedy is very fresh in our minds — too fresh for some people, I feel,” said one officer, seated near the stage, who declined to give his name. “They clapped when we were praised but when it came to race relations it was more of a stony silence where I was sitting.”

Five seats in the service were left open in memory of the officers killed in Dallas last week, reserved for those who “died for that cause” of protecting others, Mayor Mike Rawlings said during his remarks.

Dallas Police Chief David Brown recited the lyrics to a Stevie Wonder song in a moving speech at a memorial service honoring the Dallas police officers killed in an attack on July 7. (Video: Reuters)

Obama spoke not only about the shooting in Dallas — calling it “an act not just of demented violence but of racial hatred” — but he also devoted time to discussing the protests and unrest playing out in a moment of intense tension nationwide.

During the flight to Dallas, Obama called family members of Alton Sterling, the man fatally shot by police in Baton Rouge last week, and Philando Castile, the man slain in Minnesota a day later. He used the calls to offer condolences to them on behalf of the American people, the White House said Tuesday.

The country remains on edge after a three-day stretch last week in which these fatal shootings by and of police gripped the nation, inflaming the debate over race and policing — and this tension extended into the service, as some officers shaken by last week’s shooting in Dallas said they were keeping an eye out for possible escape routes.

“All of it has left us wounded and angry and hurt,” Obama said. “It’s as if the deepest fault lines of our democracy have suddenly been exposed, perhaps even widened.”

Obama continued: “We wonder if an African American community that feels unfairly targeted by police and police departments that feel unfairly maligned for doing their jobs, can ever understand each other’s experience.”

But while Obama said he knew words could be inadequate at prompting lasting change, noting that he has “spoken at too many memorials during the course of this presidency,” he also tried to call for better understanding on both sides.

Speaking directly to police and protesters alike, Obama urged law enforcement to see “that insisting we do better to root out racial bias is not an attack on cops, but an effort to live up to our highest ideals.” And he also urged those demonstrating to “guard against reckless language going forward.”

Ultimately, Obama said that “bias remains” in America, and that “when African Americans from all walks of life  … voice a growing despair over what they perceive to be unequal treatment,” and give their children “the talk” about how to respond when stopped by a police officer, this is not something that should be “dismissed by those in authority.”

He echoed what the Dallas police chief had said a day earlier about officers being asked to take on too much, saying that too great a burden is placed on police departments, which he said is the cause of much of the tension between communities and their police officers.

Obama said that things such as under-investment in schools and continued poverty ultimately turn police officers into social workers, teachers, parents and drug counselors, which he said leads to inevitable eruptions.

“We tell them to keep those neighborhoods in check at all costs, and do so without causing any political blowback or inconvenience,” he said. “Don’t make a mistake that might disturb our own peace of mind. And then we feign surprise when, periodically, the tensions boil over.”

The victims of the Dallas protest shooting

Obama is seeking this week to reopen dialogue with police agencies and other groups on ways to rebuild trust among African American and other communities. On Monday, Obama met for nearly two hours with leaders of eight law enforcement groups, informing them that he considered the killing of the five police officers in Dallas “a hate crime” and that he would work actively to serve as an intermediary between minority activists and police.

“I’m your best hope,” Obama remarked at one point, according to the Fraternal Order of Police’s James O. Pasco, one of the meeting’s attendees.

Brittany Packnett, a Black Lives Matter activist and member of the president’s Task Force on 21st Century Policing, said in an interview that Obama could use his remarks as a way to address both the racial inequities that exist today and the concerns police officers have about their own safety.

“What we are talking about is, how do we create a society that replaces order with justice so that violence is not the place where people feel they have to turn,” she said. “So they’re experiencing peace and equity every day of their lives.”

A mournful gathering

Before the memorial service, hundreds of police officers, mostly wearing black, hugged and took photographs with each other as they waited to be allowed inside the building.

Officers from across Texas came out to support their fallen colleagues, dressed in full regalia as a mark of respect, their black uniforms accompanied by badges with black ribbons across them. These ribbons have the word “Dallas” written on them in large, white letters.

All attendees have also been given a blue and yellow satin sash they are wearing over over their shoulders and across their chests. The mood inside evoked a funeral that offers a chance at a reunion, as officers hug and greet one another.

“It reminds us that it could be any one of us, of our brothers and sisters in law enforcement who are out every day,” Lorenzo Garza, a deportation officer with the U.S. Immigration and Customs Enforcement, said Tuesday.

Detective demoted after calling Black Lives Matter protesters ‘terrorists’ on Facebook

Quentin Draper, a pastor at the Spirit and Truth Church in nearby Oak Cliff, said the president’s visit was “right on time.” Draper said the children of his parish had been markedly affected by the chaos of the last week.

“Many of our kids are quite afraid so we’re spending time talking to them about the importance of law enforcement and the importance of our relationship with law enforcement,” Draper said, standing alongside his young daughter.

Security at the symphony hall was tight, as it has been for most vigils in this city. Helicopters circled overhead as Secret Service agents patrolled the perimeter. Several police officers said they had mentally checked for escape routes, fearing the worst.

Officers armed with assault rifles walked the perimeter of the hall, while outside, a fire truck hung a large American flag.

In recent days, demonstrations have broken out from New York to San Francisco after deaths of black men in Minnesota and Louisiana, evoking the protests that erupted after deaths in Ferguson, Mo., and Baltimore in recent years.

Protesters in Atlanta staged a sit-in in front of the governor’s mansion late Monday. In Baton Rouge — one of the main flash points — the American Civil Liberties Union of Louisiana complained Monday of police using “violent, militarized tactics” that have included more than 200 arrests in recent days.

After a demonstration outside of the Georgia Governor's mansion, Atlanta's mayor agrees to meet with protesters. (Video: Reuters, Photo: AP, Photo: David Goldman/Reuters, Photo: AP)

“As we grapple with the aftermath of these events, the Department of Justice will continue to do everything in our power to build bonds of trust and cooperation between law enforcement and the communities we serve,” Attorney General Loretta E. Lynch said Tuesday during congressional testimony, according to her prepared remarks. “That work has never been more difficult – or more important.”

The interfaith memorial service honored the officers killed when a gunman, 25-year-old Micah Johnson, opened fire last Thursday during a peaceful rally downtown.

Before he was killed by a robot-carried bomb, police say Johnson told them he was angry about recent police shootings and wanted to kill white police officers. They also say he claimed to have placed explosives in Dallas, though none have been located.

Photos: The nation reacts to the killing of police officers in Dallas

Law enforcement officers salute the casket of Dallas Police Sr. Cpl. Lorne Ahrens during his funeral service at Prestonwood Baptist Church in Plano, Texas, Wednesday, July 13, 2016. Ahrens and four other officers were slain by a sniper during a protest last week in downtown Dallas. (AP Photo/LM Otero)

Wan and Berman reported from Washington. Louisa Loveluck in Dallas and Brian Murphy and Juliet Eilperin in Washington contributed to this report.

Further reading:

Search warrant: Baton Rouge officers saw butt of gun in Alton Sterling’s pocket, then saw him reach for it

Former police chief in Philadelphia, Washington: America is “sitting on a powder keg”

A Dallas police major’s account of the “hellish” attack

Two bailiffs killed, deputy sheriff injured in Michigan courthouse shooting

[This story has been updated and will be updated throughout the day.]