首页 文章 精选 留言 我的

精选列表

搜索[android things],共10000篇文章
优秀的个人博客,低调大师

Android Studio 使用Intent实现页面的跳转(带参数)

不管是在APP,还是在网站中,页面之间的跳转都是很常见的,本文主要讲一下在APP中,如何通过Intent实现页面的跳转。 不带参数: 写在MainActivity页面的代码: 1 Intent intent = new Intent(); 2 intent.setClass(MainActivity.this, LoginActivity.class);//从MainActivity页面跳转至LoginActivity页面 3 this.startActivity(intent); 带参数: 写在SpendingActivity页面的代码: 1 Intent intent=new Intent(SpendingActivity.this,ExpenseProcesActivity.class);//从SpendingActivity页面跳转至ExpenseProcesActivity页面 2 intent.putExtra("strType", 0);//参数:name、value 3 SpendingActivity.this.startActivity(intent); 写在ExpenseProcesActivity接收页面的代码: 1 private int type = 0; 2 3 //接收传递过来的参数 4 final Intent intent = getIntent(); 5 type = intent.getIntExtra("strType", 0); 备注: 可传递的参数有多种类型,在接收参数的时候,也要根据传入类型,选用对应的接收函数 传递参数的类型,Eg: 对应的接收函数,Eg: 如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐] 如果您想转载本博客,请注明出处 如果您对本文有意见或者建议,欢迎留言 感谢您的阅读,请关注我的后续博客

优秀的个人博客,低调大师

Android Studio 点击两次返回键,退出APP

该功能的实现没有特别复杂,主要在onKeyDown()事件中实现,直接上代码,如下: 1 //第一次点击事件发生的时间 2 private long mExitTime; 3 4 /** 5 * 点击两次返回退出app 6 */ 7 @Override 8 public boolean onKeyDown(int keyCode, KeyEvent event) { 9 if (keyCode == KeyEvent.KEYCODE_BACK) { 10 if ((System.currentTimeMillis() - mExitTime) > 2000) { 11 Object mHelperUtils; 12 Toast.makeText(this, "再按一次退出APP", Toast.LENGTH_SHORT).show(); 13 //System.currentTimeMillis()系统当前时间 14 mExitTime = System.currentTimeMillis(); 15 } else { 16 finish(); 17 } 18 return true; 19 } 20 return super.onKeyDown(keyCode, event); 21 } 如果您看了本篇博客,觉得对您有所收获,请点击右下角的 [推荐] 如果您想转载本博客,请注明出处 如果您对本文有意见或者建议,欢迎留言 感谢您的阅读,请关注我的后续博客

优秀的个人博客,低调大师

4-SII--☆Android缓存文件(带有效时长)封装

零、前言 [1]把我的缓存文件工具改写成了策略模式,感觉还不错。 [2]以前静态方法调用,很方便,但看着就是不爽,代码真的太冗余了。 [3]突然灵光一闪,"少年,看你骨骼惊奇,策略模式了解一下吗。"便有此文。 [4]如果不了解SharedPreferences,可以先看这篇:1-SII--SharedPreferences完美封装 缓存策略类图 缓存策略.png 一、使用: //新建缓存对象 CacheWorker innerCache = new CacheWorker(new InnerFileStrategy(this)); //设置缓存 innerCache.setCache("toly", "hehe", 10); //获取缓存 String value = innerCache.getCache("toly"); //SD卡缓存 CacheWorker sdCache = new CacheWorker(new SDFileStrategy()); sdCache.setCache("toly", "hehe2", 10); String sdCach = sdCache.getCache("toly"); //SharedPreferences CacheWorker spCache = new CacheWorker(new SPStrategy(this)); spCache.setCache("toly1994.com", "{name:toly}", 10); String spValue = spCache.getCache("toly1994.com"); 缓存.png 二、附录:各类及实现 /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:6:20<br/> * 邮箱:1981462002@qq.com<br/> * 说明:缓存策略接口 */ public interface CacheStrategy { /** * 存储缓存 * @param key 文件名 * @param value 文件内容 * @param time 有效时间 单位:小时 */ void setCache(String key, String value,long time); /** * 获取缓存 * @param key 文件名 * @return 文件内容 */ String getCache(String key); } /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:6:38<br/> * 邮箱:1981462002@qq.com<br/> * 说明:文件缓存基类 */ public abstract class BaseFileStrategy implements CacheStrategy { /** * 缓存文件的文件夹名称 */ private String mDirName; /** * 构造函数 * @param dirName 缓存文件的文件夹名称 */ public BaseFileStrategy(String dirName) { mDirName = dirName; } @Override public void setCache(String key, String value, long time) { // 以url为文件名, 以json为文件内容,保存在本地 // 生成缓存文件 File cacheFile = new File(mDirName + File.separator + Md5Util.getMD5(key)); FileWriter writer = null; try { if (!cacheFile.exists()) { cacheFile.getParentFile().mkdirs(); cacheFile.createNewFile(); } writer = new FileWriter(cacheFile); // 缓存失效的截止时间 long deadline = System.currentTimeMillis() + time * 60 * 60 * 1000;//有效期 writer.write(deadline + "\n");// 在第一行写入缓存时间, 换行 writer.write(value);// 写入文件 writer.flush(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.close(writer); } } @Override public String getCache(String key) { // 生成缓存文件 File cacheFile = new File(mDirName + File.separator + Md5Util.getMD5(key)); // 判断缓存是否存在 if (cacheFile.exists()) { // 判断缓存是否有效 BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(cacheFile)); String deadline = reader.readLine();// 读取第一行的有效期 long deadTime = Long.parseLong(deadline); if (System.currentTimeMillis() < deadTime) {// 当前时间小于截止时间, // 说明缓存有效 // 缓存有效 StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); } else { setCache(key, "", 0);//缓存过期清空 } } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.close(reader); } } return null; } } /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:6:28<br/> * 邮箱:1981462002@qq.com<br/> * 说明:以文件保存缓存 到本包 <br/> */ public class InnerFileStrategy extends BaseFileStrategy { public InnerFileStrategy(Context context) { super(context.getCacheDir().getPath()); } } /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:6:28<br/> * 邮箱:1981462002@qq.com<br/> * 说明:以文件保存缓存 到SD卡cacheData目录 <br/> */ public class SDFileStrategy extends BaseFileStrategy { public SDFileStrategy() { super(Environment.getExternalStorageDirectory() +"/cacheData"); } } /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:8:03<br/> * 邮箱:1981462002@qq.com<br/> * 说明:SharedPreferences缓存 */ public class SPStrategy implements CacheStrategy { private Context mContext; public SPStrategy(Context context) { mContext = context; } @Override public void setCache(String key, String value, long time) { SpUtils<String> spString= new SpUtils<>(mContext); spString.put(key, value); SpUtils<Long> spLong = new SpUtils<>(mContext); spLong.put("LiftTime", System.currentTimeMillis() + time * 60 * 60 * 1000); } @Override public String getCache(String key) { SpUtils<Long> spLong = new SpUtils<>(mContext); Long liftTime = spLong.get("LiftTime", 0L); if (System.currentTimeMillis() < liftTime) {// 当前时间小于截止时间, SpUtils<String> spString= new SpUtils<>(mContext); return spString.get(key, ""); }else { setCache(key, "", 0);//缓存过期清空 } return null; } } /** * 作者:张风捷特烈<br/> * 时间:2018/8/26 0026:6:23<br/> * 邮箱:1981462002@qq.com<br/> * 说明:缓存工作类 */ public class CacheWorker { /** * 缓存策略 */ private CacheStrategy mCacheStrategy; /** * 无参构造 */ public CacheWorker() { } /** * 一参构造 * @param cacheStrategy 缓存策略 */ public CacheWorker(CacheStrategy cacheStrategy) { mCacheStrategy = cacheStrategy; } /** * 存储缓存 * @param key 文件名 * @param value 文件内容 * @param time 有效时间 单位:小时 */ public void setCache(String key, String value, long time) { mCacheStrategy.setCache(key, value, time); } /** * 获取缓存 * @param key 文件名 * @return 文件内容 */ public String getCache(String key) { return mCacheStrategy.getCache(key); } /** * 设置缓存策略 * @param cacheStrategy 缓存策略 */ public void setCacheStrategy(CacheStrategy cacheStrategy) { mCacheStrategy = cacheStrategy; } } 后记、 1.声明: [1]本文由张风捷特烈原创,转载请注明 [2]欢迎广大编程爱好者共同交流 [3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正 [4]你的喜欢与支持将是我最大的动力 2.连接传送门: 更多安卓技术欢迎访问:安卓技术栈 我的github地址:欢迎star 简书首发,腾讯云+社区同步更新张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com 3.联系我 QQ:1981462002 邮箱:1981462002@qq.com 微信:zdl1994328 4.欢迎关注我的微信公众号,最新精彩文章,及时送达: 公众号.jpg

优秀的个人博客,低调大师

Android安卓比较完美的无限自动轮播

轮播功能在APP中是比较常见的,网上也有很多第三方的自动轮播库,做的很不错,这里自己手写一版简单的ViewPager+Handler机制的自动无限轮播,小白可以直接上手的。 先上简单的核心代码,代码中有详细的注释。 private void initView() { auto_viewpager = (ViewPager) findViewById(R.id.vp_shuffling); tv_content = (TextView) findViewById(R.id.tv_content); dotLayout = (LinearLayout) findViewById(R.id.layout_point); mADParseArray = new ArrayList<String>(); mADParseArray .add("http://m.easyto.com/m/zhulifuwu_banner.jpg"); mADParseArray .add("http://m.easyto.com/m/japan/images/banner_3y_new.jpg"); mADParseArray .add("http://m.easyto.com/m/japan/images/banner_5y_new.jpg"); final int size = mADParseArray.size(); content = new String[]{"轮播图介绍一", "轮播图介绍二", "轮播图介绍三"}; auto_viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { /** * @param position:跳转完毕的页码角标,这个方法做自动轮播 */ @Override public void onPageSelected(int position) { Log.e(TAG, "onPageSelected"); refreshPoint(position % size); if (mHandler.hasMessages(HOME_AD_RESULT)) { mHandler.removeMessages(HOME_AD_RESULT); } mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3000); } /** * @param arg0:滑动时代表当前页角标,滑动结束时代表滑动停止时的页码角标 * @param arg1:0-1或者1-0变化 * @param arg2:0 <-> 屏幕的宽度值变化 */ @Override public void onPageScrolled(int arg0, float arg1, int arg2) { Log.e(TAG, "onPageScrolled"); } /** * @param arg0:0 没滑动 1:正在滑动 2:滑动完毕 */ @Override public void onPageScrollStateChanged(int arg0) { Log.e(TAG, "onPageScrollStateChanged = " + arg0); if (ViewPager.SCROLL_STATE_DRAGGING == arg0 && mHandler.hasMessages(HOME_AD_RESULT)) { mHandler.removeMessages(HOME_AD_RESULT); } } }); // BannerAdapter adapter = new BannerAdapter(mContext, mADParseArray); auto_viewpager.setAdapter(adapter); initPointsLayout(size); auto_viewpager.setCurrentItem(size * 1000, false);//设置当前position,不设置开始无法右滑 // 利用handler启动自动轮播 mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3000); } handler中 private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { // 广告 case HOME_AD_RESULT: auto_viewpager.setCurrentItem(auto_viewpager.getCurrentItem() + 1, true);//设置播放下一页 break; } } }; 其中比较重要的就是viewpager滑动监听的三个回调方法,注释上都有详细的解释,相信看了这个以后小白应该也会手写一个简单的无限自动轮播功能了。是不是简明易懂0.0 源码地址,直接导入modul即可

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册