Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize JD.com 's second kill function with Android

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article "how to use Android to achieve JD.com second kill function" most people do not understand, so the editor summed up the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "how to use Android to achieve JD.com second kill function" article it.

First of all, look at the effect picture:

JD.com 's second kill is a game every two hours. After we get the number of games, we get the final time through the number of games + two hours later, take the timestamp of the final time, subtract it from the current timestamp, and get the remaining hours, minutes and seconds. Can achieve the countdown function!

The specific code implementation is as follows:

1. Layout Page activity_seckill.xml

two。 The background file of the text is time_back.xml

3.SeckillActivity class, the specific comments have been given in the code

Public class SeckillActivity extends AppCompatActivity {/ / second kill private TextView tv_screening; / / 2 hours per second kill, how many hours left before the end of the second kill private TextView tv_hours; / / minutes left private TextView tv_minutes; / / number of seconds left private TextView tv_second; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_seckill) Tv_screening = findViewById (R.id.tv_screening); tv_hours = findViewById (R.id.tv_hours); tv_minutes = findViewById (R.id.tv_minutes); tv_second = findViewById (R.id.tv_second); / / calculate the number of seconds, one handler.sendEmptyMessage every two hours (0x00) } Handler handler = new Handler (new Handler.Callback () {@ Override public boolean handleMessage (@ NonNull Message msg) {if (msg.what = = 0x00) {/ / set time mkTime ();} handler.sendEmptyMessageDelayed (0x00, 1000); return true;}}) Private void mkTime () {try {/ / parse the date SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") using the given schema; StringBuilder stringBuilder = new StringBuilder (); String format = sdf.format (new Date ()); / / get the current year, month and day String substring = format.substring (0,11) StringBuilder.append (substring); / / get the calendar object Calendar calendar = Calendar.getInstance (); / / get the current 24-hour number of hours in a day int hours = calendar.get (Calendar.HOUR_OF_DAY) / / get if (hours% 2 = = 0) {tv_screening.setText (hours + "Point Field"); stringBuilder.append (hours + 2);} else {tv_screening.setText ((hours-1) + "Point Field"); stringBuilder.append (hours + 1) } stringBuilder.append (": 00:00"); Date sessionDate = sdf.parse (stringBuilder.toString ()); / / get the second kill times + two-hour timestamp long sessionDateTime = sessionDate.getTime (); / / get the timestamp of the current time Date date = new Date (); long millisecond = date.getTime () / / interval timestamp long timestampMillisecond = sessionDateTime-millisecond; / / hours remaining long hour = timestampMillisecond / (1000 * 60 * 60); / / minutes remaining long minute = (timestampMillisecond-hour * (1000 * 60 * 60)) / (1000 * 60) / / ① method: obtain remaining seconds / / long second = (timestampMillisecond-hour * (1000 * 60 * 60)-minute * (1000 * 60)) / 1000; / / ② method: obtain remaining seconds / / the remainder is also millisecond long test = timestampMillisecond% (60 * 1000) / / number of seconds left Math.round returns int type integer long second = Math.round ((float) (test / 1000)); tv_hours.setText ("0" + hour); if (minute > = 10) {tv_minutes.setText (minute + "") } else {tv_minutes.setText ("0" + minute);} if (second > = 10) {tv_second.setText (second + "");} else {tv_second.setText ("0" + second);}} catch (Exception e) {e.printStackTrace () } the above is about the content of this article on "how to use Android to achieve JD.com 's second kill function". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report