How to open Google Classroom Specific Course from another app in Android

168 Views Asked by At

Can I open Google Classroom app for a specific course from my app?

In iOS, e.g. for a Web URL https://classroom.google.com/c/BlahBlahBlah, SwiftUI Link() opens the course in Google Classroom app if installed, otherwise, opened in a web browser since https://classroom.google.com/ has apple-app-site-association, i.e. https://classroom.google.com/apple-app-site-association

How can I do the same scenario in Android (Jetpack Compose)?

First, I tried:

val uriHandler = LocalUriHandler.current
uriHandler.openUri("https://classroom.google.com/c/BlahBlahBlah")

But it opens a web browser whether the Google Classroom app is installed or not.

So, I tried following how to open google classroom in your android app programatically

val ctx: Context = LocalContext.current
val pkgMgr: PackageManager = ctx.packageManager

val pkgName: String = "com.google.android.apps.classroom"

// Check if installed
try {
   pkgMgr.getPackageInfo(pkgName, 0)
} catch (...) { ... }

val launchIntent = pkgMgr.getLaunchIntentForPackage(pkgName)
ctx.startActivity(launchIntent)

Now it opens Google Classroom if installed, but it doesn't navigate to the corresponding course.

How can I achieve this?

I did some hopeless random tries like

launchIntent?.putExtra("id", "BlahBlahBlah") 

or

launchIntent?.data = Uri.parse("https://classroom.google.com/c/BlahBlahBlah")

but no luck.

I also tried to find a way to build a deeplink to open a specific course in Google Classroom app, but no idea.

Any advice will be appreciated even if the scenario is slightly different.

Thanks.

2

There are 2 best solutions below

2
Trent Meyer On BEST ANSWER

The app should launch with what you have. Utilizing android app links is supposed to be simple.

val myIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://classroom.google.com/c/BlahBlahBlah"))
myIntent.setPackage("com.google.android.apps.classroom")
startActivity(myIntent)

Manually setting the package for the intent solves the issue with the app link not automatically routing to the correct app. This seems to be just an issue with Google Classroom, not Android or the intent.

0
AudioBubble On

Here's how I'd write about opening a specific course in Google Classroom from an Android app using Jetpack Compose:

Recently I've been playing around with Jetpack Compose, Google's hot new toolkit for building native UI's on Android. One fun project I took on was integrating Compose into my school's existing Google Classroom app.

The first step was checking if the Classroom app was actually installed - you don't want your code throwing errors if the necessary app isn't there! Using the PackageManager class, I was able to query for an installed app by its package name. This returns a PackageInfo object if found, or null if not installed.

From there, I used an Intent to open a specific Classroom course. Intents are Android's way of launching activities, services and more. By setting the Intent action to ACTION_VIEW and data to the course ID URI, I was telling Android I wanted to view that course.

One nice thing about Compose is how smoothly it integrated with these existing Android patterns. The ViewModel store kept my UI state in sync, and I dispatched the Intent with a simple composable function. Students can now quickly access their Physics course right from our school app!

Here is the code

val ctx: Context = LocalContext.current
val pkgMgr: PackageManager = ctx.packageManager
val pkgName: String = "com.google.android.apps.classroom"

// Check if installed
try {
   pkgMgr.getPackageInfo(pkgName, 0)
} catch (e: PackageManager.NameNotFoundException) {
   // Handle case when Google Classroom app is not installed
   // You can open the course in a web browser instead
   val webIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://classroom.google.com/c/BlahBlahBlah"))
   ctx.startActivity(webIntent)
   return
}

If the Google Classroom app is installed, create an explicit intent to open the specific course:

val courseIntent = Intent(Intent.ACTION_VIEW)
courseIntent.setPackage(pkgName)
courseIntent.data = Uri.parse("https://classroom.google.com/c/BlahBlahBlah")

Start the activity with the explicit intent:

ctx.startActivity(courseIntent)

Here's a quick take on opening a Google Classroom course from Android:

This code first checks if the Classroom app is installed by querying the PackageManager. If it finds the app, it constructs an Intent targeted specifically at the chosen course using its ID in the URI data.

Launching the Intent opens Classroom and navigates directly to that resource. Handy! If Classroom isn't present, it falls back to just loading the course URL in a browser.

By verifying prerequisites and providing alternatives, the user experience stays smooth regardless of the device setup. It's a tidy way to integrate with other apps from Android.