多线程下载的原理
司马光砸缸,多开几个小水管,抢救小朋友。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class MutileThreadDownload {
/**
* 线程的数量
*/
private static int threadCount = 3;
/**
* 每个下载区块的大小
*/
private static long blocksize;
/**
* 正在运行的线程的数量
*/
private static int runningThreadCount;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 服务器文件的路径
String path = "http://192.168.1.100:8080/ff.exe";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
long size = conn.getContentLength();// 得到服务端返回的文件的大小
System.out.println("服务器文件的大小:" + size);
blocksize = size / threadCount;
// 1.首先在本地创建一个大小跟服务器一模一样的空白文件。
File file = new File("temp.exe");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.setLength(size);
// 2.开启若干个子线程分别去下载对应的资源。
runningThreadCount = threadCount;
for (int i = 1; i <= threadCount; i++) {
long startIndex = (i - 1) * blocksize;
long endIndex = i * blocksize - 1;
if (i == threadCount) {
// 最后一个线程
endIndex = size - 1;
}
System.out.println("开启线程:" + i + "下载的位置:" + startIndex + "~"
+ endIndex);
new DownloadThread(path, i, startIndex, endIndex).start();
}
}
conn.disconnect();
}
private static class DownloadThread extends Thread {
private int threadId;
private long startIndex;
private long endIndex;
private String path;
public DownloadThread(String path, int threadId, long startIndex,
long endIndex) {
this.path = path;
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
try {
// 当前线程下载的总大小
int total = 0;
File positionFile = new File(threadId + ".txt");
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
// 接着从上一次的位置继续下载数据
if (positionFile.exists() && positionFile.length() > 0) {// 判断是否有记录
FileInputStream fis = new FileInputStream(positionFile);
BufferedReader br = new BufferedReader(
new InputStreamReader(fis));
// 获取当前线程上次下载的总大小是多少
String lasttotalstr = br.readLine();
int lastTotal = Integer.valueOf(lasttotalstr);
System.out.println("上次线程" + threadId + "下载的总大小:"
+ lastTotal);
startIndex += lastTotal;
total += lastTotal;// 加上上次下载的总大小。
fis.close();
}
conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
+ endIndex);
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
System.out.println("code=" + code);
InputStream is = conn.getInputStream();
File file = new File("temp.exe");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// 指定文件开始写的位置。
raf.seek(startIndex);
System.out.println("第" + threadId + "个线程:写文件的开始位置:"
+ String.valueOf(startIndex));
int len = 0;
byte[] buffer = new byte[512];
while ((len = is.read(buffer)) != -1) {
RandomAccessFile rf = new RandomAccessFile(positionFile,
"rwd");
raf.write(buffer, 0, len);
total += len;
rf.write(String.valueOf(total).getBytes());
rf.close();
}
is.close();
raf.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 只有所有的线程都下载完毕后 才可以删除记录文件。
synchronized (MutileThreadDownload.class) {
System.out.println("线程" + threadId + "下载完毕了");
runningThreadCount--;
if (runningThreadCount < 1) {
System.out.println("所有的线程都工作完毕了。删除临时记录的文件");
for (int i = 1; i <= threadCount; i++) {
File f = new File(i + ".txt");
System.out.println(f.delete());
}
}
}
}
}
}
}
多线程断点下载的原理
同上
多线程断点下载的移植
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.mutilethread"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.mutilethread.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入下载文件的路径" />
<EditText
android:id="@+id/et_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://192.168.101.138:2222/zuihou.mp3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入线程的数量" />
<EditText
android:id="@+id/et_count"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text="3" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="downLoad"
android:text="下载" />
<LinearLayout
android:id="@+id/ll_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</LinearLayout>
pb.xml
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
package com.itheima.mutilethread;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final int DOWNLOAD_ERROR = 1;
private static final int THREAD_ERROR = 2;
public static final int DWONLOAD_FINISH = 3;
private EditText et_path;
private EditText et_count;
/**
* 存放进度条的布局
*/
private LinearLayout ll_container;
/**
* 进度条的集合
*/
private List<ProgressBar> pbs;
/**
* android下的消息处理器,在主线程创建,才可以更新ui
*/
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWNLOAD_ERROR:
Toast.makeText(getApplicationContext(), "下载失败", 0).show();
break;
case THREAD_ERROR:
Toast.makeText(getApplicationContext(), "下载失败,请重试", 0).show();
break;
case DWONLOAD_FINISH:
Toast.makeText(getApplicationContext(), "下载完成", 0).show();
break;
}
};
};
/**
* 线程的数量
*/
private int threadCount = 3;
/**
* 每个下载区块的大小
*/
private long blocksize;
/**
* 正在运行的线程的数量
*/
private int runningThreadCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
et_count = (EditText) findViewById(R.id.et_count);
ll_container = (LinearLayout) findViewById(R.id.ll_container);
}
/**
* 下载按钮的点击事件
* @param view
*/
public void downLoad(View view){
//下载文件的路径
final String path = et_path.getText().toString().trim();
if(TextUtils.isEmpty(path)){
Toast.makeText(this, "对不起下载路径不能为空", 0).show();
return;
}
String count = et_count.getText().toString().trim();
if(TextUtils.isEmpty(path)){
Toast.makeText(this, "对不起,线程数量不能为空", 0).show();
return;
}
threadCount = Integer.parseInt(count);
//清空掉旧的进度条
ll_container.removeAllViews();
//在界面里面添加count个进度条
pbs = new ArrayList<ProgressBar>();
for(int j=0;j<threadCount;j++){
ProgressBar pb = (ProgressBar) View.inflate(this, R.layout.pb, null);
ll_container.addView(pb);
pbs.add(pb);
}
Toast.makeText(this, "开始下载", 0).show();
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) {
long size = conn.getContentLength();// 得到服务端返回的文件的大小
System.out.println("服务器文件的大小:" + size);
blocksize = size / threadCount;
// 1.首先在本地创建一个大小跟服务器一模一样的空白文件。
File file = new File(Environment.getExternalStorageDirectory(),getFileName(path));
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.setLength(size);
// 2.开启若干个子线程分别去下载对应的资源。
runningThreadCount = threadCount;
for (int i = 1; i <= threadCount; i++) {
long startIndex = (i - 1) * blocksize;
long endIndex = i * blocksize - 1;
if (i == threadCount) {
// 最后一个线程
endIndex = size - 1;
}
System.out.println("开启线程:" + i + "下载的位置:" + startIndex + "~"
+ endIndex);
int threadSize = (int) (endIndex - startIndex);
pbs.get(i-1).setMax(threadSize);
new DownloadThread(path, i, startIndex, endIndex).start();
}
}
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = DOWNLOAD_ERROR;
handler.sendMessage(msg);
}
};
}.start();
}
private class DownloadThread extends Thread {
private int threadId;
private long startIndex;
private long endIndex;
private String path;
public DownloadThread(String path, int threadId, long startIndex,
long endIndex) {
this.path = path;
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
try {
// 当前线程下载的总大小
int total = 0;
File positionFile = new File(Environment.getExternalStorageDirectory(),getFileName(path)+threadId + ".txt");
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
// 接着从上一次的位置继续下载数据
if (positionFile.exists() && positionFile.length() > 0) {// 判断是否有记录
FileInputStream fis = new FileInputStream(positionFile);
BufferedReader br = new BufferedReader(
new InputStreamReader(fis));
// 获取当前线程上次下载的总大小是多少
String lasttotalstr = br.readLine();
int lastTotal = Integer.valueOf(lasttotalstr);
System.out.println("上次线程" + threadId + "下载的总大小:"
+ lastTotal);
startIndex += lastTotal;
total += lastTotal;// 加上上次下载的总大小。
fis.close();
//存数据库。
//_id path threadid total
}
conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
+ endIndex);
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
System.out.println("code=" + code);
InputStream is = conn.getInputStream();
File file = new File(Environment.getExternalStorageDirectory(),getFileName(path));
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// 指定文件开始写的位置。
raf.seek(startIndex);
System.out.println("第" + threadId + "个线程:写文件的开始位置:"
+ String.valueOf(startIndex));
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
RandomAccessFile rf = new RandomAccessFile(positionFile,
"rwd");
raf.write(buffer, 0, len);
total += len;
rf.write(String.valueOf(total).getBytes());
rf.close();
pbs.get(threadId-1).setProgress(total);
}
is.close();
raf.close();
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = THREAD_ERROR;
handler.sendMessage(msg);
} finally {
// 只有所有的线程都下载完毕后 才可以删除记录文件。
synchronized (MainActivity.class) {
System.out.println("线程" + threadId + "下载完毕了");
runningThreadCount--;
if (runningThreadCount < 1) {
System.out.println("所有的线程都工作完毕了。删除临时记录的文件");
for (int i = 1; i <= threadCount; i++) {
File f = new File(Environment.getExternalStorageDirectory(),getFileName(path)+ i + ".txt");
System.out.println(f.delete());
}
Message msg = Message.obtain();
msg.what = DWONLOAD_FINISH;
handler.sendMessage(msg);
}
}
}
}
}
//http://192.168.1.100:8080/aa.exe
private String getFileName(String path){
int start = path.lastIndexOf("/")+1;
return path.substring(start);
}
}
开源项目xutils
xUtils-master.zip
<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" >
<EditText
android:id="@+id/et_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://192.168.101.138:2222/zuihou.mp3" />
<Button
android:onClick="download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载" />
<TextView
android:id="@+id/tv_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
xUtils-2.4.7.jar
package com.itheima.download2;
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
public class MainActivity extends Activity {
private EditText et_path;
private TextView tv_info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
tv_info = (TextView) findViewById(R.id.tv_info);
}
public void download(View view) {
String path = et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "请输入下载的路径", 0).show();
return;
} else {
HttpUtils http = new HttpUtils();
HttpHandler handler = http.download(path, "/sdcard/abc.mp3", true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。
new RequestCallBack<File>() {
@Override
public void onStart() {
tv_info.setText("conn...");
}
@Override
public void onLoading(long total, long current,
boolean isUploading) {
tv_info.setText(current + "/" + total);
}
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
tv_info.setText("downloaded:"
+ responseInfo.result.getPath());
}
@Override
public void onFailure(HttpException error, String msg) {
tv_info.setText(msg);
}
});
}
}
}
显示意图
人品计算器的案例
隐式意图
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.intent2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.intent2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.itheima.intent2.SecondActivity" >
<intent-filter>
<action android:name="com.itheima.intent2.open2" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/person" />
<data android:scheme="jianren" />
</intent-filter>
</activity>
</application>
</manifest>
<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"
tools:context=".MainActivity" >
<Button
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="隐式意图激活界面" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是第二个界面"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
package com.itheima.intent2;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
// 打 action
// 人 数据
// 附件的数据 Category 类别
Intent intent = new Intent();
intent.setAction("com.itheima.intent2.open2");
intent.addCategory(Intent.CATEGORY_DEFAULT);
// URL:统一资源定位符 http https ftp rtsp: URI:统一资源标识符 url是uri的一个子集
// intent.setData(Uri.parse("jianren:张三"));
// intent.setType("application/person");
intent.setDataAndType(Uri.parse("jianren:张三"), "application/person");
startActivity(intent);
}
}
package com.itheima.intent2;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
打开浏览器
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.openbrowser"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.openbrowser.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<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"
tools:context=".MainActivity" >
<Button
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="打开百度" />
</RelativeLayout>
package com.itheima.openbrowser;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
// <action android:name="android.intent.action.VIEW" />
// <category android:name="android.intent.category.DEFAULT" />
// <category android:name="android.intent.category.BROWSABLE" />
// <data android:scheme="http" />
// <data android:scheme="https" />
// <data android:scheme="about" />
// <data android:scheme="javascript" />
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addCategory("android.intent.category.BROWSABLE");
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
}
人品计算器
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.rpcalc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.rpcalc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.itheima.rpcalc.CalcActivity"
android:label="@string/title_activity_calc" >
</activity>
</application>
</manifest>
<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"
tools:context=".MainActivity" >
<Button
android:id="@+id/bt_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="enter"
android:text="点击进入人品计算器" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/rp" />
<EditText
android:id="@+id/et_name"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/bt_calc"
android:hint="请输入要计算的姓名" />
</RelativeLayout>
<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"
tools:context=".CalcActivity" >
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="我是计算器页面" />
</RelativeLayout>
package com.itheima.rpcalc;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class CalcActivity extends Activity {
private TextView tv_result;
//当activity被创建的时候调用的方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calc);
tv_result = (TextView) findViewById(R.id.tv_result);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
byte[] result = name.getBytes();
int number = 0;
for(byte b :result){
number += b&0xff;
}
int sorce = Math.abs(number)%100;
tv_result.setText(name+"的人品:"+sorce);
}
}
package com.itheima.rpcalc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
}
//显式意图 :必须指定要激活的组件的完整包名和类名 (应用程序之间耦合在一起)
// 一般激活自己应用的组件的时候 采用显示意图
//隐式意图: 只需要指定要动作和数据就可以 ( 好处应用程序之间没有耦合)
//激活别人写的应用 隐式意图, 不需要关心对方的包名和类名
public void enter(View view){
String name = et_name.getText().toString().trim();
if(TextUtils.isEmpty(name)){
Toast.makeText(this, "请输入姓名", 0).show();
return;
}
//意图 开电视 打人 打酱油
Intent intent = new Intent(this, CalcActivity.class);
intent.putExtra("name", name);
//显式意图
//intent.setClassName(getPackageName(), "com.itheima.rpcalc.CalcActivity");//不指定动作,也不指定数据 直接指定要激活的组件
startActivity(intent);
//隐式意图
//intent.setAction(action);
//intent.setData(data);
}
}
开启activity获取返回值
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.smshelper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.smshelper.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.itheima.smshelper.ListSmsActivity" >
</activity>
<activity android:name="com.itheima.smshelper.ListNumberActivity" >
</activity>
</application>
</manifest>
sms_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@android:drawable/star_big_on"
android:gravity="center_vertical"
android:text="我爱你。" />
</LinearLayout>
activity_sms.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
activity_main.xml
<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" >
<EditText
android:id="@+id/et_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入电话号码" />
<EditText
android:id="@+id/et_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入短信的内容"
android:inputType="textMultiLine"
android:lines="5" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectSms"
android:text="选择短信" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectNumber"
android:text="选择电话号码" />
<Button
android:onClick="sendSms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送短信" />
</LinearLayout>
package com.itheima.smshelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListNumberActivity extends Activity {
private ListView lv;
private String[] objects = {
"1234","34324","5643543","32424" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.sms_item,
R.id.tv_info, objects));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String smsinfo = objects[position];
Intent data = new Intent();
data.putExtra("smsinfo", smsinfo);
//设置数据。
setResult(1, data);
//关闭掉当前的activity,并且回传数据 onActivityResult().
finish();
}
});
}
}
package com.itheima.smshelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListSmsActivity extends Activity {
private ListView lv;
private String[] objects = {
"玫瑰香香,情人黏黏,情话甜甜,情歌绵绵;花灯灿灿,礼花点点,好运连连,好梦圆圆。情人节喜逢元宵节,喜鹊登枝蝴蝶成双鸳鸯成对双喜临门祝双节快乐,合家团团圆圆,甜甜蜜蜜,开开心心,幸幸福福",
"情人节快到了,我精心挑选玫瑰花、百合花和满天星,扎成一束鲜花随短信送给你,火红的玫瑰代表热烈奔放,洁白的百合代表百年好合,小小的满天星代表幸福美好。愿你的情人节热烈奔放,你们的爱情百年好合,你们的生活幸福美满。预祝情人节快乐",
"^o^满天星光,最爱你许过愿望的那一颗,鲜花绽放,最爱你摘下微笑的那一朵,曼妙旋律,最爱你感动落泪的那一段,亲爱的,情人节快乐,爱你。 ",
"^o^宝贝,情人节到了,送你一束玫瑰,用真心塑料纸包扎,系上快乐彩带,喷点爱的香水,插一张真情卡片,写着:宝贝,愿我的爱能带给你一生的快乐!" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.sms_item,
R.id.tv_info, objects));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String smsinfo = objects[position];
Intent data = new Intent();
data.putExtra("smsinfo", smsinfo);
//设置数据。
setResult(0, data);
//关闭掉当前的activity,并且回传数据 onActivityResult().
finish();
}
});
}
}
package com.itheima.smshelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_content;
private EditText et_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_content = (EditText) findViewById(R.id.et_content);
et_number = (EditText) findViewById(R.id.et_number);
}
public void selectSms(View view) {
Intent intent = new Intent(this, ListSmsActivity.class);
// 开启一个新的界面,并且获取界面的返回值
// startActivity(intent);
// int requestCode 请求码
startActivityForResult(intent, 0);
}
public void selectNumber(View view) {
Intent intent = new Intent(this, ListNumberActivity.class);
// 开启一个新的界面,并且获取界面的返回值
// startActivity(intent);
// int requestCode 请求码
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
String smsinfo = data.getStringExtra("smsinfo");
if (resultCode == 0) {
et_content.setText(smsinfo);
} else if (resultCode == 1) {
et_number.setText(smsinfo);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void sendSms(View view){
String content = et_content.getText().toString().trim();
String number = et_number.getText().toString().trim();
SmsManager.getDefault().sendTextMessage(number, null, content, null, null);
Toast.makeText(this, "发送成功", 0).show();
}
}