无法在 API 级别 17 上获得令牌

Unable to get token on API level 17

我运行这些命令是为了得到一个令牌:

  InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
  String token = instanceID.getToken(Config.GOOGLE_PROJECT_ID,
                             GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);

但是得到这个异常:

java.io.IOException: MISSING_INSTANCEID_SERVICE
            at com.google.android.gms.iid.zzc.zza(Unknown Source)
            at com.google.android.gms.iid.zzc.zzb(Unknown Source)
            at com.google.android.gms.iid.zzc.zza(Unknown Source)
            at com.google.android.gms.iid.InstanceID.zzc(Unknown Source)
            at com.google.android.gms.iid.InstanceID.getToken(Unknown Source)
            at autofocus.com.autofocus.activity.ItemListActivity.doInBackground(ItemListActivity.java:408)
            at autofocus.com.autofocus.activity.ItemListActivity.doInBackground(ItemListActivity.java:398)

知道我做错了什么吗?

根据官方文档:

https://developers.google.com/android/reference/com/google/android/gms/iid/InstanceID#ERROR_MISSING_INSTANCEID_SERVICE

你得到这个错误是因为

Tokens can't be generated. Only devices with Google Play are supported.

在没有 Google Play 服务的模拟器上测试 enabled/installed?

你的模拟器需要安装android版本的gapps,如果使用genymotion你需要先安装gapps di Genymotion-ARM-Translation_v1.1 之后,可能会起作用