WASHINGTON — In a 5-4 decision, the U.S. Supreme Court gave a victory to privacy advocates on Friday, ruling that police generally must have permission from a judge before they can get cellphone records to plot the movements of individual customers.

The decision requires police departments nationwide to get a search warrant in order to obtain telephone company data to track where a user has been. The technique is widespread, given that 95 percent of Americans own a cellphone.

Chief Justice John Roberts, who wrote the court's opinion, said "a phone goes wherever its owner goes, conveying to the wireless carrier not just dialed digits, but a detailed and comprehensive record of the person's movements."

Roberts was joined by the court's four liberals, Justices Ruth Bader Ginsburg, Stephen Breyer, Sonia Sotomayor and Elena Kagan. Justice Anthony Kennedy and the court's three conservatives dissented.

When used for calls or texts, a cellphone signals a nearby antenna tower to connect with the telephone network. As the user travels, the call is handed off to successive towers, and the cellphone companies keep records of the phone numbers routed through each tower to sort out such things as roaming charges.

By mapping which towers were used by a given phone number, police can reconstruct a person's whereabouts over days, weeks or months.

Friday's ruling said police can still get cellphone records without a warrant in such emergencies as "the need to pursue a fleeing suspect, protect individuals who are threatened with imminent harm, or prevent the imminent destruction of evidence."

The Supreme Court's ruling came in a case brought by a Michigan man, Timothy Carpenter, who was convicted of robbing a string of Radio Shack and T-Mobile stores after FBI agents used three months of cellphone records to show that he was near each store at the time of the crimes. He argued that because the FBI did not get a search warrant, that evidence, along with his conviction, should be thrown out.

Carpenter lost in the lower courts, which ruled that no search warrant was needed because phone customers have no expectation that their records will be private. Those decisions relied on a Supreme Court case from 38 years ago. It said phone customers don't expect that the numbers they dial will remain private, because the phone company uses that information for billing.

But Carpenter's lawyers argued that the reasoning of that case, issued when telephones were hard-wired into the wall and didn't move, should not apply in the digital age, when customers expect their calling patterns to remain private. Getting cellphone tower location data, they said, allows the police to discover far more than which numbers were dialed.

Noting how prevalent smartphones now are, Roberts opened his majority opinion with this line: "There are 396 million cellphone service accounts in the United States — for a nation of 326 million people."

The Justice Department had urged the court to uphold Carpenter's conviction and rule that no search warrant was required. "Cellphone users voluntarily reveal to their providers information about their proximity to cell towers so the providers can connect to their calls," the Trump administration's legal brief said. "Users cannot reasonably expect that the providers will not reveal that business information to the government."

In recent years, the justices have shown a willingness to extend digital-age privacy protections. The Supreme Court has ruled that police need warrants to search through the contents of smartphones or to attach a GPS tracking device to a car.