gpt4 book ai didi

android - GoogleFit 区分来自不同设备的数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:52 24 4
gpt4 key购买 nike

我正在使用 GoogleFit HistoryAPI用于从用户获取步骤数据。它运作良好。
但是,就我而言,我想区分来自不同设备(使用相同帐户)的数据,因为我不希望用户对 2 个或更多设备使用相同帐户(这是一个基于步骤的游戏,所以我不希望用户在 1 台设备上玩并在许多设备上取得成就)。

我发现 Device来自 DataSource#getDevice , Device#getLocalDevice可以帮助我区分来自不同设备的数据。以下是来自官方文档的信息:

To get information about the device for a data source, use the DataSource.getDevice method. The device information is useful to distinguish from similar sensors on different devices, show the device information from a sensor to the user, or process data differently depending on the device. For example, you may be interested in reading data specifically from the a sensor on a wearable device but not from the same type of sensor on the phone.

To get a Device instance for the device that is running your activity, use the Device.getLocalDevice static method. This is useful when you want to check if a data source is on the same device that your app is running on.

所以,

// My idea is comparing the uid of local device and uid of device in datasource
// if it is same => step come from THIS device
// if not => step come from OTHER device
private void dumpDataSet(DataSet dataSet) {
for (DataPoint dp : dataSet.getDataPoints()) {
Log.i(Constant.TAG, "Origin type uid: " + dp.getOriginalDataSource().getDevice().getUid());
Log.i(Constant.TAG, "Local device uid: " + Device.getLocalDevice(getApplicationContext()).getUid());

...
}
}

但是,只有 1 个设备 的数据源的 logcat 结果是

Origin type uid: 9ff67c48
Local device uid: f2b01c49ecd9c389
// Only the model, manufacturer is same (but I can not use 2 fields because user can use 2 devices with same model, manufacturer)

你可以看到 datasourceuidlocal device uid 不同,所以我不能区分数据。

有什么方法可以区分来自不同设备的数据吗?任何帮助或建议将不胜感激。

DEMO project

最佳答案

据我了解,我认为您为每台设备提供了一个稳定的 ID。使用 ID 的方法很少,您可以在这些方法上进行中继。

public static String getSerial() {
final String serial;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
serial = Build.getSerial();
} else {
serial = Build.SERIAL;
}
return serial;
}

作为另一个示例,通常使用 IMEI 来中继来自 Telephony Manager 的唯一 ID。

public static String getDeviceImei(Context ctx) {
TelephonyManager telephonyManager = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}

最后,但也许是最正确的方法,我建议使用安全 ID。这提供了使用此值的稳定背景。来自文档 在拥有多个用户的设备上,每个用户都显示为一个完全独立的设备,因此 ANDROID_ID 值对每个用户都是唯一的。 Settings.Secure#ANDROID_ID

import android.provider.Settings.Secure;

private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);

关于android - GoogleFit 区分来自不同设备的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51017762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com