[安卓] 安卓实时获取数据并刷新图表
作者:CC下载站 日期:2020-10-30 00:00:00 浏览:56 分类:编程开发
先来个效果图吧
然后直接上代码,解说以后有机会我再补上
assets>index.html
<!doctypehtml> <htmllang="en"> <head> <metacharset="UTF-8"> <metaname="viewport"content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"> <metahttp-equiv="X-UA-Compatible"content="ie=edge"> <scriptsrc="./js/echarts.min.js"></script> <title>Document</title> </head> <body> <divid="main"style="width:400px;height:400px;"></div> <scripttype="text/javascript"src="./js/echarts.min.js"></script> <script> varmyChart=echarts.init(document.getElementById('main')); functionupdateData(jsonstr){ varshuju=JSON.parse(jsonstr); option={ xAxis:{ type:'category', data:shuju.data, }, yAxis:{ type:'value' }, series:[{ data:shuju.wendu, type:'line' }] }; myChart.setOption(option); } </script> </body> </html>
Mainactivity.java
packagecn.lanol.wendu; importandroidx.annotation.NonNull; importandroidx.annotation.UiThread; importandroidx.appcompat.app.AppCompatActivity; importandroid.annotation.SuppressLint; importandroid.content.ContentValues; importandroid.database.Cursor; importandroid.database.sqlite.SQLiteDatabase; importandroid.os.Bundle; importandroid.os.Handler; importandroid.os.Message; importandroid.util.Log; importandroid.view.View; importandroid.webkit.JavascriptInterface; importandroid.webkit.WebView; importandroid.widget.Button; importcom.google.gson.Gson; importcom.google.gson.JsonElement; importcom.google.gson.JsonObject; importcom.google.gson.JsonParser; importjava.io.IOException; importjava.sql.Time; importjava.util.Timer; importjava.util.TimerTask; importcn.lanol.wendu.Helper.GetContext; importcn.lanol.wendu.Helper.SQLiteDBhelper; importokhttp3.OkHttpClient; importokhttp3.Request; importokhttp3.Response; publicclassMainActivityextendsAppCompatActivity{ privateSQLiteDBhelperdBhelper=newSQLiteDBhelper(GetContext.getContext(),"DATA",null,1); privateSQLiteDatabasedb=dBhelper.getReadableDatabase(); privateWebViewwebView; privateHandlerhandler; @SuppressLint({"SetJavaScriptEnabled","HandlerLeak"}) @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化数据库 newThread(newRunnable(){ @Override publicvoidrun(){ Timertimer=newTimer(); timer.schedule(newTimerTask(){ @Override publicvoidrun(){ getWenDuData(); Messagemsg=newMessage(); msg.what=1; handler.sendMessage(msg); } },0,60000); } }).start(); webView=findViewById(R.id.echartsView); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl("file:///android_asset/index.html"); handler=newHandler(){ @Override publicvoidhandleMessage(@NonNullMessagemsg){ super.handleMessage(msg); Log.d("标签","handleMessage:"+msg.what); webView.loadUrl("javascript:updateData('"+getNewData()+"')"); } }; try{ Thread.sleep(2000); }catch(InterruptedExceptione){ e.printStackTrace(); } } publicStringgetNewData(){ dBhelper.getWritableDatabase(); StringwenduJson="["; StringtimeJson="["; Cursora=db.query("WenDu",null,null,null,null,null,"IDdesc","5"); if(a.moveToFirst()){ do{ Stringtime=a.getString(a.getColumnIndex("time")); Stringwendu=a.getString(a.getColumnIndex("wendu")); wenduJson+="""+wendu+"","; timeJson+="""+time+"","; }while(a.moveToNext()); } timeJson=timeJson.substring(0,timeJson.length()-1)+']'; wenduJson=wenduJson.substring(0,wenduJson.length()-1)+']'; Stringresult="{"data":"+timeJson+","wendu":"+wenduJson+"}"; returnresult; } publicvoidgetWenDuData(){ OkHttpClientokHttpClient=newOkHttpClient(); Requestrequest=newRequest.Builder().url("https://api.565.ink/json").get().build(); try{ Responseresponse=okHttpClient.newCall(request).execute(); Stringres=response.body().string(); JsonObjecta=JsonParser.parseString(res).getAsJsonObject(); Stringtime=a.get("time").getAsString(); Stringwendu=a.get("wendu").getAsString(); Cursorcursor=db.query("WenDu",null,"timelike'%"+time+"%'",null,null,null,null); if(cursor.moveToFirst()){ Log.d("已存在",time); }else{ ContentValuesvalues=newContentValues(); values.put("time",time); values.put("wendu",wendu); db.insert("WenDu",null,values); Log.d("不存在",time); } Log.d("结果",time+","+wendu); }catch(IOExceptione){ e.printStackTrace(); } } }
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:ignore="WebViewLayout"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="温度"/> <WebView android:id="@+id/echartsView" android:layout_width="match_parent" android:layout_height="400dp"/> </LinearLayout> <com.google.android.material.navigation.NavigationView android:id="@+id/navigationView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start"/> </androidx.drawerlayout.widget.DrawerLayout>
AndroidMainifest.xml
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="cn.lanol.wendu"> <uses-permissionandroid:name="android.permission.INTERNET"/> <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:name=".Helper.GetContext" tools:ignore="GoogleAppIndexingWarning"> <activityandroid:name=".MainActivity"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
SQLiteDBHelper.java
packagecn.lanol.wendu.Helper; importandroid.content.Context; importandroid.database.sqlite.SQLiteDatabase; importandroid.database.sqlite.SQLiteOpenHelper; importandroid.widget.Toast; importandroidx.annotation.Nullable; publicclassSQLiteDBhelperextendsSQLiteOpenHelper{ privateContextmcontext; privatestaticfinalStringCREATE_WENDU="createtableWenDu(idintegerprimarykeyautoincrement,timetext,wenduinteger)"; publicSQLiteDBhelper(@NullableContextcontext,@NullableStringname,@NullableSQLiteDatabase.CursorFactoryfactory,intversion){ super(context,name,factory,version); mcontext=context; } @Override publicvoidonCreate(SQLiteDatabasedb){ db.execSQL(CREATE_WENDU); Toast.makeText(GetContext.getContext(),"欢迎使用温度实时监控系统",Toast.LENGTH_SHORT).show(); } @Override publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){ } }
GetContext.java
packagecn.lanol.wendu.Helper; importandroid.app.Application; importandroid.content.Context; publicclassGetContextextendsApplication{ privatestaticContextcontext; @Override publicvoidonCreate(){ super.onCreate(); context=getApplicationContext(); } publicstaticContextgetContext(){ returncontext; } }
目录结构
猜你还喜欢
- 03-29 [编程相关] Winform窗体圆角以及描边完美解决方案
- 03-29 [前端问题] has been blocked by CORS policy跨域问题解决
- 03-29 [编程相关] GitHub Actions 入门教程
- 03-29 [编程探讨] CSS Grid 网格布局教程
- 10-12 [编程相关] python实现文件夹所有文件编码从GBK转为UTF8
- 10-11 [编程算法] opencv之霍夫变换:圆
- 10-11 [编程算法] OpenCV Camshift算法+目标跟踪源码
- 10-11 [Python] python 创建 Telnet 客户端
- 10-11 [编程相关] Python 基于 Yolov8 + CPU 实现物体检测
- 03-15 [脚本工具] 使用go语言开发自动化脚本 - 一键定场、抢购、预约、捡漏
- 01-08 [编程技术] 秒杀面试官系列 - Redis zset底层是怎么实现的
- 01-05 [编程技术] 《Redis设计与实现》pdf
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[组网工具] 【开源】分享一款易用、功能丰富、无需服务端的异地组网软件
[系统辅助] 适合封装系统设置记事本右键菜单的小工具v1.0
[电影] 2024年国产大陆电视剧《少年巴比伦》连载至04
[电影] 2014年韩国经典剧情片《国际市场》蓝光韩语中字
[电影] 2022年国产喜剧片《村里来了个洋媳妇》HD国语中字
[游戏娱乐] 《潜渊症》v1.4.6.0联机版
[游戏娱乐] 《欧陆风云4》v1.37.0.0全DLC
[电影] 2024年国产犯罪片《89分之一的凶手》HD国语中字
[电影] 2023年国产动作片《龙门飞雪:寻龙教》HD国语中字
[文件传输] 双轨快传 v1.1.1 (同时使用USB和WIFI传输文件到电脑)
[资料] [大学期末救急课] 猴博士+高斯课堂+斐多课堂,全集视频合集
[云资源] 价值2万元的老男孩Python教程
[书库] 史上最全摄影书推荐(附700本PDF版打包下载)
[云资源] 花了一千多元买的私人健身教程
[下载工具] Internet Download Manager 6.42.7 (IDM)
[影视] 灌篮高手 WEB-DL版下载/Slam Dunk/スラムダンク/灌篮高手:THE FIRST/灌篮高手电影版 2022 The First Slam Dunk 61.35G
[资料] 3000 套电影电视剧 LOGO 宣传片常用音效合集包
[安卓软件] 酷我音乐APP_v10.7.6.4 去广告破解豪华VIP版
[即时通讯] 微信PC版WeChat 3.9.9.43 多开防撤回绿色版
[安卓软件] Solid Explorer文件管理器APP 2.8.38 破解版
[云资源] 价值2万元的老男孩Python教程
[影视] 灌篮高手 WEB-DL版下载/Slam Dunk/スラムダンク/灌篮高手:THE FIRST/灌篮高手电影版 2022 The First Slam Dunk 61.35G
[云资源] 花了一千多元买的私人健身教程
[书库] 史上最全摄影书推荐(附700本PDF版打包下载)
[动画] 北斗神拳(1984) [两季合集] [MKV]
[资料] 抗战阵亡将士资料+续编
[电视剧] 三体 (2024) 全8集 网飞版本 中文字幕 合集
[纪录片] 河西走廊【10集 国语 中文字幕 1080P 10.8G MP4】
[电影] 2024年喜剧片·热辣滚烫 [mp4]
[影视] 铁爪 WEB-DL版下载 2023 The Iron Claw 23.48G
- 最新评论
-
我想看看mw2ddyy 评论于:04-26 好东西阿zfy123123 评论于:04-18 谢谢楼主xiaoqi 评论于:04-12 勿在线解压,勿手机解压,请在电脑上用最新款压缩软件解压!推荐360压缩或者好压CC下载站 评论于:04-10 无法解压啊,客服能不能给个解压教程ravengrey 评论于:04-10 谢谢支持!!CC下载站 评论于:03-26 很棒的资源,感谢分享云体风身 评论于:03-26 感谢分享,好东西云体风身 评论于:03-26 谢谢支持!CC下载站 评论于:03-14 央视精品,感谢付出提供。qwer9009 评论于:03-14
- 热门tag