简体   繁体   English

创建类似Whats App的唯一消息ID

[英]Create Unique Message Id Like whats app

I am creating chat application so want generate unique message id . 我正在创建聊天应用程序,因此想生成唯一的消息id。 Is it possible never create duplicate message id. 有可能永远不会创建重复的消息ID。

MongoDB's ObjectId is pretty complex is probably one of the good randomness from a unique id point of view. 从唯一的id角度来看,MongoDB的ObjectId非常复杂,可能是良好的随机性之一。 So you can take a sneak peek in their source code to see how they generate it. 因此,您可以先窥探他们的源代码,看看他们是如何生成的。

Leaving the definition from their official documentation here for posterity: 在后代中将其定义从其官方文档中删除:

ObjectIds are small, likely unique, fast to generate, and ordered. ObjectId很小,很可能是唯一的,可以快速生成并排序。 ObjectId values consists of 12-bytes, where the first four bytes are a timestamp that reflect the ObjectId's creation, specifically: ObjectId值由12个字节组成,其中前四个字节是反映ObjectId创建的时间戳,特别是:

 a 4-byte value representing the seconds since the Unix epoch, a 3-byte machine identifier, a 2-byte process id, and a 3-byte counter, starting with a random value. 

Example of Mongo's ObjectId: Mongo的ObjectId示例:

ObjectId("507f1f77bcf86cd799439011")

base on your poor description, you can create compound id. 根据您的不良描述,您可以创建化合物ID。 for example you can create your ides with user id+timestamp. 例如,您可以使用用户id + timestamp创建自己的ide。 and if you use this pattern, your user id length must be same for all ides. 如果使用此模式,则所有ide的用户ID长度必须相同。 so if it is not, you have to add "0" befor your current id to obtain equal length for all of your user ides 因此,如果不是,则必须在当前ID之前添加“ 0”,以使所有用户ide的长度相等

for better description: 以获得更好的描述:

String uniquemsgid= userid+ System.currentTimeMillis();

as a matter of fact, your user have a unique id an timestamp is unique for this user. 实际上,您的用户具有唯一的ID,而该用户的时间戳是唯一的。 caution: if you use only timestamp or a date with any format, this method cant guarantee a unique message id. 注意:如果仅使用时间戳或任何格式的日期,则此方法不能保证唯一的消息ID。 because two user can create a message at a moment 因为两个用户可以同时创建一条消息

There could be many ways to generate one! 可能有很多方法可以产生一个! One common way would be to generate timestamp value and use it as a id which is also unique. 一种常见的方法是生成时间戳记值并将其用作唯一的id。

For example you can do this: 例如,您可以执行以下操作:

public int createID(){    
Date now = new Date();    
int id = Integer.parseInt(new SimpleDateFormat("ddHHmmss",  Locale.US).format(now));    
return id; }

you can also try and make it string and add any specific string format with it to make it more unique according to ur apps need! 您也可以尝试将其设置为字符串,并添加任何特定的字符串格式,以根据您的应用程序的需要使其更加独特!

You might want to use device IMEI number for this, which is always unique and quite easy to get. 您可能需要为此使用设备IMEI编号,该编号始终是唯一的并且很容易获得。

  <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Add above permission in your manifest file and then use the below two lines to get the IMEI. 在清单文件中添加以上权限,然后使用下面的两行获取IMEI。

TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
long id = Long.parseLong(mngr.getDeviceId());

You can make a Random randomId= new Random(); int id = randLan.nextInt(99999) + 1; 您可以创建一个Random randomId= new Random(); int id = randLan.nextInt(99999) + 1; Random randomId= new Random(); int id = randLan.nextInt(99999) + 1; Then you check if Id is already given, and if yes, try again, if not, you have an Id. 然后,您检查是否已提供ID,如果是,请重试,如果没有,则您有一个ID。

if(randomId == someOtherId), do same process again. if(randomId == someOtherId),再次执行相同的过程。

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

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