实时获取股票数据的android app应用程序源码分享
From:http://www.jb51.net/article/72449.htm
最近学习Android应用开发,不知道写一个什么样的程序来练练手,正好最近股票很火,就一个App来实时获取股票数据,取名为Mystock。使用开发工具Android Studio,需要从Android官网下载,下载地址:http://developer.android.com/sdk/index.html。不幸的是Android是Google公司的,任何和Google公司相关的在国内都无法直接访问,只能通过VPN访问。
下图为Android Studio打开一个工程的截图:
下面按步介绍Mystock的实现步骤。
1.以下是activa_main.xml的内容。上面一排是三个TextView,分别用来显示上证指数,深圳成指,创业板指。中间一排是一个EditText和一个Button,用来添加股票。下面是一个Table,用来显示添加的股票列表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
<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提供的接口来实时获取股票数据,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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,每隔两秒发送请求获取数据,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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时,可以显示上次的股票列表。代码如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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。
1
2
3
4
5
6
7
8
|
<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>
|
代码响应事件并删除:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
@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.当有大额委托挂单时,发送消息提醒,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
{
...
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应用程序源码,希望大家喜欢。
实时获取股票数据的android app应用程序源码分享相关推荐
- 一个实时获取股票数据的安卓应用程序
关键字:Stock,股票,安卓,Android Studio. OS:Windows 10. 最近学习Android应用开发,不知道写一个什么样的程序来练练手,正好最近股票很火,就一个App来实时获取 ...
- 构建股票量化数据库一.实时获取股票数据
实时获取股票数据 (1)实现步骤 1.通过网络爬虫–>爬取所需要的数据->股票实时价,最高价,最低价等等 2.通过python的->pandas库->进行数据整理清洗 (2)实 ...
- 代驾APP小程序源码交付 所需功能大全
生活水平的提高,使得车已经是现代人生活中不可或缺的代步工具了,但有时候因为疲劳或者是应酬喝了酒没办法继续驾车,招人代驾就很有必要了,代驾APP小程序就是这样应运而生的.代驾系统开发时,不要具备哪些基本 ...
- Android实用应用程序源码
andriod闹钟源代码 http://www.apkbus.com/android-20974-1-1.html android源码分享之指南针程序 http://www.apkbus.com/an ...
- android简单记账源码,Android+个人记账程序源码.rar(入门级)
[实例简介]Android 个人记账程序源码,入门级源码,适合新手... [实例截图] [核心代码] package com.cola.ui; import java.util.Calendar; i ...
- 自适应app下载页-源码分享-趣学程序
自适应app下载页-源码分享-趣学程序 hello,大家好,我是shaofeer,今天给大家分享常用的<app下载页面>的源码,希望对大家有所帮助. 本文中涉及到的源码,获取方式在文末哦~ ...
- 计算机毕业设计ssm基于JAVA毕业生发展去向查询平台及数据统计系统6263k系统+程序+源码+lw+远程部署
计算机毕业设计ssm基于JAVA毕业生发展去向查询平台及数据统计系统6263k系统+程序+源码+lw+远程部署 计算机毕业设计ssm基于JAVA毕业生发展去向查询平台及数据统计系统6263k系统+程序 ...
- 实时获取股票数据,免费!——Python爬虫Sina Stock实战
数量技术宅团队在CSDN学院推出了量化投资系列课程 欢迎有兴趣系统学习量化投资的同学,点击下方链接报名: 量化投资速成营(入门课程) Python股票量化投资 Python期货量化投资 Pyth ...
- 基于HTML开发外观漂亮大气的APP下载页源码分享
说明: 一款模仿袋鼠APP的下载页面源码,非常简单的纯html网站APP下载页源码. 下载地址:菜鸟站长之家
最新文章
- ctf-cybrics
- 【数据结构与算法】之深入解析“二叉树的层序遍历II”的求解思路与算法示例
- 获取本地IP和端口号的指令
- baidumap vue 判断范围_vue百度地图 + 定位的详解
- Go语言-defer的使用
- Win2008 R2 VDI动手实验系列之二:远程桌面虚拟化主机配置
- layuit 框架_Layui|经典模块化前端框架
- 详细解读用C语言编写的 “扫雷”程序
- 使用5502自带的UART口发送数据乱码的问题
- 2008服务器系统显卡,Windows2008 R2 开启显卡硬件加速
- js-函数式编程-柯里化和语义化
- 2021年中国棘轮手柄市场趋势报告、技术动态创新及2027年市场预测
- epub格式电子书剖析之一:文档构成
- 很多人遇到问题:win10锁屏唤醒后程序全部关闭
- 思科CCNA认证课程内容
- 软考中级【数据库系统工程师】第1章:计算机系统知识,自学软考笔记,备考2022年5月份软考,计算机硬件系统CPU组成指令寄存器组总线输入输出的程序控制方式计算机体系结构与存储系统加密技术流水线技术
- 网页自动填表html,WebBrowser1.HtmlInput 实现浏览器文本自动填写与点击
- myeclipse新建项目部署到tomcat中,点击finish键没反应
- web前端能做到多少岁
- 电化学传感器使用-电子学角度分析