实时获取股票数据的android app应用程序源码分享
最近学习android应用开发,不知道写一个什么样的程序来练练手,正好最近股票很火,就一个app来实时获取股票数据,取名为mystock。使用开发工具android studio,需要从android官网下载,下载地址:。不幸的是android是google公司的,任何和google公司相关的在国内都无法直接访问,只能通过vpn访问。
下图为android studio打开一个工程的截图:
下面按步介绍mystock的实现步骤。
1.以下是activa_main.xml的内容。上面一排是三个textview,分别用来显示上证指数,深圳成指,创业板指。中间一排是一个edittext和一个button,用来添加股票。下面是一个table,用来显示添加的股票列表。
<linearlayout xmlns: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" android:orientation="vertical" tools:context=".mainactivity"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <linearlayout android:layout_width="0dp" android:layout_weight="0.33" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stock_sh_name"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/stock_sh_index"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="12sp" android:id="@+id/stock_sh_change"/> </linearlayout> <linearlayout android:layout_width="0dp" android:layout_weight="0.33" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stock_sz_name"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/stock_sz_index"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="12sp" android:id="@+id/stock_sz_change"/> </linearlayout> <linearlayout android:layout_width="0dp" android:layout_weight="0.33" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stock_chuang_name"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/stock_chuang_index"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="12sp" android:id="@+id/stock_chuang_change"/> </linearlayout> </linearlayout> <linearlayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="number" android:maxlength="6" android:id="@+id/edittext_stockid" android:layout_weight="1" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_add_label" android:onclick="addstock" /> </linearlayout> <!--listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listview" /--> <scrollview android:layout_width="match_parent" android:layout_height="wrap_content"> <tablelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/stock_table"></tablelayout> </scrollview> </linearlayout>
应用截图如下:
2.数据获取,这里使用sina提供的接口来实时获取股票数据,代码如下:
public void querysinastocks(string list){ // instantiate the requestqueue. requestqueue queue = volley.newrequestqueue(this); string url ="http://hq.sinajs.cn/list=" + list; //http://hq.sinajs.cn/list=sh600000,sh600536 // request a string response from the provided url. stringrequest stringrequest = new stringrequest(request.method.get, url, new response.listener<string>() { @override public void onresponse(string response) { updatestocklistview(sinaresponsetostocks(response)); } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { } }); queue.add(stringrequest); }
这里发送http请求用到了volley,需要在build.gradle里面添加dependencies:compile 'com.mcxiaoke.volley:library:1.0.19'。
3.定时刷新股票数据,使用了timer,每隔两秒发送请求获取数据,代码如下:
timer timer = new timer("refreshstocks"); timer.schedule(new timertask() { @override public void run() { refreshstocks(); } }, 0, 2000); private void refreshstocks(){ string ids = ""; for (string id : stockids_){ ids += id; ids += ","; } querysinastocks(ids); }
4.在程序退出时存储股票代码,下次打开app时,可以显示上次的股票列表。代码如下。
private void savestockstopreferences(){ string ids = ""; for (string id : stockids_){ ids += id; ids += ","; } sharedpreferences sharedpref = getpreferences(context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); editor.putstring(stockidskey_, ids); editor.commit(); } @override public void ondestroy() { super.ondestroy(); // always call the superclass savestockstopreferences(); }
5.删除选中的股票,在menu_main.xml里面添加一个action。
<menu 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" tools:context=".mainactivity"> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderincategory="100" app:showasaction="never" /> <item android:id="@+id/action_delete" android:title="@string/action_delete" android:orderincategory="100" app:showasaction="never" /> </menu>
代码响应事件并删除:
@override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. the action bar will // automatically handle clicks on the home/up button, so long // as you specify a parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } else if(id == r.id.action_delete){ if(selectedstockitems_.isempty()) return true; for (string selectedid : selectedstockitems_){ stockids_.remove(selectedid); tablelayout table = (tablelayout)findviewbyid(r.id.stock_table); int count = table.getchildcount(); for (int i = 1; i < count; i++){ tablerow row = (tablerow)table.getchildat(i); linearlayout nameid = (linearlayout)row.getchildat(0); textview idtext = (textview)nameid.getchildat(1); if(idtext != null && idtext.gettext().tostring() == selectedid){ table.removeview(row); break; } } } selectedstockitems_.clear(); } return super.onoptionsitemselected(item); }
屏幕截图:
6.当有大额委托挂单时,发送消息提醒,代码如下:
{ ... string text = ""; string sbuy = getresources().getstring(r.string.stock_buy); string ssell = getresources().getstring(r.string.stock_sell); if(double.parsedouble(stock.b1_ )>= stocklargetrade_) { text += sbuy + "1:" + stock.b1_ + ","; } if(double.parsedouble(stock.b2_ )>= stocklargetrade_) { text += sbuy + "2:" + stock.b2_ + ","; } if(double.parsedouble(stock.b3_ )>= stocklargetrade_) { text += sbuy + "3:" + stock.b3_ + ","; } if(double.parsedouble(stock.b4_ )>= stocklargetrade_) { text += sbuy + "4:" + stock.b4_ + ","; } if(double.parsedouble(stock.b5_ )>= stocklargetrade_) { text += sbuy + "5:" + stock.b5_ + ","; } if(double.parsedouble(stock.s1_ )>= stocklargetrade_) { text += ssell + "1:" + stock.s1_ + ","; } if(double.parsedouble(stock.s2_ )>= stocklargetrade_) { text += ssell + "2:" + stock.s2_ + ","; } if(double.parsedouble(stock.s3_ )>= stocklargetrade_) { text += ssell + "3:" + stock.s3_ + ","; } if(double.parsedouble(stock.s4_ )>= stocklargetrade_) { text += ssell + "4:" + stock.s4_ + ","; } if(double.parsedouble(stock.s5_ )>= stocklargetrade_) { text += ssell + "5:" + stock.s5_ + ","; } if(text.length() > 0) sendnotifation(integer.parseint(sid), stock.name_, text); ... } public void sendnotifation(int id, string title, string text){ notificationcompat.builder nbuilder = new notificationcompat.builder(this); nbuilder.setsmallicon(r.drawable.ic_launcher); nbuilder.setcontenttitle(title); nbuilder.setcontenttext(text); nbuilder.setvibrate(new long[]{100, 100, 100}); nbuilder.setlights(color.red, 1000, 1000); notificationmanager notifymgr = (notificationmanager) getsystemservice(notification_service); notifymgr.notify(id, nbuilder.build()); }
屏幕截图:
以上通过图文并茂的方式给大家分享了一个实时获取股票数据的android app应用程序源码,希望大家喜欢。
上一篇: Python魔术方法详解
下一篇: android之camera用法实例详解