如何通过编程检测应用是否在Google Play上架?未上架时显示Toast或Activity?
Hey there! Great question—let’s break this down into two clear, actionable parts: checking if your app is still live on Google Play, and handling the fallback actions (Toast or Activity jump) if it’s not.
There are two reliable approaches here, depending on your setup and needs:
Option 1: Simple HTTP request (no authentication required)
The easiest way is to send a lightweight HEAD request to your app’s public Google Play store URL. If the response returns a 200 status, the app is live; a 404 means it’s likely been taken down. This works for any developer, no extra setup needed.
Example Kotlin code (using Coroutines for background processing):
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.net.HttpURLConnection import java.net.URL fun checkAppOnPlayStore(packageName: String): Boolean { val playStoreUrl = "https://play.google.com/store/apps/details?id=$packageName" return try { val url = URL(playStoreUrl) val connection = url.openConnection() as HttpURLConnection connection.requestMethod = "HEAD" // Avoid downloading full page content connection.connectTimeout = 5000 connection.readTimeout = 5000 connection.responseCode == HttpURLConnection.HTTP_OK } catch (e: Exception) { // Handle network errors—you might want to default to "live" here, or adjust based on your needs false } } // Call this from your app (always run network tasks off the main thread!) fun verifyAppStatus() { launch(Dispatchers.Main) { val isLive = withContext(Dispatchers.IO) { checkAppOnPlayStore("com.your.app.package") } if (!isLive) { // Trigger your fallback actions here showUnavailableToast() // or navigate to a custom Activity openFallbackActivity() } } }
Option 2: Google Play Developer API (for app owners)
If you’re the app’s official developer and want a 100% accurate status check, use the Google Play Developer API’s edits.tracks.get endpoint. This requires setting up a service account linked to your Play Console project, but it directly pulls the app’s publishing status (e.g., "published" = live, "draft" = not available).
Quick setup steps:
- Create a service account in the Google Cloud Console
- Link the service account to your Play Console project (under Settings > API access)
- Use the official API client library to fetch the track status and parse the response
Once you’ve confirmed the app is no longer on Play Store, adding the fallback logic is straightforward:
Show a Toast notification:
fun showUnavailableToast() { Toast.makeText( applicationContext, "This app is no longer available on Google Play.", Toast.LENGTH_LONG ).show() }
Jump to a specified Activity:
fun openFallbackActivity() { val intent = Intent(this, FallbackActivity::class.java) // Optional: Clear the back stack so users can't navigate back to the main app intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK startActivity(intent) }
- Network Permission: Add
<uses-permission android:name="android.permission.INTERNET" />to yourAndroidManifest.xml - Avoid ANRs: Never run network requests on the main thread—use Coroutines, AsyncTask, or OkHttp’s enqueue method
- Error Handling: Network failures (no internet) can give false negatives. Decide if you want to default to "app is live" or show a network error message to users
- Rate Limiting: Don’t spam the Play Store URL with requests—check once per app session, or add a daily limit
内容的提问来源于stack exchange,提问作者Cristina99

