Skip to content

Commit

Permalink
v1.0.4 获取值方法更新为回调方式,旧方法标记为废弃暂时还可使用
Browse files Browse the repository at this point in the history
  • Loading branch information
flayone committed Nov 24, 2021
1 parent e046e06 commit ca32ecb
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 66 deletions.
24 changes: 8 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,17 @@

### 获取OAID

初始化

```
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化代码获取的oaid值
MyOAID.init(this);
}
}
```

获取oaid
获取oaid结果有可能是异步返回的,使用时请注意这一点差异


```
MyOAID.getOAID(this)
MyOAID.getOAID(this, new ResultCallBack() {
@Override
public void onResult(final String oaid) {
//获取到的oaid结果,如果是未支持的设备,有可能返回值为空
}
});
```

11 changes: 5 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'


//OAID 引入
//推荐使用此方式:源代码方式获取,大小65k
// implementation 'io.github.flayone:oaid-source:1.0'
implementation 'com.github.flayone:OaidProject:v1.0.3'
//OAID gradle 引入,推荐使用此方式:源代码方式获取,大小65k
// implementation 'com.github.flayone.OaidProject:myOaid:v1.0.3.1'

//msa SDK方式获取,大小1.0M
implementation 'io.github.flayone:oaid-sdk:1.0.25'
implementation project(path: ':myOaid')
implementation 'com.github.flayone.OaidProject:msaOaid:v1.0.3.1'


implementation project(path: ':myOaid')
}
3 changes: 0 additions & 3 deletions app/src/main/java/com/lyy/oaidproject/DemoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import android.app.Application;

import com.flayone.oaid.MyOAID;

public class DemoApplication extends Application {

@Override
public void onCreate() {
super.onCreate();

//初始化代码获取的oaid值
MyOAID.init(this);
}
}
42 changes: 28 additions & 14 deletions app/src/main/java/com/lyy/oaidproject/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,61 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.flayone.oaid.AppIdsUpdater;
import com.flayone.oaid.MyOAID;
import com.flayone.oaid.OAIDHelper;
import com.flayone.oaid.ResultCallBack;

public class MainActivity extends AppCompatActivity {

TextView oaidTextCode;
TextView oaidTextCodeSaved;
TextView MsaOaid;
TextView MyOaid;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

oaidTextCodeSaved = findViewById(R.id.codeSaved);
oaidTextCode = findViewById(R.id.code);
MyOaid = findViewById(R.id.myoaid);
MsaOaid = findViewById(R.id.msaoaid);

getOAID();
}

//在init时已经获取了oaid值,如果有返回,此时会直接拿到本地存储的oaid值,不需等待。当APP第一次启动时,部分机型可能无法获取到,之后启动如果有值均能快速拿到。
oaidTextCodeSaved.setText("oaid 存储值:\n " + MyOAID.getOAID(this));
private void getOAID() {

//实时值部分机型上由于是异步的,获取的比较慢,不建议直接使用此方式获取
OAIDHelper.getOAid(this, new AppIdsUpdater() {
//MyOaid SDK方式(推荐)
MyOAID.getOAID(this, new ResultCallBack() {
@Override
public void OnIdsAvalid(@NonNull final String id) {

public void onResult(final String oaid) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
oaidTextCode.setText("oaid 实时值:\n " + id);

MyOaid.setText("MyOaid SDK获取的Oaid值:\n " + oaid);
}
});
}
});

//msa SDK方式
new MsaOaidHelper(new MsaOaidHelper.OaidUpdater() {
@Override
public void IdReceived(@NonNull final String id) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
MsaOaid.setText("Msa SDK获取的Oaid值:\n " + id);
}
});
}
}).getDeviceIds(this);
}

public void refresh(View view) {
getOAID();
}
}
25 changes: 12 additions & 13 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,39 @@
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />

<TextView
<Button
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="建议application中初始化,使用时获取 oaid 存储值使用,因为一部分手机(比如一加、oppo等)获取值是异步的,可能比较比较久才会返回oaid值。"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="@id/center"
android:layout_marginTop="20dp"
android:onClick="refresh"
android:text="重新获取"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/codeSaved"
android:id="@+id/myoaid"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="my oaid存储值:"
android:text=""
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="@id/center"
app:layout_constraintRight_toRightOf="parent "
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />


<TextView
android:id="@+id/code"
android:id="@+id/msaoaid"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="my oaid实时获取值:"
android:text=""
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
app:layout_constraintVertical_bias="0.5" />


</androidx.constraintlayout.widget.ConstraintLayout>
3 changes: 1 addition & 2 deletions myOaid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ group = 'com.github.flayone'


def pomName = "oaid-source"
def oaidVersionName = "1.0.3"
//def oaidVersionName = "1.0-SNAPSHOT"
def oaidVersionName = "1.0.4"
def outputName = pomName + "_" + oaidVersionName + ".aar"

android {
Expand Down
45 changes: 39 additions & 6 deletions myOaid/src/main/java/com/flayone/oaid/MyOAID.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public class MyOAID {
public static String SP_OAID = "myoaid";


@Deprecated
public static void init(final Context context) {
init(context, null);
}

@Deprecated
public static void init(final Context context, final ResultCallBack callBack) {
OAIDHelper.getOAid(context, new AppIdsUpdater() {
MyOAIDHelper.getOAid(context, new AppIdsUpdater() {
@Override
public void OnIdsAvalid(@NonNull final String oaid) {
try {
Expand All @@ -39,21 +41,52 @@ public void OnIdsAvalid(@NonNull final String oaid) {
});
}

public static void getOAID(Context context, final ResultCallBack callBack) {
try {
String insOaid = OAIDManager.getInstance().getOaId();
String savedOaid = OAIDManager.getSavedString(context, SP_OAID);
if (TextUtils.isEmpty(insOaid)) {
if (TextUtils.isEmpty(savedOaid)) {
//获取oaid
init(context, new ResultCallBack() {
@Override
public void onResult(String oaid) {

if (callBack != null) {
callBack.onResult(oaid);
}
}
});
} else {
if (callBack != null) {
callBack.onResult(savedOaid);
}
}
} else {
if (callBack != null) {
callBack.onResult(insOaid);
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}

@Deprecated
public static String getOAID(Context context) {
String result = "";
try {
String insOaid = OAIDManager.getInstance().getOaId();
String savedOaid = OAIDManager.getSavedString(context, SP_OAID);
String oaid;
if (TextUtils.isEmpty(insOaid)) {
oaid = savedOaid;
result = savedOaid;
} else {
oaid = insOaid;
result = insOaid;
}
return oaid;
} catch (Throwable e) {
e.printStackTrace();
return "";
}
return result;
}

public static String getVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
* @author AF
* @time 2020/4/14 17:11
*/
public class OAIDHelper {
public class MyOAIDHelper {

private AppIdsUpdater appIdsUpdater;

public OAIDHelper(AppIdsUpdater callback) {
public MyOAIDHelper(AppIdsUpdater callback) {
appIdsUpdater = callback;
}

public OAIDHelper() {
public MyOAIDHelper() {
}

/**
Expand All @@ -46,13 +46,13 @@ public static void getOAid(Context context, AppIdsUpdater mAppIdUpdateListener)
try {
String oaid = null;
String manufacturer = getManufacturer().toUpperCase();
Log.d("DevicesIDsHelper", "manufacturer===> " + manufacturer);
Log.d("MyOAIDHelper", "manufacturer===> " + manufacturer);
if (mAppIdUpdateListener == null) {
return;
}

if (isFreeMeOS() || isSSUIOS()) {
getOAIDByNewThread(context,mAppIdUpdateListener);
getOAIDByNewThread(context, mAppIdUpdateListener);
}
DeviceTypeEnum deviceType = DeviceTypeEnum.getInstance(manufacturer);
switch (deviceType) {
Expand All @@ -61,7 +61,7 @@ public static void getOAid(Context context, AppIdsUpdater mAppIdUpdateListener)
case Oppo:
case OnePlus:
case ZTE:
getOAIDByNewThread(context,mAppIdUpdateListener);
getOAIDByNewThread(context, mAppIdUpdateListener);
break;
case Lenove:
case Motolora:
Expand Down Expand Up @@ -93,6 +93,35 @@ public static void getOAid(Context context, AppIdsUpdater mAppIdUpdateListener)

}

/**
* 判断是否为异步获取oaid的系统
*
* @param context
* @return
*/
public static boolean isAsynchronousSystem(Context context) {
boolean result = false;
try {
if (isFreeMeOS() || isSSUIOS()) {
return true;
}
String manufacturer = getManufacturer().toUpperCase();
DeviceTypeEnum deviceType = DeviceTypeEnum.getInstance(manufacturer);
switch (deviceType) {
case HuaShuo:
case HuaWei:
case Oppo:
case OnePlus:
case ZTE:
result = true;
break;
}
} catch (Throwable e) {
e.printStackTrace();
}
return result;
}


/**
* 这些平台获取OAID是耗时操作,需要放在异步线程中
Expand Down

0 comments on commit ca32ecb

Please sign in to comment.