Upgrade to Pro — share decks privately, control downloads, hide ads and more …

11_组件-通信连接

powerhope
August 11, 2016
150

 11_组件-通信连接

App Inventor移动应用开发

powerhope

August 11, 2016
Tweet

Transcript

  1. College of Mathematics and Computer Science, Hunan Normal University 

    该组件可以使用启动活动对象(StartActivity)方法启 动一个活动(即调用其它app)。可以被启动的活动包括 :  启动由App Inventor创建的其它应用。首先要通过下 载其它应用程序的源代码弄清楚类名(class),然后 使用文件资源管理器或解压缩程序解压源文件(如把源 文件的扩展名修改成.zip或.rar,然后用winrar解压) ,在解压后的文件夹下找到 “youngandroidproject/project.properties”,打开 “project.properties”文件的第一行是“main=”,紧接着 是类名 Activity启动器ActivityStarter
  2. College of Mathematics and Computer Science, Hunan Normal University 

    下面以HelloPurr为例,project.properties文件内容为: main=appinventor.ai_powerhope.HelloPurr.Screen1 name=HelloPurr assets=../assets source=../src build=../build versioncode=1 versionname=1.0 useslocation=False Activity启动器ActivityStarter
  3. College of Mathematics and Computer Science, Hunan Normal University 

    为了让你的启动活动对象(StartActivity)方法启 动这个应用程序,设置以下属性: ◦ 活动包(ActivityPackage):“main”后面去掉最后一个 组件,如“appinventor.ai_powerhope.HelloPurr”。 ◦ 活动类(ActivityClass):整个类名,如 “appinventor.ai_powerhope.HelloPurr.Screen1”。 Activity启动器ActivityStarter
  4. College of Mathematics and Computer Science, Hunan Normal University 

    启动摄像头应用。属性设置如下: ◦ 动作【Action】:android.intent.action.MAIN ◦ 活动包【ActivityPackage】:com.android.camera ◦ 活动类【ActivityClass】: com.android.camera.Camera Activity启动器ActivityStarter
  5. College of Mathematics and Computer Science, Hunan Normal University 

    执行Web搜索。假设你想搜索“吸血鬼”(替换成你要 搜索的关键词),属性设置如下: ◦ Action:android.intent.action.WEB_SEARCH ◦ ExtraKey:query ◦ ExtraValue:吸血鬼 ◦ ActivityPackage: com.google.android.providers.enhancedgooglesear ch ◦ ActivityClass: com.google.android.providers.enhancedgooglesear ch.Launcher Activity启动器ActivityStarter
  6. College of Mathematics and Computer Science, Hunan Normal University 

    打开浏览器到指定的web页面。假设你想打开 “www.baidu.com”(或其他网站),属性设置如下: ◦ Action:android.intent.action.VIEW ◦ DataUri:http://www.baidu.com Activity启动器ActivityStarter
  7. College of Mathematics and Computer Science, Hunan Normal University Activity启动器ActivityStarter

     Action:动作,将启动的activity动作,即 Android中Intent。如 android.intent.action.MAIN  ActivityClass:活动类,将启动的activity类名。 如 appinventor.ai_powerhope.HelloPurr.Screen1  ActivityPackage:活动包,将启动的activity包名 。如appinventor.ai_powerhope.HelloPurr
  8. College of Mathematics and Computer Science, Hunan Normal University Activity启动器ActivityStarter

     DataType:数据类型  DataUri:URI传到将启动的activity。  ExtraKey:文本键名传递给activity。  ExtraValue:文本值传递给activity。  Extras:接受一个用作键/值对的列表,传递给 activity。  Result:被启动的activity的返回值。  ResultName:用来从被启动的activity提取结果的 名称。
  9. College of Mathematics and Computer Science, Hunan Normal University Activity启动器ActivityStarter

     ResultType:从被启动的activity返回的类型信息 。  ResultUri:从被启动的activity返回的URI(或数 据)信息。
  10. College of Mathematics and Computer Science, Hunan Normal University 

    活动调用取消(ActivityCanceled):因为活动( Activity)被取消, ActivityStarter返回时触发事 件。  活动调用结束(AfterActivity):在 ActivityStarter返回后,触发该事件。 Activity启动器ActivityStarter
  11. College of Mathematics and Computer Science, Hunan Normal University 

    处理活动信息(ResolveActivity):返回与 ActivityStarter对应的活动(Activity)的名称,如 果找不到相应的活动,则返回空字符串。  启动活动对象(StartActivity):启动 ActivityStarter对应的活动(Activity)。 Activity启动器ActivityStarter
  12. College of Mathematics and Computer Science, Hunan Normal University 

    例:实现播放器,用ES文件浏览器进行选择文件。  下载ES文件浏览器 ◦ http://www.estrongs.com/  安装到Android手机 Activity启动器ActivityStarter
  13. College of Mathematics and Computer Science, Hunan Normal University 

    Web客户端是非可视组件,提供了HTTP的GET、 POST、PUT和DELETE请求。 Web客户端
  14. College of Mathematics and Computer Science, Hunan Normal University 

    允许使用cookies(AllowCookies):是否允许来 自响应的cookies在设备上保存,并在后续的请求 中使用。Cookies只支持Android2.3或更高版本。  请求头(RequestHeaders):请求头是一个列表 ,列表元素为包含两个元素的子列表。每个子列表 的第一个元素为请求头字段名。每个子列表的第二 个元素为请求头字段值,它可能是单个值或包含多 个值的列表。 Web客户端属性
  15. College of Mathematics and Computer Science, Hunan Normal University 

    响应文件名称(ResponseFileName):响应的文 件被保存的文件名。如果保存响应信息( SaveResponse)的属性值为true和响应文件名称 (ResponseFileName)的属性值为空,则将生成 新的文件名。  保存响应信息(SaveResponse):是否将响应保 存为文件。  网址(Url):Web请求的URL地址。 Web客户端属性
  16. College of Mathematics and Computer Science, Hunan Normal University 

    获得文件(GotFile):事件表明请求已经完成。  获得文本(GotText):事件表明请求已经完成。  返回参数说明:  响应代码:是用以表示网页服务器HTTP响应状态 的3位数字代码。它由 RFC 2616 规范定义的,并 得到RFC 2518、RFC 2817、RFC 2295、RFC 2774、RFC 4918等规范扩展。 Web客户端事件
  17. College of Mathematics and Computer Science, Hunan Normal University 

    HTTP协议状态码表示的意思主要分为五类:  1×× 保留  2×× 表示请求成功地接收  3×× 为完成请求客户需进一步细化请求  4×× 客户错误  5×× 服务器错误  如200表示请求已成功,请求所希望的响应头或数据体 将随此响应返回。其它详细的状态码可以网上查询。  响应类型:是服务器响应的 HTTP 内容类型。如 text/html,image/GIF等。 Web客户端事件
  18. College of Mathematics and Computer Science, Hunan Normal University 

    解码HTML文本(HtmlTextDecode):对给定的 HTML文本值进行解码。HTML字符实体,如 &amp;, &lt;, &gt;, &apos;&quot;经解码后将变为 &, <, >, ',"。而像&#xhhhh ,&#nnnn这样的实 体将被转换为适当的字符。  解码 Json文本(JsonTextDecode):解码给定的 JSON编码的值,生成相应的App Inventor值。一 个JSON列表[x,y,z]经解码后转换为列表(x,y,z),一 个名称为A、值为B的JSON对象(表示为{A:B})经解 码后变为列表((A B)),即一个列表的每行包含两个 元素的子列表(A B)。 Web客户端方法
  19. College of Mathematics and Computer Science, Hunan Normal University 

    如何通过Web客户端在线获取股票的价格?这里需要借助 于其它提供实时获取股票行情的API,我们使用新浪股票 API。  首先,分析一下新浪股票API接口的特征,新浪股票接口 数据地址为“http://hq.sinajs.cn/list=”,等号后面接 证券上市交易所标识和股票代码,如果股票是在上海证券 交易所上市,则为“sh600031”(以三一重工为例), 完整的接口数据地址就是 “http://hq.sinajs.cn/list=sh600031”;如果股票是在 深圳证券交易所上市,则为“sz000858”(以五粮液为 例),完整的接口数据地址就是 “http://hq.sinajs.cn/list=sz000858”。 股票在线价格查询
  20. College of Mathematics and Computer Science, Hunan Normal University 

    以三一重工(股票代码:600031)为例,如果要获取它 的最新行情,只需访问新浪的股票数据接口: http://hq.sinajs.cn/list=sh600031  这个url会返回一串文本,例如:  var hq_str_sh600031="三一重工 ,9.65,9.53,9.90,9.92,9.55,9.90,9.91,167635517,1 628770330,460000,9.90,197766,9.89,372000,9.8 8,244939,9.87,211700,9.86,40500,9.91,278500,9 .92,237611,9.93,102400,9.94,639600,9.95,2015- 05-08,15:03:06,00";  这个字符串由许多数据拼接在一起,不同含义的数据用逗 号隔开了,按照程序员的思路,顺序号从1开始。 股票在线价格查询
  21. College of Mathematics and Computer Science, Hunan Normal University 

    1:”三一重工”,股票名字;  2:”9.65″,今日开盘价;  3:”9.53″,昨日收盘价;  4:”9.90″,当前价格;  5:”9.92″,今日最高价;  6:”9.55″,今日最低价;  7:”9.90″,竞买价,即“买一”报价;  8:”9.91″,竞卖价,即“卖一”报价;  9:”167635517″,成交的股票数,由于股票交易以一百股为基本 单位,所以在使用时,通常把该值除以一百;  10:”1628770330″,成交金额,单位为“元”,为了一目了然 ,通常以“万元”为成交金额的单位,所以通常把该值  除以一万;  10:”460000″,“买一”申请460000股,即4600手;  11:”9.90″,“买一”报价;  12:”197766″,“买二”  13:”9.89″,“买二”
  22. College of Mathematics and Computer Science, Hunan Normal University 

    通过Web客户端从新浪股票接口获取股票的价格代 码 股票在线价格查询
  23. College of Mathematics and Computer Science, Hunan Normal University 

    处理GET返回的数据和查询分时图及K线图  数据返回后,在Web客户端的获得文本事件中,首 先要判断是否获取数据成功。如果“响应代码 =200”则表示获取数据成功,接下来可以对数据进 行处理,如(1)中所述GET返回的数据是一串 “,”分隔的文本,我们需要根据需要进行提取,将 其分成称列表,然后从列表中读取需要的数据。 股票在线价格查询
  24. College of Mathematics and Computer Science, Hunan Normal University 

    分时图及K线图也通过分析新浪提供的股票K线图接 口,分时图为: http://image.sinajs.cn/newchart/min/n/,K 线图为: http://image.sinajs.cn/newchart/daily/n/, “n/“后面所接内容与查询股票价格一致。返回的数 据就是一张图片,可以把URL直接传给Web浏览框 ,通过Web浏览框浏览显示。 股票在线价格查询
  25. College of Mathematics and Computer Science, Hunan Normal University 

    虽然App Inventor在很多方面的功能都受到了限制 ,但如果能很好的利用互联网上众多的API接口, 借助于Web客户端可以实现很多功能强大的App。 股票在线价格查询
  26. College of Mathematics and Computer Science, Hunan Normal University 

    App Inventor 2提供了两个蓝牙通信组件:蓝牙客 户端BluetoothClient、服务器BluetoothServer , 这两个组件配合使用,可以实现简单的即时通信功 能。  例:蓝牙聊天工具 蓝牙
  27. College of Mathematics and Computer Science, Hunan Normal University 

    定义全局变量“角色”。用来表明当前设备在通信 中的角色:服务器或客户端。 与蓝牙服务器相关的代码
  28. College of Mathematics and Computer Science, Hunan Normal University 

    当选中“复选框_在线” 时,表明本设备现在充当 服务器的角色,可以接受来自其他设备的连接请求 。一旦该设备成为服务器,“选择列表”及“断开 连接”组件将不可用。 “在线”设置
  29. College of Mathematics and Computer Science, Hunan Normal University 

    当蓝牙服务器收到连接请求时,将触发该事件。如 果连接成功,即连接状态值为真,则向所连接的设 备发送“已连接到服务器,可以开始聊天了……”, 并开始启用计时器,随时监听客户端发来的消息; 如果连接不成功,则不启用计时器。 接收连接事件
  30. College of Mathematics and Computer Science, Hunan Normal University 

    当“列表_选择蓝牙设备”准备选择,当用户点击 “列表_选择蓝牙设备”时,将打开一个列表,里 面列出了所有已配对的蓝牙设备,用户可以选择其 一进行连接。 与蓝牙客户端相关的代码
  31. College of Mathematics and Computer Science, Hunan Normal University 

    当用户选择了一个蓝牙设备,意味着用户向所选设 备发出了连接请求,“让蓝牙客户端连接设备”的 结果可能是真(连接成功),也可能是假(连接失 败)。如果成功,则设全局变量“角色”值为“客 户端”,并启动计时器;如果失败,则不启动计时 器。 当“列表_选择蓝牙设备” 选择完成
  32. College of Mathematics and Computer Science, Hunan Normal University 

    定义全局变量“聊天内容”与自定义过程“连接字 符串”:以适当的方式来呈现聊天记录,即,为聊 天内容添加换行符,并加上发言者身份。 发送消息
  33. College of Mathematics and Computer Science, Hunan Normal University 

    实验12. 帮助任课老师制作一个点名软件,可以管 理多个班级和多门课程的点名,并能进行相关统计 ,要求能从文件导入一个班同学的信息。 实验