Skip to content

Commit

Permalink
华为高版本获取OAID可能发生ANR: #63 #72
Browse files Browse the repository at this point in the history
  • Loading branch information
liyujiang-gzu committed Nov 12, 2023
1 parent fe800fb commit 0019be2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
10 changes: 9 additions & 1 deletion app/src/main/java/com/github/gzuliyujiang/fallback/DemoApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ public class DemoApp extends Application {

static {
if (BuildConfig.DEBUG) {
//开启日志打印,默认是关闭的
// 开启日志打印,默认是关闭的,启动本应用会打印如下类似的日志:
// IMEI/MEID not allowed on Android 10+
// android.content.pm.PackageManager$NameNotFoundException: com.mdid.msa
// Google Play Service has been found: com.github.gzuliyujiang.oaid.impl.GmsImpl
// Service has been bound: Intent { act=com.google.android.gms.ads.identifier.service.START pkg=com.google.android.gms }
// Service has been connected: com.google.android.gms.ads.identifier.service.AdvertisingIdService
// OAID/AAID acquire success: 3f398576-c70a-455c-95ab-1fe35a9ae175
// Client id is OAID/AAID: 3f398576-c70a-455c-95ab-1fe35a9ae175
// Service has been unbound: com.google.android.gms.ads.identifier.service.AdvertisingIdService
OAIDLog.enable();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.huawei.hms.ads.identifier.AdvertisingIdClient;

import java.io.IOException;
import java.util.concurrent.Executors;

/**
* 参阅华为官方 <a href="https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/identifier-service-integrating-sdk-0000001056460552">HUAWEI Ads SDK</a>。
Expand Down Expand Up @@ -62,22 +63,30 @@ public void doGet(final IGetter getter) {
if (context == null || getter == null) {
return;
}
try {
AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(context);
if (info == null) {
getter.onOAIDGetError(new OAIDException("Advertising identifier info is null"));
return;
}
if (info.isLimitAdTrackingEnabled()) {
// 实测在系统设置中关闭了广告标识符,将获取到固定的一大堆0
getter.onOAIDGetError(new OAIDException("User has disabled advertising identifier"));
return;
// 获取OAID信息(SDK方式)
// 参阅 https://developer.huawei.com/consumer/cn/doc/HMSCore-Guides/identifier-service-obtaining-oaid-sdk-0000001050064988
// 华为官方开发者文档提到“调用getAdvertisingIdInfo接口,获取OAID信息,不要在主线程中调用该方法。”
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(context);
if (info == null) {
getter.onOAIDGetError(new OAIDException("Advertising identifier info is null"));
return;
}
if (info.isLimitAdTrackingEnabled()) {
// 实测在系统设置中关闭了广告标识符,将获取到固定的一大堆0
getter.onOAIDGetError(new OAIDException("User has disabled advertising identifier"));
return;
}
getter.onOAIDGetComplete(info.getId());
} catch (IOException e) {
OAIDLog.print(e);
getter.onOAIDGetError(e);
}
}
getter.onOAIDGetComplete(info.getId());
} catch (IOException e) {
OAIDLog.print(e);
getter.onOAIDGetError(e);
}
});
}

}

0 comments on commit 0019be2

Please sign in to comment.