Android复杂类型传递

  1. Serilziable

    把需要传递的类实现Serilziable接口

  2. Parceable

    把需要传递的类实现Parceable接口

UserInfo.java

1
2
3
4
5
6
7
8
9
10
package com.example.inter;

import java.io.Serializable;

public class UserInfo implements Serializable {
public String UserName;
public int age;
public int gender;
public String address;
}

Order.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
package com.example.inter;

import android.os.Parcel;
import android.os.Parcelable;

public class Order implements Parcelable {
public String address;
public boolean isReceived;
public int count;

public Order() {
}

protected Order(Parcel in) {
address = in.readString();
count = in.readInt();
boolean[] booleans = new boolean[1];
in.readBooleanArray(booleans);
isReceived = booleans[0];

}

public static final Creator<Order> CREATOR = new Creator<Order>() {
@Override
public Order createFromParcel(Parcel in) {
return new Order(in);
}

@Override
public Order[] newArray(int size) {
return new Order[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(address);
dest.writeInt(count);
dest.writeBooleanArray(new boolean[]{isReceived});
}
}

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
package com.example.inter;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,InterActivity.class);
//复杂类型数据传递-Serializable
// UserInfo userInfo= new UserInfo();
// userInfo.UserName = "ala";
// userInfo.address = "dalian";
// userInfo.age = 18;
// userInfo.gender = 1;
// intent.putExtra("UserInfo",userInfo);
//复杂类型数据传递-Parcelable
Order order = new Order();
order.address = "samaolu";
order.count = 18;
order.isReceived = false;
intent.putExtra("order",order);
startActivity(intent);
}
});
}
}

InterActivity.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
package com.example.inter;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;

import android.widget.TextView;

import androidx.annotation.Nullable;

public class InterActivity extends Activity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inter_layout);
//获取组件对象
TextView textView = (TextView) findViewById(R.id.text1);
TextView int1 = (TextView) findViewById(R.id.int1);
//复杂类型数据获取-Serializable
// Intent intent = getIntent();
// UserInfo userInfo = (UserInfo) intent.getSerializableExtra("UserInfo");
// textView.setText(userInfo.UserName+","+userInfo.age+","+userInfo.gender+","+userInfo.address);
//复杂类型数据获取-Parcelable
Order order = getIntent().getParcelableExtra("order");
textView.setText(order.address+","+order.isReceived+","+order.count);
}
}

Activity数据回传

  1. startActivityForResult(intent)
  2. onActivityResult

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="带参数传递"
/>
<TextView
android:id="@+id/result"
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
32
33
34
35
36
37
38
39
package com.example.inter;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,InterActivity.class);
//数据回显
startActivityForResult(intent,100);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
TextView textView1 = (TextView) findViewById(R.id.result);
String databe = data.getStringExtra("back");
textView1.setText(databe);

}
}

InterActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.inter;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;

import android.widget.TextView;

import androidx.annotation.Nullable;

public class InterActivity extends Activity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inter_layout);
//数据回显
Intent intent = new Intent();
intent.putExtra("back","bcseazd");
setResult(RESULT_OK,intent);
}
}

启动系统的Activity组件

拨打电话

inter.setAction(Intent.ACTION_DIAL);

发送短信

inter.setAction(Intent.ACTION_SENDTO);

打开相机

inter.setAction(MediaStore.ACTION_CAPTURE);

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨打电话"/>
<Button
android:id="@+id/sendto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送短信"/>
<Button
android:id="@+id/capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开相机"/>

</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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example.interactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button phone = (Button) findViewById(R.id.phone);
Button sendto = (Button) findViewById(R.id.sendto);
Button capture = findViewById(R.id.capture);

phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
startActivity(intent);
}
});
sendto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
Uri uri = Uri.parse("smsto:"+"15525510329");
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(uri);
intent.putExtra("sms_body","这里可以填写短信内容");
startActivity(intent);
}
});
capture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
}
});
}
}

AndroidManifest.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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.interactivity">

<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

隐式调用的意图过滤中的data匹配和type匹配

data匹配的4个规则

  1. 如果Intent没有指定Data相关的字段,只能匹配上没有指定Data的intentfilter;
  2. 如果一个intent只指定了URI没有指定tape(并且type也不能从URI中分析出),只能匹配到仅指定了相应Scheme且没有指定type的intentfilter。
  3. 如果一个intent只指定了type而没有指定URI,它只能匹配到只指定了相应Type且没有指定Scheme的intentfilter;
  4. 如果一个intent既有URI也有type,会匹配上:
    1. URI和type都匹配的intentfilter
    2. 首先type要匹配,另外如果intent的URI是content:或file,且intentfilter没有指定scheme时就会匹配

URI:通用资源标志符。URI代表要操作的数据,Android上可用的每种资源-图像、视频片段等都可以用URI来表示。

URI一般由三部分组成:访问资源的命名机制、存放资源的主机名、资源自身的名称,由路径表示。

URI与URL的区别:

URI与URL都是定位资源位置的,URI是一种更广的定义,URL是URI的一个子集,就是说URL是URI的一部分。

URL是可以直接操作的,但URI不行。