Nella mia applicazione, devo controllare la versione dei servizi di Google Play (che è installata nel dispositivo dell'utente). È possibile ? E se sì, come posso farlo? Ho cercato su Google ma non sono riuscito a trovare nulla!
Nella mia applicazione, devo controllare la versione dei servizi di Google Play (che è installata nel dispositivo dell'utente). È possibile ? E se sì, come posso farlo? Ho cercato su Google ma non sono riuscito a trovare nulla!
Risposte:
Ho trovato una soluzione semplice:
int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
Ma versionCode
è deprecato nell'API 28, quindi puoi usare PackageInfoCompat
:
long v = PackageInfoCompat.getLongVersionCode(getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ));
Se guardi nel link fornito da Stan0 vedrai questo :
public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE
Versione minima del pacchetto dei servizi di Google Play (dichiarata in AndroidManifest.xml android: versionCode) per essere compatibile con questa versione client. Valore costante: 3225000 (0x003135a8)
Quindi, quando lo imposti nel tuo manifest e poi chiami isGooglePlayServicesAvailable(context)
:
public static int isGooglePlayServicesAvailable (Context context)
Verifica che Google Play Services sia installato e abilitato su questo dispositivo e che la versione installata su questo dispositivo non sia precedente a quella richiesta da questo client.
ritorna
- codice di stato che indica se si è verificato un errore. Può essere uno dei seguenti in ConnectionResult:
SUCCESS
,SERVICE_MISSING
,SERVICE_VERSION_UPDATE_REQUIRED
,SERVICE_DISABLED
,SERVICE_INVALID
.
Garantirà che il dispositivo utilizzi la versione richiesta dalla tua app, in caso contrario, puoi agire in base alla documentazione
GooglePlayServicesUtil.isGooglePlayServicesAvailable()
è deprecato e ora GoogleApiAvailability.isGooglePlayServicesAvailable()
dovrebbe essere utilizzato al suo posto.
ecco la mia soluzione:
private boolean checkGooglePlayServices() {
final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS) {
Log.e(TAG, GooglePlayServicesUtil.getErrorString(status));
// ask user to update google play services.
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, 1);
dialog.show();
return false;
} else {
Log.i(TAG, GooglePlayServicesUtil.getErrorString(status));
// google play services is updated.
//your code goes here...
return true;
}
}
e hai due opzioni, o scrivi il tuo codice direttamente nel blocco else come commentato, o usa il valore booleano restituito per questo metodo per scrivere codice personalizzato.
Spero che questo codice aiuti qualcuno.
Dagli aggiornamenti più recenti sono finito con questo codice, ho creato un metodo pratico per gestire tutte le cose ad esso correlate ..
Tutti i dettagli relativi alla disponibilità del Servizio e i dettagli relativi sono disponibili qui .
private void checkPlayService(int PLAY_SERVICE_STATUS)
{
switch (PLAY_SERVICE_STATUS)
{
case ConnectionResult.API_UNAVAILABLE:
//API is not available
break;
case ConnectionResult.NETWORK_ERROR:
//Network error while connection
break;
case ConnectionResult.RESTRICTED_PROFILE:
//Profile is restricted by google so can not be used for play services
break;
case ConnectionResult.SERVICE_MISSING:
//service is missing
break;
case ConnectionResult.SIGN_IN_REQUIRED:
//service available but user not signed in
break;
case ConnectionResult.SUCCESS:
break;
}
}
Lo uso così
GoogleApiAvailability avail;
int PLAY_SERVICE_STATUS = avail.isGooglePlayServicesAvailable(this);
checkPlayService(PLAY_SERVICE_STATUS);
E per versione GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE;
ti darò.
E una delle risposte più utili che ho trovato durante la mia ricerca è qui.
int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if(status != ConnectionResult.SUCCESS) {
if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){
Toast.makeText(this,"please update your google play service",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "please download the google play service", Toast.LENGTH_SHORT).show();
}
}
Ho incontrato lo stesso problema questa mattina. E l'ho fatto grazie al consiglio di stan0. Grazie stan0.
È necessaria una sola modifica in base al codice di esempio da https://developers.google.com/maps/documentation/android/map .
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
FragmentManager mgr = getFragmentManager();
MapFragment mapFragment = (MapFragment)mgr.findFragmentById(R.id.map);
mMap = mapFragment.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
setUpMap();
}else{
// check if google play service in the device is not available or out-dated.
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
// nothing anymore, cuz android will take care of the rest (to remind user to update google play service).
}
}
}
Inoltre, devi aggiungere un attributo al tuo manifest.xml come:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name"
android:versionCode="3225130"
android:versionName="your.version" >
E il valore di "3225130" è preso da google-play-services_lib che sta utilizzando il tuo progetto. Inoltre, quel valore risiede nella stessa posizione di manifest.xml in google-play-services_lib.
Spero possa essere d'aiuto.
Se cerchi Google Play Services in Gestione applicazioni, verrà visualizzata la versione installata.
Aggiornato (2019.07.03) versione Kotlin:
class GooglePlayServicesUtil {
companion object {
private const val GOOGLE_PLAY_SERVICES_AVAILABLE_REQUEST = 9000
fun isGooglePlayServicesWithError(activity: Activity, showDialog: Boolean): Boolean {
val status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)
return if (status != ConnectionResult.SUCCESS) {
if (showDialog) {
GoogleApiAvailability.getInstance().getErrorDialog(activity, status, GOOGLE_PLAY_SERVICES_AVAILABLE_REQUEST).show()
}
true
} else {
false
}
}
}
}
utilizzare la seguente funzione per verificare se è possibile utilizzare o meno i servizi di Google Play
private boolean checkGooglePlayServicesAvailable() {
final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
return false;
}
return true;
}
int apkVersion = GoogleApiAvailability.getInstance().getApkVersion(getContext());
otterrà lo stesso risultato di:
int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
Ma il secondo deve essere inserito in un try-catch.
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}