简体   繁体   English

如何检测另一个Android设备?

[英]How to detect another Android device?

I have a requirement that if I run my app in one Android device and then try to run the same app in another Android device I need to check first if its another device or not and if it is then I should continue. 我有一个要求,如果我在一个Android设备上运行我的应用程序,然后尝试在另一个Android设备上运行相同的应用程序,我需要首先检查是否是其他设备,如果是,那么我应该继续。 Could you please tell me if there is any way in which I can achieve this? 你能否告诉我是否有办法实现这一目标?

Thank you. 谢谢。

you can use this: 你可以用这个:

telephonyManager = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();
telephonyManager.getSimSerialNumber();

getDeviceId and getSimSerialNumber are unique per device, so you can check this values getDeviceIdgetSimSerialNumber对于每个设备都是唯一的,因此您可以检查此值

To Achieve This the problem is this : 实现这个问题是这样的:

How can we differentiate, if we are installing app,
first time on 1st device or first time on second device.
so for that we have to use a unique key for app to run 
on each different device.
So that before saving shared pref,
we can validate the unique key for that device. 

Now Question is that how user will get the unique key? 现在问题是用户将如何获得唯一密钥? So lets assume application vendor will provide key for each device. 因此,假设应用程序供应商将为每个设备提供密钥。 but for that, app should have some algorithm to validate the key provided by us. 但为此,应用程序应该有一些算法来验证我们提供的密钥。

But Unfortunately, If vendor provide the key, user can use that key to install on another device also. 但不幸的是,如果供应商提供密钥,用户也可以使用该密钥安装在另一台设备上。

SO The Final Solution is before first run app Must register device on Some Web Service for Activation. 因此,最终解决方案是在首次运行应用程序之前必须在某些Web服务激活上注册设备。

Set a flag in shared preferences in first run 首次运行时在共享首选项中设置标志

SharedPreferences preferences = getSharedPreferences("PREF_NAME", 0);
boolean onlyonce = preferences.getBoolean("FLAG_NAME", false);

if (!onlyonce ) {
   SharedPreferences.Editor editor = preferences.edit();
   editor.putBoolean("FLAG_NAME", true);
   editor.commit();
}

There is a risk if user resets application it gets cleared also. 如果用户重置应用程序,它也会被清除。 Will this solve your problem? 这会解决你的问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM