无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)
- 1.smartimageview使用
- <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" >
- <com.loopj.android.image.SmartImageView
- android:id="@+id/iv_beauty"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="100" />
- <EditText
- android:id="@+id/et_path"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:text="http://a.hiphotos.baidu.com/album/w%3D2048/sign=0a938b00d53f8794d3ff4f2ee6230cf4/faedab64034f78f06fe0f24b78310a55b2191c9a.jpg" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="watch"
- android:text="浏览" />
- </LinearLayout>
- public class MainActivity extends Activity {
- private EditText et_path;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.et_path = (EditText) this.findViewById(R.id.et_path);
- }
- public void watch(View view) {
- String url = this.et_path.getText().toString().trim();
- SmartImageView siv = (SmartImageView) this.findViewById(R.id.iv_beauty);
- siv.setImageUrl(url, R.drawable.ic_launcher, R.drawable.ic_launcher);
- }
- }
- <uses-permission android:name="android.permission.INTERNET"/>
- 2.android多线程下载
- public class MainActivity extends Activity {
- protected static final int SERVER_ERROR = 1;
- protected static final int DOWN_LOAD_ERROR = 2;
- protected static final int DOWN_LOAD_SUCCESS = 3;
- protected static final int UPDATE_TEXT = 4;
- private EditText et_path;
- private ProgressBar pb_download;
- private TextView tv_process;
- private static int threadCount = 3;
- private static int runningThread = threadCount;
- private static int currentProgress = 0;
- private static String resource = "/mnt/sdcard/oracle.pptx";
- private static String path = "http://110.65.99.66:8080/oracle.pptx";
- private static String mode = "rwd";
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case SERVER_ERROR:
- Toast.makeText(MainActivity.this,
- "服务器错误,错误码:" + (String) msg.obj, 0).show();
- break;
- case DOWN_LOAD_ERROR:
- Toast.makeText(MainActivity.this, "下载失败", 0).show();
- break;
- case DOWN_LOAD_SUCCESS:
- Toast.makeText(MainActivity.this, "文件下载完毕,临时文件被删除!", 0).show();
- break;
- case UPDATE_TEXT:
- tv_process.setText("当前进度:" + pb_download.getProgress() * 100
- / pb_download.getMax());
- break;
- }
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.et_path = (EditText) this.findViewById(R.id.et_path);
- this.pb_download = (ProgressBar) this.findViewById(R.id.pb_download);
- this.tv_process = (TextView) this.findViewById(R.id.tv_process);
- }
- public void download(View view) {
- new Thread() {
- public void run() {
- try {
- // 连接服务器获取一个文件,在本地创建一个和它相同大小的临时文件
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url
- .openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5000);
- int code = conn.getResponseCode();
- if (code == 200) {
- // 服务器返回的数据长度,实际上就是文件的长度
- int length = conn.getContentLength();
- System.out.println("文件的长度 :" + length);
- // 设置进度条的最大值
- pb_download.setMax(length - 1);
- // 在本地创建一个大小跟服务器文件一样大小的临时文件
- RandomAccessFile raf = new RandomAccessFile(resource,
- mode);
- // 指定文件的长度
- raf.setLength(length);
- raf.close();
- // 假设是3个线程去下载资源
- // 计算平均每个线程下载的文件大小
- int blockSize = length / threadCount;
- // 标记正在运行子线程的个数
- runningThread = threadCount;
- // 下载进度归零
- currentProgress = 0;
- // 下载进度条归零
- pb_download.setProgress(0);
- System.out.println("blockSize = " + blockSize);
- for (int threadId = 1; threadId <= threadCount; threadId++) {
- // 线程的开始和结束位置
- int startIndex = (threadId - 1) * blockSize;
- int endIndex = threadId * blockSize - 1;
- if (threadId == threadCount) {
- endIndex = length - 1;
- }
- System.out.println("线程:" + threadId + "下载:"
- + startIndex + "--->" + endIndex);
- new DownLoadThread(threadId, startIndex, endIndex,
- path).start();
- }
- } else {
- Message msg = new Message();
- msg.what = SERVER_ERROR;
- msg.obj = code + "";
- handler.sendMessage(msg);
- }
- } catch (Exception e) {
- e.printStackTrace();
- Message msg = new Message();
- msg.what = DOWN_LOAD_ERROR;
- handler.sendMessage(msg);
- }
- };
- }.start();
- }
- public class DownLoadThread extends Thread {
- private int threadId;
- private int startIndex;
- private int endIndex;
- private String path;
- private String tempFileUrl;
- public DownLoadThread(int threadId, int startIndex, int endIndex,
- String path) {
- this.threadId = threadId;
- this.startIndex = startIndex;
- this.endIndex = endIndex;
- this.path = path;
- tempFileUrl = "/mnt/sdcard/" + threadId + ".txt";
- }
- @Override
- public void run() {
- try {
- // 检查是否存在记录下载长度的文件,如果存在读取这个文件的数据
- File tempFile = new File(tempFileUrl);
- if (tempFile.exists() && tempFile.length() > 0) {
- FileInputStream fis = new FileInputStream(tempFile);
- byte[] buffer = new byte[1024];
- int length = fis.read(buffer);
- String downloadLength = new String(buffer, 0, length);
- int downloadLenInt = Integer.parseInt(downloadLength);
- int alreayDownloadInt = downloadLenInt - startIndex;
- currentProgress += alreayDownloadInt;// 计算上次断点下载到的进度
- // 修改下载的真实开始位置
- startIndex = downloadLenInt;
- fis.close();
- }
- System.out.println("线程真正开始的位置 :" + threadId + "下载:"
- + startIndex + "--->" + endIndex);
- URL url = new URL(path);
- HttpURLConnection conn = (HttpURLConnection) url
- .openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5000);
- // 请求服务器下载部分文件,指定文件的位置
- conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
- + endIndex);
- conn.setConnectTimeout(60000);
- // 200表示从服务器请求全部资源,206表示从服务器请求部分资源
- int code = conn.getResponseCode();
- if (code == 206) {
- // 已经设置了请求的位置,返回的是当前文件位置对应的文件的输入流
- InputStream is = conn.getInputStream();
- // 此类的实例支持对随机访问文件的读取和写入
- RandomAccessFile raf = new RandomAccessFile(resource, mode);
- // 定位文件
- raf.seek(startIndex);
- int length = 0;
- byte[] buffer = new byte[1024];
- int total = 0;// 实时记录已经下载的长度
- // 一阵狂读
- while ((length = is.read(buffer)) != -1) {
- RandomAccessFile file = new RandomAccessFile(
- tempFileUrl, mode);
- raf.write(buffer, 0, length);
- total += length;
- // 记录当前线程下载的数据位置
- file.write((total + startIndex - 1 + "").getBytes());
- file.close();
- synchronized (MainActivity.this) {
- // 获取所有线程下载的总进度
- currentProgress += length;
- // 更改界面上progressbar的进度(progressbar和progressdialog可以在子线程里修改UI)
- pb_download.setProgress(currentProgress);
- Message msg = Message.obtain();
- msg.what = UPDATE_TEXT;
- handler.sendMessage(msg);
- }
- }
- raf.close();
- is.close();
- System.out.println("线程 : " + threadId + "下载完毕。。。。。");
- runningThread--;
- } else {
- System.out.println("线程 : " + threadId + "下载失败。。。。。");
- }
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("线程 : " + threadId + " 抛出异常!");
- runningThread = threadCount;
- } finally {
- System.out.println("runningThread = " + runningThread);
- if (runningThread == 0) {
- for (int i = 1; i <= threadCount; i++) {
- File deleteFile = new File("/mnt/sdcard/" + i + ".txt");
- deleteFile.delete();
- }
- Message msg = new Message();
- msg.what = DOWN_LOAD_SUCCESS;
- handler.sendMessage(msg);
- }
- }
- }
- }
- }
- 3.显式意图激活另外一个activity,检查网络是否可用定位到网络的位置
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- 这段代码的作用是告诉系统在桌面创建一个快捷图标
- <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:onClick="click1"
- android:text="跳转到第二个界面1" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignRight="@+id/button1"
- android:layout_below="@+id/button1"
- android:layout_marginTop="24dp"
- android:onClick="click2"
- android:text="跳转到第二个界面2" />
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/button2"
- android:layout_below="@+id/button2"
- android:layout_marginLeft="44dp"
- android:layout_marginTop="27dp"
- android:onClick="click3"
- android:text="检查手机网络状态" />
- <Button
- android:id="@+id/button4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/button2"
- android:layout_below="@+id/button3"
- android:layout_marginLeft="16dp"
- android:layout_marginTop="37dp"
- android:onClick="click4"
- android:text="跳转到图库" />
- </RelativeLayout>
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void click1(View view) {
- Intent intent = new Intent();
- intent.setClassName(this, "com.itheima.explicitintent.SecondActivity");
- this.startActivity(intent);
- }
- public void click2(View view) {
- Intent intent = new Intent(this, SecondActivity.class);
- this.startActivity(intent);
- }
- public void click3(View view) {
- ConnectivityManager manager = (ConnectivityManager) this
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo info = manager.getActiveNetworkInfo();
- if (info != null && info.isAvailable()) {
- Toast.makeText(this, "网络可用", 0).show();
- } else {
- Toast.makeText(this, "网络不可用", 0).show();
- Intent intent = new Intent();
- intent.setClassName("com.android.phone",
- "com.android.phone.MiuiSettings");
- this.startActivity(intent);
- }
- }
- public void click4(View view) {
- Intent intent = new Intent();
- intent.setClassName("com.miui.gallery", "com.miui.gallery.app.Gallery");
- this.startActivity(intent);
- }
- }
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- 4.隐式意图激活另外一个activity,即打开另外一个app的activity
- public void click(View view) {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_VIEW);
- intent.setData(Uri.parse("http://www.baidu.com"));
- this.startActivity(intent);
- }
- public void send(View view) {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_SENDTO);
- intent.setData(Uri.parse("sms:10086"));
- intent.addCategory("android.intent.category.DEFAULT");
- this.startActivity(intent);
- }
- .隐式意图的配置,自定义隐式意图
- 第一个activity启动第二个activity
- 第一个Activity
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void click(View view) {
- Intent intent = new Intent();
- intent.setAction("com.itheima.secondActivity");
- // 额外信息,提供一些执行的环境参数
- intent.addCategory("android.intent.category.DEFAULT");
- intent.setDataAndType(Uri.parse("itheima:jerry"),
- "vnd.android.cursor.item/mp3");
- this.startActivity(intent);
- }
- }
- 第二个Activity
- public class SecondActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- Intent intent = this.getIntent();
- Uri uri = intent.getData();
- String data = uri.toString();
- String type = intent.getType();
- Toast.makeText(this, "data = " + data + " , type = " + type, 0).show();
- }
- }
- <activity
- android:name=".SecondActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="com.itheima.secondActivity"/>
- <category android:name="android.intent.category.DEFAULT"/>
- <data android:scheme="itheima" android:mimeType="vnd.android.cursor.item/mp3"/>
- </intent-filter>
- </activity>
- .隐式意图和显示意图的使用场景
- 同一个应用程序里面,自己激活自己的东西,推荐使用显示意图,效率高
- 不同的应用程序里面,激活别人的应用,或者是让自己的某一个界面希望被别人激活,推荐使用隐式意图。
- 7.在不同activity之间数据传递
- public void click(View view) {
- String name = this.et_name.getText().toString().trim();
- if (TextUtils.isEmpty(name)) {
- Toast.makeText(this, "姓名不能为空", 0).show();
- return;
- }
- Intent intent = new Intent(this, ResultActivity.class);
- intent.putExtra("name", name);
- this.startActivity(intent);
- }
- 接收数据
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_result);
- TextView tv_result = (TextView) this.findViewById(R.id.tv_result);
- Intent intent = this.getIntent();
- String name = intent.getStringExtra("name");
- Random random = new Random();
- int rp = random.nextInt(101);
- tv_result.setText(name + "的人品为:" + rp);
- }
无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)的更多相关文章
- Android 在不同Actitity之间数据传递
本文实现一个简易的人品计算器来实践在不同Actitity之间数据传递 intent的数据传递 从A界面打开B界面 把A界面的数据传递给B界面 1. intent.setData(uri) -- int ...
- Android 开发工具类 27_多线程下载大文件
多线程下载大文件时序图 FileDownloader.java package com.wangjialin.internet.service.downloader; import java.io.F ...
- android 应用程序Activity之间数据传递与共享的几种途径
一.基于消息的通信机制 Intent ---boudle ,extraAndroid为了屏蔽进程的概念,利用不同的组件[Activity.Service]来表示进程之间的通信!组件间通信的核心机制是I ...
- android Activity之间数据传递 Parcelable和Serializable接口的使用
Activity之间传数据时,为了避免麻烦,往往会将一些值封装成对象,然后将整个对象传递过去.传对象的时候有两种情况,一种是实现Parcelable接口,一种是实现Serializable接口.0.解 ...
- Android activity之间数据传递和共享的方式之Application
1.基于消息的通信机制 Intent ---bundle ,extra 数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream,或者LinkedList链表等等数据类型就不太好用 ...
- android 不同Activity之间数据传递
1. 传值Activity package mydemo.mycom.demo2; import android.content.Intent; import android.support.v7.a ...
- Android——不同activity之间数据传递
/* * 不同activity之间数据的传递 */ public class MainActivity extends Activity { private EditText et_name; @Ov ...
- Android - fragment之间数据传递
<Fragment跳转时传递参数及结果回传的方法> <Fragment详解之五——Fragment间参数传递> <Android解惑 - 为什么要用Fragment.se ...
- Android检测网络是否可用并获取网络类型
在类中使用getSystemService的时候需要这样进行使用:1. public class JajaMenu extends Activity { public static JajaMenu ...
随机推荐
- Unable to add window -- token null is not for an application
导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApp ...
- 计蒜客 删除字母'c'
删除字母'c' 右侧的程序实现的功能是从字符串s中删除所有的小写字母c,请改正程序错误的地方. 注意:main函数不可以改动,程序结构也不能修改. 很简单的哦,加油吧- 样例输入 abccabcn 样 ...
- PHP 中的行为 ,与什么是切面
行为(Behavior)扩展以及插件(Plug or Hook)详解: 行为(Behavior)是ThinkPHP扩展机制中比较关键的一项扩展,行为即可以独立调用,也可以绑定到某个 标签中进行监听,官 ...
- python 内置速度最快算法(堆排)
import random import time from heapq import heappush, heappop def heapsort(iterable): h = [] for val ...
- ShellCode框架(Win32ASM编写)
主要方法: 使用宏的一切技巧让编译器 算出代码的长度 有较好的扩充性 include ShellCodeCalc.inc ;>>>>>>>>>&g ...
- Database、User、Schema、Tables、Col、Row
可以把Database看作是一个大仓库,仓库分了很多很多的房间,Schema就是其中的房间,一个Schema代表一个房间,Table可以看作是每个Schema中的床,Table(床)就被放入每个房间中 ...
- mysql grant ,User,revoke
mysql的权限一直都都是很关心的重点,我知道的也只是很少的一部分,对于每个数据库我习惯创建一个一个用户,该用户只对自己从属的数据库产生进行操作,在一部分的程度上可以保护自己的数据库, 比如我有一个数 ...
- javascript开发 ios和android app的简单介绍
先看几个名词解释: nodejs ionic,Cordova,phoneGap,anjularjs react-native,reactjs nodeJs 的介绍参见这里,写的很好http://www ...
- ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)
两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可 ...
- c#操作时间
本年还剩下多少天 private string GetEndTime() { DateTime dt = DateTime.Now; DateTime startYear = DateTime.Now ...