ASimpleCache开源缓存框架使用之实现抽签小程序
来源:互联网 发布:网购秒杀软件哪个好 编辑:程序博客网 时间:2024/05/21 20:27
使用ASimpleCache开源缓存框架实现抽签小程序
最近在考虑数据缓存的时候发现了这个ASimpleCache,简单到只需要拷贝一个Java文件,正好最近团队有个活动需要抽签,我就做了一个抽签的小程序。介绍如下。
- 设定足够的签数,每次抽取之后不会再出现
- 将已经抽签的结果进行缓存,保证下次重启程序的时候不会重新开始抽签
运行效果图:
代码实现思路:
1.通过ACache的对象在缓存中获取值来判断是否为空,如果是空,证明目前没有缓存,直接进行赋值就可以,如果不为空,则操作获取到的数据。
JSONArray array = aCache.getAsJSONArray("list");if (array != null) { for (int i = 0; i < array.length(); i++) { try { list.add(array.getInt(i)); } catch (JSONException e) { e.printStackTrace(); } } } else { for (int i = 0; i < number_all; i++) { list.add(i); } }
2.当抽签完成之后,要把抽到的数据从list中删除,并且把当前的数据进行缓存。
list.remove(list.get(tag)); JSONArray array = new JSONArray(); for (int i = 0; i < list.size(); i++) { array.put(list.get(i)); } aCache.put("list", array);
主要就是这两个点。因为我这还加了个筛选耗时的操作,所以用Thread和Handler来进行线程和UI更新的操作。
项目的详细代码如下:如果还有下载ACache.java的话,请点击这里 ,新手第一次写博客。不喜勿喷。这个小程序当时写的仓促,但是功能还是实现了,仅供参考,有更好的实现思路和建议请留言评论。
xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#FF4500" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:orientation="horizontal" > <TextView android:id="@+id/text_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="王者荣耀抽签" android:textColor="#FFFFFF" android:textSize="18sp" /> </LinearLayout> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="已抽签数:" /> <TextView android:id="@+id/text_yichou" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="0" android:textColor="#FF4500" android:textSize="20sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="剩余签数:" /> <TextView android:id="@+id/text_shengyu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="70" android:textColor="#FF4500" android:textSize="20sp" android:textStyle="bold" /> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#F0F0F0" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" > <TextView android:id="@+id/text_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textColor="#000000" android:textSize="100sp" /> <Button android:id="@+id/btn_start" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:layout_marginTop="40dp" android:background="@drawable/circle" android:text="开始" android:textColor="#FFFFFF" android:textSize="25dp" /> </LinearLayout> </RelativeLayout></LinearLayout>
java代码
public class ChouQian extends Activity { private TextView text_yichou = null; private TextView text_weichou = null; private TextView text_number = null; private Button start = null; private IHandler handler = new IHandler(); private TimerTask task = null; private Timer timer = null; private int time = 15; private boolean chouqian = true; private ArrayList<Integer> list = new ArrayList<Integer>(); private int tag = 0; private int number_yichou = 0; private int number_all = 70; private int number_weichou = number_all; private ACache aCache = null; private boolean slow = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); super.setContentView(R.layout.chouqian); aCache = ACache.get(getApplicationContext()); initViews(); } private void initViews() { text_number = (TextView) findViewById(R.id.text_number); text_weichou = (TextView) findViewById(R.id.text_shengyu); text_yichou = (TextView) findViewById(R.id.text_yichou); start = (Button) findViewById(R.id.btn_start); JSONArray array = aCache.getAsJSONArray("list"); if (array != null) { for (int i = 0; i < array.length(); i++) { try { list.add(array.getInt(i)); } catch (JSONException e) { e.printStackTrace(); } } number_yichou = number_all - list.size(); number_weichou = list.size(); text_weichou.setText(list.size() + ""); text_yichou.setText((number_all - list.size()) + ""); } else { for (int i = 0; i < number_all; i++) { list.add(i); } } start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { if (number_yichou >= number_all) { Toast.makeText(getApplicationContext(), "已经全部抽完啦", 200) .show(); } else { chouqian = true; time = 15; if (slow) { ChouQianThread(); } else { Toast.makeText(getApplicationContext(), "抽的太快了", 100) .show(); } } } }); } private void ChouQianThread() { timer = new Timer(); task = new TimerTask() { @Override public void run() { if (chouqian) { if (time >= 0) { handler.sendEmptyMessage(0); slow = false; } else { handler.sendEmptyMessage(1); slow = true; chouqian = false; } } } }; timer.schedule(task, 0, 100); } private class IHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: tag = (int) (Math.random() * list.size()); text_number.setText(list.get(tag) + ""); time--; break; case 1: number_yichou++; number_weichou--; if (number_yichou <= number_all) { text_yichou.setText(number_yichou + ""); } if (number_weichou >= 0) { text_weichou.setText(number_weichou + ""); } text_number.setText(list.get(tag) + ""); list.remove(list.get(tag)); JSONArray array = new JSONArray(); for (int i = 0; i < list.size(); i++) { array.put(list.get(i)); } aCache.put("list", array); break; } } }}
谢谢阅读!
0 0
- ASimpleCache开源缓存框架使用之实现抽签小程序
- ASimpleCache缓存框架之简单使用
- ASimpleCache缓存框架之简单使用
- ASimpleCache缓存框架之简单使用
- 轻量级的 开源缓存框架ASimpleCache
- Android轻量级的开源缓存框架ASimpleCache
- android缓存框架ASimpleCache
- Android缓存框架--ASimpleCache
- android缓存框架ASimpleCache
- Android 缓存框架:ASimpleCache
- Android 缓存框架 ASimpleCache
- android缓存框架ASimpleCache
- android缓存框架ASimpleCache
- android缓存框架ASimpleCache
- android缓存框架ASimpleCache
- 每日一记之ASimpleCache缓存框架的使用方法
- 使用缓存框架 ASimpleCache和SharePreferences进行数据储存
- 开源框架——轻量级缓存框架——ACache(ASimpleCache)
- |洛谷|动态规划|P1968 美元汇率
- 文章标题
- UnsatisfiedLinkError: dlopen "/data/data/<package>/app_jni_lib/libxxx.so is 32-bit instead of 64-bit
- linux文件系统详解
- linux 下安装jdk并且配置环境变量
- ASimpleCache开源缓存框架使用之实现抽签小程序
- Coursera Machine Learning 第一周 引言(Introduction) quiz 习题答案
- 利用蚁群算法进行优化PID参数(个人总结)
- C语言中函数strlen等的实现
- 对象和数组:JVM中,处理对象和数组的字节码介绍
- 【BZOJ 4326】运输计划【树链剖分+差分+二分答案】
- 413. Arithmetic Slices
- scikit-learn 线性回归算法库小结
- C# RSA 无 长度限制 加密解密 示例