6.安卓基础之多线程下载&Activity

1. 利用开源框架-xUtils-master-实现多线程下载

  • 需要开启如下权限
1
2
3
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
  • 实现代码
1
2
3
4
5
6
7
8
9
10
11
12
13
public void download(View v){
String path = ed_path.getText().toString().trim();
HttpUtils util = new HttpUtils();
util.download(path, "/mnt/sdcard/jjyy.exe", true, new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
toastshow.setText("下载成功");
}
@Override
public void onFailure(HttpException error, String msg) {
toastshow.setText("下载失败!");
}
});

2. 多界面应用的实现

多界面应用的实现

每个应用都可以实现多个界面,如果一个应用需要多个界面,那么需要在应用中添加多个activity就可以.

一般情况下,一个应用内部的组件可以使用显示意图也可以使用隐式意图去激活这个组件,但是如果一个外部应用中,想激活另一个应用的组件时,一般推荐使用隐式意图.

  • 从AndroidManifest.xml注册一个Activity
1
2
3
4
5
6
7
8
<!-- 注册一个activity -->
<activity android:name=".SecondActivity">
<intent-filter>
<!-- MAIN : 表示程序入口,LAUNCHER : 表示应用程序可以从laucher启动 -->
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
  • 设置两个activity_main.xml布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.javami.twoactivity.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
<!-- activity_main2.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.javami.twoactivity.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World2!" />
</RelativeLayout>
```在SecondActivity引入新的布局文件
```java
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}

2.1 人品计算器的实现

  • 务必注册一个活动
1
<activity android:name=".SecondActivity"></activity>
  • activity_main.xml的写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<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" >
<RadioGroup
android:id="@+id/rgb"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/male"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/female"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="女" />
<RadioButton
android:id="@+id/unknown"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="未知" />
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/ed_name"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="3" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="cacl"
android:text="计算" />
</LinearLayout>
<TextView
android:id="@+id/rp_value"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
  • activity_main2.xml的写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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"
android:background="#44ff0000"
tools:context=".MainActivity" >
<TextView
android:id="@+id/rp_value"
android:text="我是第二个界面"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
  • MainActivity.java的写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class MainActivity extends AppCompatActivity {
private EditText ed_name;
private TextView tv_rp;
private RadioGroup rgb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_name = (EditText)findViewById(R.id.ed_name);
tv_rp = (TextView) findViewById(R.id.rp_value);
rgb = (RadioGroup)findViewById(R.id.rgb);
}
public void cacl(View v){
String name = ed_name.getText().toString().trim();
if(TextUtils.isEmpty(name)){
Toast.makeText(this,"姓名不能为空",0).show();
return;
}
//将内容A--->B
Intent intent = new Intent();
intent.setClass(this,SecondActivity.class);
intent.putExtra("name",name);
intent.putExtra("sex",rgb.getCheckedRadioButtonId());
intent.putExtra("img", BitmapFactory.decodeResource(getResources(),R.drawable.cat));
startActivity(intent);
}
}
  • SecondActivity.java的写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class SecondActivity extends Activity {
private TextView rp_value;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
rp_value = (TextView)findViewById(R.id.rp_value);
iv = (ImageView)findViewById(R.id.iv);
//拿到激活SecondActivity是那个意图的对象
Intent intent = getIntent();
String name = intent.getStringExtra("name");
int sex = intent.getIntExtra("sex",R.id.male);
Bitmap img = intent.getParcelableExtra("img");
iv.setImageBitmap(img);
byte[] bb = null;
try{
switch (sex){
case R.id.male:
//将UTF-8编码变为二进制数据
bb = name.getBytes();
break;
case R.id.female:
bb = name.getBytes("gbk");
break;
case R.id.unknown:
bb = name.getBytes("iso8859-1");
break;
default:
break;
}
}catch (Exception e){
e.printStackTrace();
}
byte[] result = bb;
int total = 0;
for (byte b : result){
int data = b&0xff;
total = total+Math.abs(data);
}
int rp = total%100;
String data = null;
if(rp>90){
data = "good!";
}else if (rp>60){
data = "happy";
}else if(rp>30){
data = "sad";
}else{
data = "I'm 5555...";
}
rp_value.setText(data);
}
}

2.2 短信助手的应用

  • 开启短信发送权限与活动的注册
1
2
3
4
<activity android:name=".SmsAcitivityList"></activity>
<activity android:name=".ContactListActivity"></activity>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
  • activity_main.xml的写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/ed_contact"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="请输入联系人" />
<Button
android:onClick="selectContact"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+" />
</LinearLayout>
<EditText
android:id="@+id/sms_body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入短信内容"
android:lines="5" />
<Button
android:onClick="selectMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选择短信的模版" />
<Button
android:onClick="sendmsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击发送短信" />
</LinearLayout>
  • contact_item.xml的写法
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</TextView>
  • contactslist.xml的写法
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/contacts_lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
  • smsitem.xml的写法
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</TextView>
  • smslist.xml的写法
1
2
3
4
5
6
7
8
9
10
11
12
<?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="fill_parent"
android:layout_height="fill_parent"
></ListView>
</LinearLayout>
  • ContactListActivity.java的写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Created by 诸葛亮 on 2016/11/20.
*/
public class ContactListActivity extends Activity {
String[] contacts ={
"13987654321",
"18217180124",
"13687987653",
"13687987222",
"13687987555",
"13687987666",
};
private ListView contacts_lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactslist);//需要传入菜单
contacts_lv = (ListView)findViewById(R.id.contacts_lv);
contacts_lv.setAdapter(new ArrayAdapter<String>(this,R.layout.contact_item,contacts));
//添加一个item监听器
contacts_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String contact = contacts[position];
Intent intent = new Intent();
intent.putExtra("contact",contact);
setResult(0,intent);
finish();
}
});
}
}
  • MainActivity.java的写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public class MainActivity extends AppCompatActivity {
private EditText sms_body;
private EditText ed_contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sms_body = (EditText)findViewById(R.id.sms_body);
ed_contact = (EditText)findViewById(R.id.ed_contact);
}
//选择联系人
public void selectContact(View v){
Intent intent = new Intent();
intent.setClass(this,ContactListActivity.class);
startActivityForResult(intent,1);
}
//选择短信
public void selectMsg(View v){
Intent intent = new Intent();
intent.setClass(this,SmsAcitivityList.class);
startActivityForResult(intent,2);
}
//发送普通文本内容内的短信,使用的是SmsMessage类的sendTextMessage()方法
//首先调用SmsMessage类的getDefault()方法获取到SmsMessage的实例对象
final SmsManager smsManager = SmsManager.getDefault();
public void sendmsg(View v){
/*
* destinationAddress:手机号码
* scAddress:服务中心号码
* text:短信内容
* sentIntent:PendingIntent,信息发送成功或失败时触发
* deliveryIntent:PendingIntent,信息抵达收件人触发
*/
System.out.print(ed_contact.getText().toString().trim());
smsManager.sendTextMessage(ed_contact.getText().toString().trim(),null,sms_body.getText().toString().trim(),null,null);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==1){
if(data!=null){
String contact = data.getStringExtra("contact");
ed_contact.setText(contact);
}
}else if (requestCode==2){
if(data!=null){
String msg = data.getStringExtra("msg");
sms_body.setText(msg);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
  • SmsAcitivityList.java的写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Created by 诸葛亮 on 2016/11/20.
*/
public class SmsAcitivityList extends Activity {
private ListView lv;
String[] msgs = {
"想到你的名字心就会砰砰跳!看到你的容颜脸就会火辣辣烧!牵你的手像云朵轻轻飘!你可知道?我被你彻底迷倒!发誓要和你一起变老",
"你是否愿意给我一个依靠:可以让我在红尘的烦恼与喧嚣中一想到你,就会有甜蜜的平静和无穷的动力,就会有足够的勇气继续向前走。",
"命运让我们相遇,让我沉醉在你的眼眸里。我想陪着你,为你阻挡一生的风雨。请你相信,我会让你的生命,充满快乐回忆!",
"我会把心里最好的地方留给你,只要你敲敲门,我就拥你入怀。因为爱你,愿意为你在这世界造一处平台,与你纵观爱情的古往今来。",
"不经意间,相遇;不经意间,相惜;不经意间,刻骨;不经意间,铭记;不经意间,爱上了你。看似不经意,但我真的很在意。",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.smslist);
lv = (ListView)findViewById(R.id.lv);
//设置一个适配器,将意图的内容出传递进去
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.smsitem,msgs));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String msg = msgs[position];
Intent intent = new Intent();
intent.putExtra("msg",msg);
setResult(0,intent);
finish();
}
});
}
}

2.3 给一段文字使用版权链接

  • 开启联网权限
1
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
  • 基础代码的写法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class MainActivity extends AppCompatActivity {
private TextView links;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
links = (TextView)findViewById(R.id.links);
/*
*
<intent-filter>
<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-filter>
*/
links.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("http://www.fkcat.com"));
startActivity(intent);
}
});
}
}

3. 总结

加强与总结,多实践代码!并且看谷歌官方文档!