1. 1.smartimageview使用
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity" >
  8.  
  9. <com.loopj.android.image.SmartImageView
  10. android:id="@+id/iv_beauty"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:layout_weight="100" />
  14.  
  15. <EditText
  16. android:id="@+id/et_path"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:singleLine="true"
  20. android:text="http://a.hiphotos.baidu.com/album/w%3D2048/sign=0a938b00d53f8794d3ff4f2ee6230cf4/faedab64034f78f06fe0f24b78310a55b2191c9a.jpg" />
  21.  
  22. <Button
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:onClick="watch"
  26. android:text="浏览" />
  27.  
  28. </LinearLayout>
  29.  
  30. public class MainActivity extends Activity {
  31.  
  32. private EditText et_path;
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_main);
  37. this.et_path = (EditText) this.findViewById(R.id.et_path);
  38. }
  39.  
  40. public void watch(View view) {
  41. String url = this.et_path.getText().toString().trim();
  42. SmartImageView siv = (SmartImageView) this.findViewById(R.id.iv_beauty);
  43. siv.setImageUrl(url, R.drawable.ic_launcher, R.drawable.ic_launcher);
  44. }
  45. }
  46.  
  47. <uses-permission android:name="android.permission.INTERNET"/>
  48.  
  49. 2.android多线程下载
  50. public class MainActivity extends Activity {
  51.  
  52. protected static final int SERVER_ERROR = 1;
  53. protected static final int DOWN_LOAD_ERROR = 2;
  54. protected static final int DOWN_LOAD_SUCCESS = 3;
  55. protected static final int UPDATE_TEXT = 4;
  56.  
  57. private EditText et_path;
  58. private ProgressBar pb_download;
  59. private TextView tv_process;
  60.  
  61. private static int threadCount = 3;
  62. private static int runningThread = threadCount;
  63. private static int currentProgress = 0;
  64.  
  65. private static String resource = "/mnt/sdcard/oracle.pptx";
  66. private static String path = "http://110.65.99.66:8080/oracle.pptx";
  67. private static String mode = "rwd";
  68.  
  69. private Handler handler = new Handler() {
  70.  
  71. @Override
  72. public void handleMessage(Message msg) {
  73. switch (msg.what) {
  74. case SERVER_ERROR:
  75. Toast.makeText(MainActivity.this,
  76. "服务器错误,错误码:" + (String) msg.obj, 0).show();
  77. break;
  78.  
  79. case DOWN_LOAD_ERROR:
  80. Toast.makeText(MainActivity.this, "下载失败", 0).show();
  81. break;
  82. case DOWN_LOAD_SUCCESS:
  83. Toast.makeText(MainActivity.this, "文件下载完毕,临时文件被删除!", 0).show();
  84. break;
  85. case UPDATE_TEXT:
  86. tv_process.setText("当前进度:" + pb_download.getProgress() * 100
  87. / pb_download.getMax());
  88. break;
  89. }
  90. }
  91.  
  92. };
  93.  
  94. @Override
  95. protected void onCreate(Bundle savedInstanceState) {
  96. super.onCreate(savedInstanceState);
  97. setContentView(R.layout.activity_main);
  98. this.et_path = (EditText) this.findViewById(R.id.et_path);
  99. this.pb_download = (ProgressBar) this.findViewById(R.id.pb_download);
  100. this.tv_process = (TextView) this.findViewById(R.id.tv_process);
  101. }
  102.  
  103. public void download(View view) {
  104. new Thread() {
  105.  
  106. public void run() {
  107. try {
  108. // 连接服务器获取一个文件,在本地创建一个和它相同大小的临时文件
  109. URL url = new URL(path);
  110. HttpURLConnection conn = (HttpURLConnection) url
  111. .openConnection();
  112. conn.setRequestMethod("GET");
  113. conn.setConnectTimeout(5000);
  114. int code = conn.getResponseCode();
  115. if (code == 200) {
  116. // 服务器返回的数据长度,实际上就是文件的长度
  117. int length = conn.getContentLength();
  118. System.out.println("文件的长度 :" + length);
  119.  
  120. // 设置进度条的最大值
  121. pb_download.setMax(length - 1);
  122.  
  123. // 在本地创建一个大小跟服务器文件一样大小的临时文件
  124. RandomAccessFile raf = new RandomAccessFile(resource,
  125. mode);
  126. // 指定文件的长度
  127. raf.setLength(length);
  128. raf.close();
  129.  
  130. // 假设是3个线程去下载资源
  131. // 计算平均每个线程下载的文件大小
  132. int blockSize = length / threadCount;
  133. // 标记正在运行子线程的个数
  134. runningThread = threadCount;
  135. // 下载进度归零
  136. currentProgress = 0;
  137. // 下载进度条归零
  138. pb_download.setProgress(0);
  139. System.out.println("blockSize = " + blockSize);
  140. for (int threadId = 1; threadId <= threadCount; threadId++) {
  141. // 线程的开始和结束位置
  142. int startIndex = (threadId - 1) * blockSize;
  143. int endIndex = threadId * blockSize - 1;
  144. if (threadId == threadCount) {
  145. endIndex = length - 1;
  146. }
  147. System.out.println("线程:" + threadId + "下载:"
  148. + startIndex + "--->" + endIndex);
  149. new DownLoadThread(threadId, startIndex, endIndex,
  150. path).start();
  151. }
  152. } else {
  153. Message msg = new Message();
  154. msg.what = SERVER_ERROR;
  155. msg.obj = code + "";
  156. handler.sendMessage(msg);
  157. }
  158.  
  159. } catch (Exception e) {
  160. e.printStackTrace();
  161. Message msg = new Message();
  162. msg.what = DOWN_LOAD_ERROR;
  163. handler.sendMessage(msg);
  164. }
  165.  
  166. };
  167. }.start();
  168. }
  169.  
  170. public class DownLoadThread extends Thread {
  171.  
  172. private int threadId;
  173. private int startIndex;
  174. private int endIndex;
  175. private String path;
  176. private String tempFileUrl;
  177.  
  178. public DownLoadThread(int threadId, int startIndex, int endIndex,
  179. String path) {
  180. this.threadId = threadId;
  181. this.startIndex = startIndex;
  182. this.endIndex = endIndex;
  183. this.path = path;
  184. tempFileUrl = "/mnt/sdcard/" + threadId + ".txt";
  185. }
  186.  
  187. @Override
  188. public void run() {
  189. try {
  190.  
  191. // 检查是否存在记录下载长度的文件,如果存在读取这个文件的数据
  192. File tempFile = new File(tempFileUrl);
  193. if (tempFile.exists() && tempFile.length() > 0) {
  194. FileInputStream fis = new FileInputStream(tempFile);
  195. byte[] buffer = new byte[1024];
  196. int length = fis.read(buffer);
  197. String downloadLength = new String(buffer, 0, length);
  198. int downloadLenInt = Integer.parseInt(downloadLength);
  199.  
  200. int alreayDownloadInt = downloadLenInt - startIndex;
  201. currentProgress += alreayDownloadInt;// 计算上次断点下载到的进度
  202.  
  203. // 修改下载的真实开始位置
  204. startIndex = downloadLenInt;
  205. fis.close();
  206. }
  207.  
  208. System.out.println("线程真正开始的位置 :" + threadId + "下载:"
  209. + startIndex + "--->" + endIndex);
  210.  
  211. URL url = new URL(path);
  212. HttpURLConnection conn = (HttpURLConnection) url
  213. .openConnection();
  214. conn.setRequestMethod("GET");
  215. conn.setConnectTimeout(5000);
  216. // 请求服务器下载部分文件,指定文件的位置
  217. conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
  218. + endIndex);
  219. conn.setConnectTimeout(60000);
  220.  
  221. // 200表示从服务器请求全部资源,206表示从服务器请求部分资源
  222. int code = conn.getResponseCode();
  223. if (code == 206) {
  224. // 已经设置了请求的位置,返回的是当前文件位置对应的文件的输入流
  225. InputStream is = conn.getInputStream();
  226. // 此类的实例支持对随机访问文件的读取和写入
  227. RandomAccessFile raf = new RandomAccessFile(resource, mode);
  228. // 定位文件
  229. raf.seek(startIndex);
  230.  
  231. int length = 0;
  232. byte[] buffer = new byte[1024];
  233. int total = 0;// 实时记录已经下载的长度
  234. // 一阵狂读
  235. while ((length = is.read(buffer)) != -1) {
  236.  
  237. RandomAccessFile file = new RandomAccessFile(
  238. tempFileUrl, mode);
  239.  
  240. raf.write(buffer, 0, length);
  241. total += length;
  242.  
  243. // 记录当前线程下载的数据位置
  244. file.write((total + startIndex - 1 + "").getBytes());
  245. file.close();
  246.  
  247. synchronized (MainActivity.this) {
  248. // 获取所有线程下载的总进度
  249. currentProgress += length;
  250. // 更改界面上progressbar的进度(progressbar和progressdialog可以在子线程里修改UI)
  251. pb_download.setProgress(currentProgress);
  252.  
  253. Message msg = Message.obtain();
  254. msg.what = UPDATE_TEXT;
  255. handler.sendMessage(msg);
  256. }
  257. }
  258.  
  259. raf.close();
  260. is.close();
  261.  
  262. System.out.println("线程 : " + threadId + "下载完毕。。。。。");
  263. runningThread--;
  264. } else {
  265. System.out.println("线程 : " + threadId + "下载失败。。。。。");
  266. }
  267. } catch (Exception e) {
  268. e.printStackTrace();
  269. System.out.println("线程 : " + threadId + " 抛出异常!");
  270. runningThread = threadCount;
  271. } finally {
  272. System.out.println("runningThread = " + runningThread);
  273. if (runningThread == 0) {
  274. for (int i = 1; i <= threadCount; i++) {
  275. File deleteFile = new File("/mnt/sdcard/" + i + ".txt");
  276. deleteFile.delete();
  277. }
  278.  
  279. Message msg = new Message();
  280. msg.what = DOWN_LOAD_SUCCESS;
  281. handler.sendMessage(msg);
  282.  
  283. }
  284.  
  285. }
  286. }
  287. }
  288.  
  289. }
  290.  
  291. 3.显式意图激活另外一个activity,检查网络是否可用定位到网络的位置
  292. <intent-filter>
  293. <action android:name="android.intent.action.MAIN" />
  294. <category android:name="android.intent.category.LAUNCHER" />
  295. </intent-filter>
  296. 这段代码的作用是告诉系统在桌面创建一个快捷图标
  297.  
  298. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  299. xmlns:tools="http://schemas.android.com/tools"
  300. android:layout_width="match_parent"
  301. android:layout_height="match_parent"
  302. android:paddingBottom="@dimen/activity_vertical_margin"
  303. android:paddingLeft="@dimen/activity_horizontal_margin"
  304. android:paddingRight="@dimen/activity_horizontal_margin"
  305. android:paddingTop="@dimen/activity_vertical_margin"
  306. tools:context=".MainActivity" >
  307.  
  308. <Button
  309. android:id="@+id/button1"
  310. android:layout_width="wrap_content"
  311. android:layout_height="wrap_content"
  312. android:layout_alignParentLeft="true"
  313. android:layout_alignParentTop="true"
  314. android:onClick="click1"
  315. android:text="跳转到第二个界面1" />
  316.  
  317. <Button
  318. android:id="@+id/button2"
  319. android:layout_width="wrap_content"
  320. android:layout_height="wrap_content"
  321. android:layout_alignRight="@+id/button1"
  322. android:layout_below="@+id/button1"
  323. android:layout_marginTop="24dp"
  324. android:onClick="click2"
  325. android:text="跳转到第二个界面2" />
  326.  
  327. <Button
  328. android:id="@+id/button3"
  329. android:layout_width="wrap_content"
  330. android:layout_height="wrap_content"
  331. android:layout_alignLeft="@+id/button2"
  332. android:layout_below="@+id/button2"
  333. android:layout_marginLeft="44dp"
  334. android:layout_marginTop="27dp"
  335. android:onClick="click3"
  336. android:text="检查手机网络状态" />
  337.  
  338. <Button
  339. android:id="@+id/button4"
  340. android:layout_width="wrap_content"
  341. android:layout_height="wrap_content"
  342. android:layout_alignLeft="@+id/button2"
  343. android:layout_below="@+id/button3"
  344. android:layout_marginLeft="16dp"
  345. android:layout_marginTop="37dp"
  346. android:onClick="click4"
  347. android:text="跳转到图库" />
  348.  
  349. </RelativeLayout>
  350.  
  351. public class MainActivity extends Activity {
  352.  
  353. @Override
  354. protected void onCreate(Bundle savedInstanceState) {
  355. super.onCreate(savedInstanceState);
  356. setContentView(R.layout.activity_main);
  357. }
  358.  
  359. public void click1(View view) {
  360. Intent intent = new Intent();
  361. intent.setClassName(this, "com.itheima.explicitintent.SecondActivity");
  362. this.startActivity(intent);
  363. }
  364.  
  365. public void click2(View view) {
  366. Intent intent = new Intent(this, SecondActivity.class);
  367. this.startActivity(intent);
  368. }
  369.  
  370. public void click3(View view) {
  371. ConnectivityManager manager = (ConnectivityManager) this
  372. .getSystemService(Context.CONNECTIVITY_SERVICE);
  373. NetworkInfo info = manager.getActiveNetworkInfo();
  374. if (info != null && info.isAvailable()) {
  375. Toast.makeText(this, "网络可用", 0).show();
  376. } else {
  377.  
  378. Toast.makeText(this, "网络不可用", 0).show();
  379. Intent intent = new Intent();
  380. intent.setClassName("com.android.phone",
  381. "com.android.phone.MiuiSettings");
  382. this.startActivity(intent);
  383.  
  384. }
  385. }
  386.  
  387. public void click4(View view) {
  388. Intent intent = new Intent();
  389. intent.setClassName("com.miui.gallery", "com.miui.gallery.app.Gallery");
  390. this.startActivity(intent);
  391. }
  392.  
  393. }
  394.  
  395. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  396.  
  397. 4.隐式意图激活另外一个activity,即打开另外一个appactivity
  398. public void click(View view) {
  399. Intent intent = new Intent();
  400. intent.setAction(Intent.ACTION_VIEW);
  401. intent.setData(Uri.parse("http://www.baidu.com"));
  402. this.startActivity(intent);
  403. }
  404.  
  405. public void send(View view) {
  406. Intent intent = new Intent();
  407. intent.setAction(Intent.ACTION_SENDTO);
  408. intent.setData(Uri.parse("sms:10086"));
  409. intent.addCategory("android.intent.category.DEFAULT");
  410. this.startActivity(intent);
  411. }
  412.  
  413. .隐式意图的配置,自定义隐式意图
  414. 第一个activity启动第二个activity
  415. 第一个Activity
  416. public class MainActivity extends Activity {
  417.  
  418. @Override
  419. protected void onCreate(Bundle savedInstanceState) {
  420. super.onCreate(savedInstanceState);
  421. setContentView(R.layout.activity_main);
  422. }
  423.  
  424. public void click(View view) {
  425. Intent intent = new Intent();
  426. intent.setAction("com.itheima.secondActivity");
  427.  
  428. // 额外信息,提供一些执行的环境参数
  429. intent.addCategory("android.intent.category.DEFAULT");
  430.  
  431. intent.setDataAndType(Uri.parse("itheima:jerry"),
  432. "vnd.android.cursor.item/mp3");
  433.  
  434. this.startActivity(intent);
  435. }
  436. }
  437. 第二个Activity
  438. public class SecondActivity extends Activity {
  439.  
  440. @Override
  441. protected void onCreate(Bundle savedInstanceState) {
  442. super.onCreate(savedInstanceState);
  443. setContentView(R.layout.activity_second);
  444. Intent intent = this.getIntent();
  445. Uri uri = intent.getData();
  446. String data = uri.toString();
  447.  
  448. String type = intent.getType();
  449.  
  450. Toast.makeText(this, "data = " + data + " , type = " + type, 0).show();
  451.  
  452. }
  453.  
  454. }
  455. <activity
  456. android:name=".SecondActivity"
  457. android:label="@string/app_name" >
  458. <intent-filter>
  459. <action android:name="com.itheima.secondActivity"/>
  460. <category android:name="android.intent.category.DEFAULT"/>
  461. <data android:scheme="itheima" android:mimeType="vnd.android.cursor.item/mp3"/>
  462. </intent-filter>
  463. </activity>
  464.  
  465. .隐式意图和显示意图的使用场景
  466. 同一个应用程序里面,自己激活自己的东西,推荐使用显示意图,效率高
  467. 不同的应用程序里面,激活别人的应用,或者是让自己的某一个界面希望被别人激活,推荐使用隐式意图。
  468.  
  469. 7.在不同activity之间数据传递
  470. public void click(View view) {
  471. String name = this.et_name.getText().toString().trim();
  472. if (TextUtils.isEmpty(name)) {
  473. Toast.makeText(this, "姓名不能为空", 0).show();
  474. return;
  475. }
  476. Intent intent = new Intent(this, ResultActivity.class);
  477. intent.putExtra("name", name);
  478. this.startActivity(intent);
  479. }
  480. 接收数据
  481. protected void onCreate(Bundle savedInstanceState) {
  482. super.onCreate(savedInstanceState);
  483. setContentView(R.layout.activity_result);
  484. TextView tv_result = (TextView) this.findViewById(R.id.tv_result);
  485. Intent intent = this.getIntent();
  486. String name = intent.getStringExtra("name");
  487. Random random = new Random();
  488. int rp = random.nextInt(101);
  489. tv_result.setText(name + "的人品为:" + rp);
  490. }

无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)的更多相关文章

  1. Android 在不同Actitity之间数据传递

    本文实现一个简易的人品计算器来实践在不同Actitity之间数据传递 intent的数据传递 从A界面打开B界面 把A界面的数据传递给B界面 1. intent.setData(uri) -- int ...

  2. Android 开发工具类 27_多线程下载大文件

    多线程下载大文件时序图 FileDownloader.java package com.wangjialin.internet.service.downloader; import java.io.F ...

  3. android 应用程序Activity之间数据传递与共享的几种途径

    一.基于消息的通信机制 Intent ---boudle ,extraAndroid为了屏蔽进程的概念,利用不同的组件[Activity.Service]来表示进程之间的通信!组件间通信的核心机制是I ...

  4. android Activity之间数据传递 Parcelable和Serializable接口的使用

    Activity之间传数据时,为了避免麻烦,往往会将一些值封装成对象,然后将整个对象传递过去.传对象的时候有两种情况,一种是实现Parcelable接口,一种是实现Serializable接口.0.解 ...

  5. Android activity之间数据传递和共享的方式之Application

    1.基于消息的通信机制  Intent ---bundle ,extra 数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream,或者LinkedList链表等等数据类型就不太好用 ...

  6. android 不同Activity之间数据传递

    1. 传值Activity package mydemo.mycom.demo2; import android.content.Intent; import android.support.v7.a ...

  7. Android——不同activity之间数据传递

    /* * 不同activity之间数据的传递 */ public class MainActivity extends Activity { private EditText et_name; @Ov ...

  8. Android - fragment之间数据传递

    <Fragment跳转时传递参数及结果回传的方法> <Fragment详解之五——Fragment间参数传递> <Android解惑 - 为什么要用Fragment.se ...

  9. Android检测网络是否可用并获取网络类型

    在类中使用getSystemService的时候需要这样进行使用:1. public class JajaMenu extends Activity { public static JajaMenu ...

随机推荐

  1. Unable to add window -- token null is not for an application

    导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApp ...

  2. 计蒜客 删除字母'c'

    删除字母'c' 右侧的程序实现的功能是从字符串s中删除所有的小写字母c,请改正程序错误的地方. 注意:main函数不可以改动,程序结构也不能修改. 很简单的哦,加油吧- 样例输入 abccabcn 样 ...

  3. PHP 中的行为 ,与什么是切面

    行为(Behavior)扩展以及插件(Plug or Hook)详解: 行为(Behavior)是ThinkPHP扩展机制中比较关键的一项扩展,行为即可以独立调用,也可以绑定到某个 标签中进行监听,官 ...

  4. python 内置速度最快算法(堆排)

    import random import time from heapq import heappush, heappop def heapsort(iterable): h = [] for val ...

  5. ShellCode框架(Win32ASM编写)

    主要方法: 使用宏的一切技巧让编译器 算出代码的长度 有较好的扩充性 include ShellCodeCalc.inc ;>>>>>>>>>&g ...

  6. Database、User、Schema、Tables、Col、Row

    可以把Database看作是一个大仓库,仓库分了很多很多的房间,Schema就是其中的房间,一个Schema代表一个房间,Table可以看作是每个Schema中的床,Table(床)就被放入每个房间中 ...

  7. mysql grant ,User,revoke

    mysql的权限一直都都是很关心的重点,我知道的也只是很少的一部分,对于每个数据库我习惯创建一个一个用户,该用户只对自己从属的数据库产生进行操作,在一部分的程度上可以保护自己的数据库, 比如我有一个数 ...

  8. javascript开发 ios和android app的简单介绍

    先看几个名词解释: nodejs ionic,Cordova,phoneGap,anjularjs react-native,reactjs nodeJs 的介绍参见这里,写的很好http://www ...

  9. ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)

    两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可 ...

  10. c#操作时间

    本年还剩下多少天 private string GetEndTime() { DateTime dt = DateTime.Now; DateTime startYear = DateTime.Now ...