[安卓] 安卓实时获取数据并刷新图表
作者:精品下载站 日期:2020-10-30 00:00:00 浏览:85 分类:编程开发
先来个效果图吧
然后直接上代码,解说以后有机会我再补上
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
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[短剧合集] 2025年06月2日 精选+付费短剧推荐39部
[软件合集] 25年6月2日 精选软件18个
[软件合集] 25年6月1日 精选软件15个
[短剧合集] 2025年06月1日 精选+付费短剧推荐59部
[短剧] 2025年05月31日 精选+付费短剧推荐58部
[软件合集] 25年5月31日 精选软件66个
[电影] 黄沙漫天(2025) 4K.EDRMAX.杜比全景声 / 4K杜比视界/杜比全景声
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[美图] 2W美女个美女小姐姐,饱眼福
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag