【黑马Android】(07)多线程下载的原理/开源项目xutils/显示意图/隐式意图/人品计算器/开启activity获取返回值

多线程下载的原理

司马光砸缸,多开几个小水管,抢救小朋友。

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();
	}
}
时间: 2024-10-02 20:22:29

【黑马Android】(07)多线程下载的原理/开源项目xutils/显示意图/隐式意图/人品计算器/开启activity获取返回值的相关文章

Android APP代码拨打电话、打开手机分享功能等隐式意图

Android APP拨打电话: Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+110)); startActivity(intent); } Android APP打开电话薄: Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI); startActivity(intent); Android

无废话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中显示和隐式Intent的使用

显示启动activity                                                                                                                   这个很简单,下面是代码示例:为了看着方便,所以就把layout直接截图了,里面的代码只是一些简单的布局.activity_login.xml: activity_main.xml: 下面是Manifest.xml中的代码 1 <?xml vers

(Android review)显示意图激活与隐式意图激活

一.基本知识点 1.<activity android:label="第一个activity" android:name=".Main2Activity"/> label属性:某个Acivity的标题 2.R文件不要引错了,引成Android底层的了 3.intent.setClass(this, Main2Activity.class);第一个参数:上下文第二个参数:要激活的组件的字节码文件 4.显示意图激活(明确指定了要激活的组件)1)intent.

android隐式意图激活自定义界面

我们也可以使用隐士意图激活自定义的界面,并且可以携带数据: 效果: 点击第二个按钮后: 附代码: 主窗体的代码: 1 package com.yy.twoactivity; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.net.Uri; 6 import android.os.Bundle; 7 import android.view.View; 8 9 public clas

android隐式意图激活浏览器

在有些时候,我们可能想打开某个不确定的应用,比如,我们想要通过浏览器打开百度的站点,但是我们并不强调必须要使用哪一个浏览器,那么这种情况我们使用显示意图可能不太好,更好的做法是使用隐式意图打开网站. 效果: 点击按钮后: 附代码: 1 package com.yy.activity; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.net.Uri; 6 import androi

java多线程之从任务中获取返回值

package wzh.test; import java.util.ArrayList; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; class TaskWithResult implements Callable<Strin

Android onActivityResult获取返回值的用法

2014-03-09 00:08:15 分类: Android平台 现有MainActivity,当它进入到SecondActivity后,在SecondActivity中进行了某些操作然后需要将值返回给MainActivity时, 于是就可以用到onActivityResult() 这个方法了,下面呢,我就具体来介绍一下用法. 不过在这之前,先讲一下intent的用法: 对于intent主要的分类主要包括隐式意图和显式意图.显式意图通常主要是启动本应用中的Activity之间的数据, 而隐式意

Android 显示意图和隐式意图的区别

意图在android的应用开发中是很重要的,明白了意图的作用和使用后,对开发会有很大帮助.如果没有把意图搞懂,以后开发应用会感觉缺些什么. 意图的作用: 1.激活组件 2.携带数据 3.意图的匹配(运用到隐式意图) android基本的设计理念是鼓励减少组件间的耦合,因此android提供了Intent(意图),用意图激活其他组件.Intent提供了一种通用的消息系统,它允许在你的应用程序与其他应用程序间传递Intent来执行和产生事件.使用Intent可以激活android应用的三个核心组件: