You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过编程检测应用是否在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.

1. Detect if your app is live on Google Play

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:

  1. Create a service account in the Google Cloud Console
  2. Link the service account to your Play Console project (under Settings > API access)
  3. Use the official API client library to fetch the track status and parse the response
2. Implement Toast or Activity navigation for unavailable apps

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)
}
Key Things to Remember
  • Network Permission: Add <uses-permission android:name="android.permission.INTERNET" /> to your AndroidManifest.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:00:05