android判断及处理设备是否支持Google服务
1.在project/build.gradle文件中添加Google服务系列代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
buildscript { repositories { google() jcenter() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:3.2.0' classpath 'com.google.gms:google-services:4.2.0' } } allprojects { repositories { google() jcenter() mavenCentral() } } |
2.在app/build.gradle文件中添加Google服务系列代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 lintOptions { checkReleaseBuilds false abortOnError false } useLibrary 'org.apache.http.legacy' packagingOptions { exclude 'META-INF/LICENSE' } sourceSets { main { jniLibs.srcDirs = ['libs'] } } dexOptions { javaMaxHeapSize "4g" preDexLibraries true maxProcessCount 8 } defaultConfig { applicationId "com.android.app" minSdkVersion 15 targetSdkVersion 27 versionCode 1 versionName "1.0.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" multiDexEnabled true ndk { abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86' } vectorDrawables.useSupportLibrary true } buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) implementation 'com.android.support:support-v4:28.0.0' implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:cardview-v7:28.0.0' implementation 'com.android.support:palette-v7:28.0.0' implementation 'com.android.support:customtabs:28.0.0' implementation 'com.android.support:multidex:1.0.3' implementation 'com.android.support.constraint:constraint-layout:1.1.3' // google服务系列 implementation 'com.google.android.gms:play-services-base:16.1.0' implementation 'com.google.android.gms:play-services-maps:16.1.0' implementation 'com.google.android.gms:play-services-analytics:16.0.7' } apply plugin: 'com.google.gms.google-services' |
3.在代码中添加判断
关于GoogleApiAvailability的API帮助文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.Window; import android.view.WindowManager; import com.addcn.android.hk591new.R; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT); } setContentView(R.layout.activity_main); } @Override protected void onResume() { super.onResume(); onCheckGooglePlayServices(); } /** * 检查 Google Play 服务 */ private void onCheckGooglePlayServices() { // 验证是否已在此设备上安装并启用Google Play服务,以及此设备上安装的旧版本是否为此客户端所需的版本 int code = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { // 支持Google服务 } else { /** * 依靠 Play 服务 SDK 运行的应用在访问 Google Play 服务功能之前,应始终检查设备是否拥有兼容的 Google Play 服务 APK。 * 我们建议您在以下两个位置进行检查:主 Activity 的 onCreate() 方法中,及其 onResume() 方法中。 * onCreate() 中的检查可确保该应用在检查成功之前无法使用。 * onResume() 中的检查可确保当用户通过一些其他方式返回正在运行的应用(比如通过返回按钮)时,检查仍将继续进行。 * 如果设备没有兼容的 Google Play 服务版本,您的应用可以调用以下方法,以便让用户从 Play 商店下载 Google Play 服务。 * 它将尝试在此设备上提供Google Play服务。如果Play服务已经可用,则Task可以立即完成返回。 */ GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(this); // 或者使用以下代码 /** * 通过isUserResolvableError来确定是否可以通过用户操作解决错误 */ if (GoogleApiAvailability.getInstance().isUserResolvableError(code)) { /** * 返回一个对话框,用于解决提供的errorCode。 * @param activity 用于创建对话框的父活动 * @param code 通过调用返回的错误代码 * @param activity 调用startActivityForResult时给出的requestCode */ GoogleApiAvailability.getInstance().getErrorDialog(this, code, 200).show(); } } } } |
4.修复Google服务
通过手机应用市场下载“GG服务框架安装器.apk”文件进行安装,进行一键修复Google服务
来源:https://blog.csdn.net/u011038298/article/details/89676632