用Android实现京东秒杀功能详解

 更新时间:2022年01月26日 11:26:38   作者:路宇  
大家好,本篇文章主要讲的是用Android实现京东秒杀功能详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下

首先看效果图:

在这里插入图片描述

京东秒杀是两个小时一个场次,我们获取到场次后,通过场次+两个小时后,获取到最终的时间,拿最终时间的时间戳,与当前时间时间戳相减,求得剩余的小时,分钟,秒数,即可实现倒计时功能!

具体代码实现如下:

1.布局页面activity_seckill.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SeckillActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center"
        android:text="仿京东秒杀"
        android:textColor="@color/black"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_screening"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="几点场:"
            android:textColor="@color/black"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_hours"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/time_back"
            android:gravity="center"
            android:text="00"
            android:textColor="@color/white"
            android:textSize="15sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=":"
            android:textColor="#fd5343"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_minutes"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/time_back"
            android:gravity="center"
            android:text="00"
            android:textColor="@color/white"
            android:textSize="15sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=":"
            android:textColor="#fd5343"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_second"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/time_back"
            android:gravity="center"
            android:text="00"
            android:textColor="@color/white"
            android:textSize="15sp" />
    </LinearLayout>
</LinearLayout>

2.文本的背景文件为time_back.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#fd5343" />
    <corners android:radius="10dp" />
</shape>

3.SeckillActivity类,具体注释已经在代码中给出

public class SeckillActivity extends AppCompatActivity {
    //秒杀场次
    private TextView tv_screening;
    //2个小时一个秒杀场次,距离秒杀结束剩余多少小时
    private TextView tv_hours;
    //剩余分钟数
    private TextView tv_minutes;
    //剩余秒数
    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);

        //计算秒杀场次,两个小时一个场次
        handler.sendEmptyMessage(0x00);
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0x00) {
                //设置时间
                mkTime();
            }

            handler.sendEmptyMessageDelayed(0x00, 1000);
            return true;
        }
    });

    private void mkTime() {
        try {
            //使用给定的模式解析日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            StringBuilder stringBuilder = new StringBuilder();
            String format = sdf.format(new Date());
            //获取当前的年月日
            String substring = format.substring(0, 11);
            stringBuilder.append(substring);

            //获取日历对象
            Calendar calendar = Calendar.getInstance();
            //获取一天中当前的小时数 24小时制的
            int hours = calendar.get(Calendar.HOUR_OF_DAY);
            //获取秒杀场次
            if (hours % 2 == 0) {
                tv_screening.setText(hours + "点场");
                stringBuilder.append(hours + 2);
            } else {
                tv_screening.setText((hours - 1) + "点场");
                stringBuilder.append(hours + 1);
            }
            stringBuilder.append(":00:00");
            Date sessionDate = sdf.parse(stringBuilder.toString());
            //获取秒杀场次+两个小时 的时间戳
            long sessionDateTime = sessionDate.getTime();

            //获取当前时间的时间戳
            Date date = new Date();
            long millisecond = date.getTime();

            //间隔时间戳
            long timestampMillisecond = sessionDateTime - millisecond;

            //剩余小时数
            long hour = timestampMillisecond / (1000 * 60 * 60);
            //剩余分钟数
            long minute = (timestampMillisecond - hour * (1000 * 60 * 60)) / (1000 * 60);
            //第①种方法: 获得剩余秒数
//            long second = (timestampMillisecond - hour * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000;

            //第②种方法: 获得剩余秒数
            //取余数 得到的也是毫秒数
            long test = timestampMillisecond % (60 * 1000);
            //剩余的秒数 Math.round按照四舍五入返回最接近参数的int型整数
            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();
        }
    }
}

以上就是京东秒杀的具体实现

总结

到此这篇关于用Android实现京东秒杀功能详解的文章就介绍到这了,更多相关Android京东秒杀功能内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android 自定义View步骤

    Android 自定义View步骤

    这篇文章主要介绍了Android 自定义View步骤 的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • Android中使用Post请求的方法

    Android中使用Post请求的方法

    这篇文章主要介绍了Android中使用Post请求的方法,实例分析了Android中使用post请求的原理与具体实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • android使用flutter的ListView实现滚动列表的示例代码

    android使用flutter的ListView实现滚动列表的示例代码

    现如今打开一个 App,比如头条、微博,都会有长列表,那么android使用flutter的ListView滚动列表如何实现,本文就来详细的介绍一下,感兴趣的同学可以来了解一下
    2018-12-12
  • Android深入分析属性动画源码

    Android深入分析属性动画源码

    这篇文章主要给大家介绍了关于Android动画系列教程之属性动画的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • Flutter 仿微信支付界面

    Flutter 仿微信支付界面

    网传微信支付页面的第三方链接一个格子需要广告费1一个亿,微信支付页非常适合做功能导航,本篇使用 ListView和 GridView 模仿了微信支付的页面,同时介绍了如何装饰一个组件的背景和边缘样式。
    2021-05-05
  • Android编程开发之RadioGroup用法实例

    Android编程开发之RadioGroup用法实例

    这篇文章主要介绍了Android编程开发之RadioGroup用法,结合实例形式分析了Android中RadioGroup单选按钮的具体使用技巧,需要的朋友可以参考下
    2015-12-12
  • Android自定义View仿QQ健康界面

    Android自定义View仿QQ健康界面

    这篇文章主要为大家详细介绍了Android自定义View仿QQ健康界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android简单实现 缓存数据

    Android简单实现 缓存数据

    这篇文章主要介绍了Android简单实现 缓存数据,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据

    ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据

    之前写的绑定数据是只是简单的绑定了字符串,这次我们将一次绑定多条数据并且尝试用自定义的布局。在这篇文章中首先讲解的是用Hashmap 去绑定数据,第二个例子,讲解自定义布局然后绑定数据
    2013-06-06
  • android中AutoCompleteTextView的简单用法(实现搜索历史)

    android中AutoCompleteTextView的简单用法(实现搜索历史)

    本篇文章主要介绍了android中AutoCompleteTextView的简单用法(自动提示),有需要的可以了解一下。
    2016-11-11

最新评论